473,700 Members | 3,099 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 19077
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
2412
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 with my custom objects (SwapItem), which simply
8
2836
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 both the listboxes. I have the code for my method below. Is there a way I can solve this problem?
0
2045
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
2874
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 the listbox object as an object to set the listitem's selected property before it gets rendered).. ...
6
5212
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 <select> control: used.options = new Option(name, typeId); This all works fine and I can move across...
1
2237
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
3292
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
3839
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 = System.Windows.Forms.SelectionMode.MultiExtended ListBoxUser.Size = New System.Drawing.Size(136, 147) ListBoxUser.TabIndex = 6
2
2758
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 user is not able to know an item it is selected or not unless they scroll through the listbox.
2
3028
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 function which repopulates the listbox. Could this be getting in the way?
0
8713
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9059
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8957
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8912
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7795
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6556
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4396
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4650
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2021
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.