【问题标题】:Getting value of selected item in list box as string获取列表框中所选项目的值作为字符串
【发布时间】:2013-02-06 19:49:52
【问题描述】:

我正在尝试使用下面的代码获取列表框中所选项目的值,但它总是返回空字符串。

DataSet ds = searchforPrice(Convert.ToString(listBox1.SelectedItem));

这里我试图将所选项目的值作为字符串传递给方法 searchforPrice 以从数据库中检索数据集。

如何将所选项目的值检索为字符串?

我正在从组合框向列表框添加项目,组合框又从数据库中加载项目。

 listBox1.Items.Add(comboBox2.Text);

任何人都有答案..

【问题讨论】:

  • 请贴出加载列表框的代码。

标签: c# winforms listbox


【解决方案1】:

如果要检索项目的显示文本,请使用GetItemText 方法:

string text = listBox1.GetItemText(listBox1.SelectedItem);

【讨论】:

  • @AmritSharma,你检查过SelectedItem 不为空吗?
  • @ThomasLevesque 查看上面的截图
  • @ThomasLevesque,是的,selectedItem 值为空
  • @BaruchAtta GetItemText 绝对是exists(继承自ListControl)。我的猜测是您使用的不是 Windows 窗体,而是 WPF 或 UWP,它们具有不同的 ListBox 控件。
  • @BaruchAtta 你看我的评论了吗?您没有使用我的答案适用的相同 UI 框架。问题是关于 Windows 窗体(标签中的winforms),这个答案适用于 Windows 窗体,它肯定有 GetItemText 方法。如果您使用的是 WPF 或 UWP 等其他 UI 框架,则答案不适用。您使用 VS2017 的事实并没有说明您使用的是哪个 UI 框架,因为存在多个。
【解决方案2】:

如果您在应用程序中使用 ListBox,并且想要返回 ListBox 的选定值并将其显示在 Label 或其他任何东西中,请使用此代码,它将对您有所帮助

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
         label1.Text  = listBox1.SelectedItem.ToString();
    }

【讨论】:

  • 已经尝试过您建议的代码,它输出的字符串是: System.Windows.Controls.ListViewItem: AndThenTheActualString - 如何摆脱冗长的 system.windows - 没人想要看到了吗?
  • @BKSpureon 答案适用于 ListBox。您的代码使用的是 ListView,这就是为什么那里有“System.Windows.Controls.ListViewItem”。
【解决方案3】:

要检索 à listbox 中所有选定项的值,您可以在 DataRowView 中转换选定项,然后选择数据所在的列:

foreach(object element in listbox.SelectedItems) {
    DataRowView row = (DataRowView)element;
    MessageBox.Show(row[0]);
}

【讨论】:

    【解决方案4】:
    string textValue = ((ListBoxItem)listBox1.SelectedItem).Content.ToString();
    

    【讨论】:

    • 这样使用as 不是一个好主意。它看起来更干净,但它的格式不正确。 as 用于检查对象是否为某种类型。你为这项工作使用了错误的工具。最好投正确((ListBoxItem)listBox1.SelectedItem)
    • 感谢 PC Luddite 的解释,会更正它。
    【解决方案5】:

    如果您想从列表框中检索您的值 你应该试试这个:

    String itemSelected = numberListBox.GetItemText(numberListBox.SelectedItem);
    

    【讨论】:

    • GetItemText 方法不可用...请帮助...我正在开发 windows phone 应用程序
    【解决方案6】:

    在文件列表框中获取 FullName(完整路径)列表(Thomas Levesque 回答修改,感谢 Thomas):

    ...
            string tmpStr = "";
            foreach (var item in listBoxFiles.SelectedItems)
            {
                tmpStr += listBoxFiles.GetItemText(item) + "\n";
            }
            MessageBox.Show(tmpStr);
    ...
    

    【讨论】:

      【解决方案7】:

      你可以使用这个来获取选中的 ListItme Name ::

      String selectedItem = ((ListBoxItem)ListBox.SelectedItem).Name.ToString();
      

      确保您的每个 ListBoxItem 都有一个 Name 属性

      【讨论】:

        【解决方案8】:

        详细说明 Pir Fahim 先前的回答,他是对的,但我使用的是 selectedItem.Text(唯一能让它对我有用的方法)

        使用 SelectedIndexChanged() 事件将数据存储在某处。 就我而言,我通常填写一个自定义类,例如:

        class myItem {
            string name {get; set;}
            string price {get; set;}
            string desc {get; set;}
        }
        
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
             myItem selected_item = new myItem();
             selected_item.name  = listBox1.SelectedItem.Text;
             Retrieve (selected_item.name);
        }
        

        然后您可以从“myItems”列表中检索其余数据..

        myItem Retrieve (string wanted_item) {
            foreach (myItem item in my_items_list) {
                if (item.name == wanted_item) {
                       // This is the selected item
                       return item; 
                }
            }
            return null;
        }
        

        【讨论】:

          【解决方案9】:

          正确的解决方案似乎是:

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

          请务必使用 .Content 而不是 .Name

          【讨论】:

            【解决方案10】:

            如果你想检索从列表框中选择的项目,这里是代码...

            String SelectedItem = listBox1.SelectedItem.Value;
            

            【讨论】:

            • listBox1.SelectedItem.Value; // listBox1.SelectedItem 和的 .Value 部分似乎没有出现在我的 IDE 中。任何指针?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-06-08
            • 1970-01-01
            • 1970-01-01
            • 2012-11-16
            • 1970-01-01
            相关资源
            最近更新 更多