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

前端 未结 6 994
南旧
南旧 2020-12-20 20:49

I am writing a winforms app in which a user selects an item from a listbox and edits some data that forms part of an associated object. The edits are then applied from the o

相关标签:
6条回答
  • 2020-12-20 21:07

    I know this is a very old post, but I was unable to cast my list box items into a Dictionary item. This solution worked for me in .NET 3.5 for windows forms.

    KeyValuePair<int, string> kvp = (KeyValuePair<int, string>)listBoxSystems.SelectedItem;
    string szValue = kvp.Value;
    
    0 讨论(0)
  • 2020-12-20 21:07

    Try grabbing the "ValueMember" from within the ListBox1_SelectedValueChanged event.

    private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
    {
        if (ListBox1.SelectedIndex != -1)
        {
            string orbit = ListBox1.SelectedValue.ToString();
        }
    }
    
    ArrayList planets = new ArrayList();
    planets.Add(new Planet("Mercury", "1"));
    planets.Add(new Planet("Venus", "2"));
    
    //Remember to set the Datasource
    ListBox1.DataSource = planets;
    //Name and Orbit are properties of the 'Planet' class
    ListBox1.DisplayMember = "Name";
    ListBox1.ValueMember = "Orbit";
    
    0 讨论(0)
  • 2020-12-20 21:18

    A simple and clean way:

    Add Item (include Key and Value) to ListBox:

    lsbListBoxName.Items.Insert(0, New ListItem("Item 1", 1))
    lsbListBoxName.Items.Insert(0, New ListItem("Item 2", 2))
    ...
    


    Get Item on user selection:

    Private Sub lsbListBoxName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lsbListBoxName.SelectedIndexChanged
        Console.WriteLine(TryCast(lsbListBoxName.SelectedItem, ListItem).Text)
        Console.WriteLine(TryCast(lsbListBoxName.SelectedItem, ListItem).Value)
    End Sub
    

    the lsbListBoxName is name of ListBox and the code is VB.NET you can use This Online Tool to convent to C#.

    0 讨论(0)
  • 2020-12-20 21:23

    Okay so the answer came as a result of Andy's answer, hence my upvoting that answer.

    But when I created a little class and tried to cast the listitem into that class the program threw an exception.

    Revealingly the exception told me that the program could not cast a DictionaryEntry into a class of the type I had defined.

    So I deleted the proxy class and reframed the request thus:

    DictionaryEntry de = (DictionaryEntry)listbox.SelectedItem;
    string htKey = de.Key.ToString();

    And it's all good.

    Bizarrely simple answer in the end. Thanks for the hint Andy.

    0 讨论(0)
  • 2020-12-20 21:25

    Using both the SelectedIndexChanged and SelectedValueChanged didn't work for me - the ListBox's SelectedValue property was always null. This surprised me, too.

    As a lame workaround, you could just pull the object out of the ListBox directly, using the SelectedIndex:

    public Form1()
    {
        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 void OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if(-1 != this.listBox1.SelectedIndex)
        {
            Test t = this.listBox1.Items[this.listBox1.SelectedIndex] as Test;
            if(null != t)
            {
                this.textBox1.Text = t.Name;
            }
        }
    }
    

    (Test is just a simple class with two properties - ID and Name).

    It seems like there should be a better way, but if nothing else this should work.

    0 讨论(0)
  • 2020-12-20 21:25

    I ended up here when I was searching how to get the value of an Item in ListBox, then the obvious came to my mind.
    The secret is that Item method in C#, VB and others is an array, so to get a value of any Item you just have to write this:

    //Get value of the #1 Item in the ListBox;
    ListBox1.Items[1].toString();
    

    To get all the Items and put into a document or to a string, just do a for like this:

    string Value;
    for (int c = 0; c < ListBox1.Items.Count; c++)  {
        Value = Value + ListBox1.Items[c].toString();
    }
    

    I hope I helped you guys out. This is the most simple answer to your post.

    0 讨论(0)
提交回复
热议问题