开发者

Setting the selected value of a ListBoxView as a string (C#)

When a value is selected in a ListBoxView, How could the selection be stored as a string / variable, so I can then call a开发者_如何学Cnother method that will use this string to perform an action?


If the Items in your listbox are all strings, you could use

System.Windows.Forms.ListBox lb= new ListBox();
            lb.SelectedItem.ToString();

Otherwise, we will need to know the types of items you have added to the listbox.


If you using ListBox get selected item like this:

string item = listBox1.SelectedItem.ToString();

If you using ListView get selected item(s) depending on MultiSelect property:

  1. Use this code if MultiSelect property = false

    string itemName = listView.SelectedItems[0].Name;
    
  2. Use this code if MultiSelect property = true

    foreach (ListViewItem item in ltvMainMenu.SelectedItems)
    {
       string itemName = item.Name;    
    }
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜