473,566 Members | 3,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19069
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.AddRan ge(new object[] { "Item1", "Item2", "Item3" });
lb.SelectedInde xChanged += delegate {
f.Text = string.Format(" {0}: {1}", lb.SelectedInde x,
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***@discussi ons.microsoft.c omwrote in message
news:45******** *************** ***********@mic rosoft.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.SelectionMod e = SelectionMode.M ultiExtended;
lb.Items.AddRan ge(new object[] { "Item1", "Item2", "Item3" });
lb.SelectedInde xChanged += delegate {
string text = ""; // low throughput, we'll use concatenation
for now...
foreach (int index in lb.SelectedIndi ces) {
text+=string.Fo rmat("{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
2391
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 writing a usercontrol which displays the typical two listboxes and the ability to move items from one to the other. The listboxes are populated...
8
2826
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 of the last list box in both the list boxes. If i reverse the calls to the filllistbox methods it shows the value selected for the 1st list box in...
0
2037
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 to highlight them in the listbox within the datagrid control......... Thanks, Bill... >-----Original Message-----
6
2863
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 a denormalized mirror of the database, but I'm not having much luck getting the selection logic down (I haven't found a 'hook' where I can access...
6
5193
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 have also tried changing the second ListBox to an HtmlSelect control) using bog standard JavaScript code like so where "used" is the name of my...
1
2229
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 the list.
10
3278
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
3824
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: ------------------------------- ListBoxUser.Location = New System.Drawing.Point(16, 240) ListBoxUser.Name = "ListBoxUser" ListBoxUser.SelectionMode =...
2
2752
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. How can I show those selected items at the top of my listbox. The issue is when the user selects an item and it is in the middle of the list, the...
2
3020
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 server). The RemoveFiles function is being executed, bu I have a feeling the selections are being cleared beforehand. There is a Page_Load...
0
7673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.