How do I get at the listbox item's "key" in c# winforms app? -


I am writing a winforms app in which a user selects one item from a listback and edits some data Any related item Then the editing object is applied to a built-in file from the list.

Specifying a separate system value in ASP.NET as a display text in the user is less trivial in the list item. In a winning app, you have to set "displaymember" and "valuemember" of each object which is a bit more complicated (and not related to the internet) in the process.

I have done this in debug mode that I have confirmed that each item has a value that is displayed in the display member (a "friendly" string that looks at the user) and a key, the validum, which Hashtabile keeps the key of the object where the data can be updated is present.

So when a user chooses a string to edit the program, then "is" must be passed in the hashteble, pushing the object and allowing it to be edited.

Hold?

I can not see a clear way to tell the program to see the item value value. I was undoubtedly hoping to populate the "selected value" property of this list box, but it would be far easier. So how do I get the list item value?

change both selected index and selectedValueChanged ListVox SelectedValue property was always empty

As a lame stir, you can directly access ListBox You can drag objects using SelectedIndex from:

  Public form 1 () {InitializeComponent (); This.listBox1.DisplayMember = "name"; This.listBox1.ValueMember = "id"; This.listBox1.Items.Add (new test (1, "A")); This.listBox1.Items.Add (New Test (2, "B")); This.listBox1.Items.Add (new test (3, "C")); This.listBox1.Items.Add (new test (4, "D")); This.listBox1.Items.Add (new test (5, "e")); } Private Zero changed-indented indices (object sender, eventarges e) {if (-1! = This.listbox. Selected indices) {test t = this.libilbox 1 ITEM [THIS. List.Coin 1 Selection] in the form of examination; If (empty! = T) {this.textBox1.Text = t.Name; }}}  

( Test is a simple class with two properties - id and names ) .

It seems that should be done better, but if nothing else then it should work.


Comments