Getting value of selected item in list box as string

前端 未结 10 513
长情又很酷
长情又很酷 2020-12-01 09:31

I am trying to get the value of the selected item in the listbox using the code below, but it is always returning null string.

DataSet ds = searchforPrice(Co         


        
相关标签:
10条回答
  • 2020-12-01 09:43

    If you want to retrieve the display text of the item, use the GetItemText method:

    string text = listBox1.GetItemText(listBox1.SelectedItem);
    
    0 讨论(0)
  • 2020-12-01 09:43

    Get FullName in ListBox of files (full path) list (Thomas Levesque answer modificaton, thanks Thomas):

    ...
            string tmpStr = "";
            foreach (var item in listBoxFiles.SelectedItems)
            {
                tmpStr += listBoxFiles.GetItemText(item) + "\n";
            }
            MessageBox.Show(tmpStr);
    ...
    
    0 讨论(0)
  • 2020-12-01 09:44

    If you want to retrieve the item selected from listbox, here is the code...

    String SelectedItem = listBox1.SelectedItem.Value;
    
    0 讨论(0)
  • 2020-12-01 09:48

    If you are using ListBox in your application and you want to return the selected value of ListBox and display it in a Label or any thing else then use this code, it will help you

     private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
             label1.Text  = listBox1.SelectedItem.ToString();
        }
    
    0 讨论(0)
  • 2020-12-01 09:54

    The correct solution seems to be:

    string text = ((ListBoxItem)ListBox1.SelectedItem).Content.ToString();

    Please be sure to use .Content and not .Name.

    0 讨论(0)
  • 2020-12-01 09:58
    string textValue = ((ListBoxItem)listBox1.SelectedItem).Content.ToString();
    
    0 讨论(0)
提交回复
热议问题