473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Listbox selected items

Is the following a correct representation of the relationship of the selected
index to selected item:
INDEX ITEM
0 "item 1"
1 "item 2"
2 "item 3"
and so on.
I keep getting the same string for selected index 0 and 1; i.e. both are
"item 1".
I'm new to C# and not exactly an accomplished programmer. Any suggestions?
Aug 18 '06 #1
3 19033
Do you have a short sample that illustrates this? The following works fine:

using System;
using System.Windows.Forms;
class Program {
static void Main() {
using(Form f = new Form())
using (ListBox lb = new ListBox()) {
lb.Items.AddRange(new object[] { "Item1", "Item2", "Item3" });
lb.SelectedIndexChanged += delegate {
f.Text = string.Format("{0}: {1}", lb.SelectedIndex,
lb.SelectedItem);
};
f.Controls.Add(lb);
f.ShowDialog();
}
}
}
Aug 18 '06 #2
No, it is not a correct representation of the relationship of the selected
index to the item. I'll tell you why.

A ListBox has an Items Collection, which is a Collection of objects. The
SelectedIndex of the ListBox is the index in the Collection of the Selected
Item. This has no correspondence whatsoever with the values viewed in the
ListBox, other than the fact that each value viewed is a ListItem, and
naturally that they are in the order of their indices. For example, consider
the following:

INDEX ITEM
0 "item 3"
1 "item 1"
2 "item 2"

Notice that the item with index 0 is named "item 3". They will appear in the
ListBox in the order on the left, but will display the values in the order
on the right.

Now, here's another example:

INDEX ITEM
0 "item 3"
1 "item 3"
2 "item 3"

Each of these is a separate item, but they all have the same text. There is
nothing to prevent you from putting multiple items with the same displayed
value in a ListBox.

So, what I'm getting at is, you have not established that the string you see
is the same string, or whether both values are the same. In addition, you
haven't specified how you "keep getting the same thing." That is a very
human and inexact way to describe your experience. It would be more helpful
to say "when I click the listbox, I see this in the first item, that in the
second..." and so on. Or whatever it is that you did, and what you
observed. In other words, describe your experiment, what you did, what you
saw, when and where you saw it, in detail. Then we can help you out more.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
"Mitch" <Mi***@discussions.microsoft.comwrote in message
news:45**********************************@microsof t.com...
Is the following a correct representation of the relationship of the
selected
index to selected item:
INDEX ITEM
0 "item 1"
1 "item 2"
2 "item 3"
and so on.
I keep getting the same string for selected index 0 and 1; i.e. both are
"item 1".
I'm new to C# and not exactly an accomplished programmer. Any
suggestions?

Aug 18 '06 #3
And for multi-select:

using System;
using System.Windows.Forms;
class Program {
static void Main() {
using(Form f = new Form())
using (ListBox lb = new ListBox()) {
lb.SelectionMode = SelectionMode.MultiExtended;
lb.Items.AddRange(new object[] { "Item1", "Item2", "Item3" });
lb.SelectedIndexChanged += delegate {
string text = ""; // low throughput, we'll use concatenation
for now...
foreach (int index in lb.SelectedIndices) {
text+=string.Format("{0}: {1};", index,
lb.Items[index]);
}
f.Text = text;
};
f.Controls.Add(lb);
f.ShowDialog();
}
}
}
Aug 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
8
by: Vipin Kedia | last post by:
Hi I have written a code for showing the list boxes as selected using a Listitem and the selected property of the items. Now I have 2 list boxes in my page. But it shows only the selected values...
0
by: bill yeager | last post by:
Duray, it helps in regards to knowing how to get the items that were selected in the lisbox, but I'm going in reverse --- after I get the items from the db that were selected, I'd like to be able...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
6
by: Janaka | last post by:
Help! I have two ListBox controls on my web form. The first one gets populated on entry with values from the DB. I then use JavaScript to copy options from this ListBox to my second one. (I...
1
by: sab | last post by:
Hi, We have a web form with a listbox. The listbox is a multiple select listbox and has data that looks something like: ALL Unit 1 Unit 2 Unit 3 Note: "ALL" is always the first item in...
10
by: Adam Clauss | last post by:
I have a page containing a list box. This list may contain duplicate items - in which the ORDER is important. ex: a b b a is significant as compared to: b
2
by: tangokilo | last post by:
Hello and thanks for your help, I have the following Listbox created in VisualStudio 2003 designer, desiring to select multiple entries from that list: -------------------------------...
2
by: John | last post by:
I have a listbox that is databound when my form loads. A user can then select and option using a drop down box. When the user selects an option the corresponding items in the listbox gets selected....
2
by: ProgrammerChicago | last post by:
I'm not sure if this is the result of the postback behavior or my own code, but for some reason my onclick function is not detecting listbox selections (It's meant to delete files uploaded to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.