如何在文本框中显示列表框选择的值
问题描述:
我正在使用lisbox显示数据库值....如果我从列表框中选择特定项目,我只想在文本框中显示产品代码....
这是我的代码,用于将数据库值显示到列表框中
I am using lisbox to display the database value....if i select the particular item from listbox i just want to display the product code in textbox.......
This is my code to display the database value into listbox
private void productlist_Load(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select productcode,productname from products", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
listBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString() + "\t\t" + ds.Tables[0].Rows[i][1].ToString());
}
con.Close();
}
如何在文本框中获取产品代码.........
how to get the productcode in textbox.........
答
您可以使用ListBox.SelectItem
属性.您可以在文档中找到代码示例. ^ ].
You may use the ListBox.SelectItem
property. You may find a code sample in the documentation[^].
请使用以下代码:
Hi, use below code:
textBx1.Text = ListBox1.SelectedItem.Value;
您可以使用以下代码显示单个值:
textbox1.text = listbox1.selecteditem.value
如果要选择多个值,则可能需要循环:
对于i = 0到listbox1.items.count-1
如果listbox1.items(i).selected = true,则
textbox1.text = listbox1.items(i).value
如果
结束 下一个
You may use the below code to display a single value:
textbox1.text=listbox1.selecteditem.value
If you want to select multiple values then u may need for loop:
for i=0 to listbox1.items.count-1
if listbox1.items(i).selected=true then
textbox1.text=listbox1.items(i).value
end if
next