Amr*_*rma 22 c# listbox winforms
我试图使用下面的代码获取列表框中所选项的值,但它总是返回空字符串.
DataSet ds = searchforPrice(Convert.ToString(listBox1.SelectedItem));
Run Code Online (Sandbox Code Playgroud)
在这里,我试图将所选项的值作为字符串传递给方法searchforPrice以从数据库中检索数据集.
如何将所选项目的值作为字符串进行检索?
我正在从组合框添加项目到列表框,组合框又从数据库加载项目.
listBox1.Items.Add(comboBox2.Text);
Run Code Online (Sandbox Code Playgroud)

任何人都有这个答案..
Tho*_*que 75
如果要检索项目的显示文本,请使用以下GetItemText方法:
string text = listBox1.GetItemText(listBox1.SelectedItem);
Run Code Online (Sandbox Code Playgroud)
如果您在应用程序中使用ListBox并且想要返回ListBox的选定值并将其显示在Label或任何其他内容中,那么使用此代码,它将帮助您
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = listBox1.SelectedItem.ToString();
}
Run Code Online (Sandbox Code Playgroud)