[英]get selected item id in listbox
How do I get the select item in a listbox and pass it on to another class. 如何在列表框中获取选择项并将其传递给另一个类。 At the moment it does display the item id.
目前,它确实显示了商品ID。
private void moveup_Click(object sender, EventArgs e)
{
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{
MessageBox.Show(listBox1.SelectedItems[i].ToString());
}
}
any help appreciated. 任何帮助表示赞赏。
Try this one, use SelectedItems[i].Text instead of SelectedItems[i].ToString() 试试这个,使用SelectedItems [i] .Text代替SelectedItems [i] .ToString()
private void moveup_Click(object sender, EventArgs e)
{
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{
MessageBox.Show(listBox1.SelectedItems[i].Text);
}
}
If I understand you correctly, you want the text from the listbox? 如果我对您的理解正确,那么您想要列表框中的文本吗? Instead of .ToString(), use .text
代替.ToString(),使用.text
private void moveup_Click(object sender, EventArgs e)
{
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{
MessageBox.Show(listBox1.SelectedItems[i].text);
}
}
To pass it to another class, you can then use it as a parameter or save it as an independent variable, or save the selected items to a list, then pass that to a class. 要将其传递给另一个类,则可以将其用作参数或将其另存为自变量,或者将所选项目保存到列表中,然后将其传递给类。
Hope that helps. 希望能有所帮助。
您可以将对象添加到接受对象的列表框集合中,然后将选定的项目转换为您使用的任何类型。
what exactly do you mean by passing it a to a new class? 将其传递给新班级的确切含义是什么?
if you are creating a new class, you can use it in the constructor, by defining a constructor which accepts a string parameter 如果要创建一个新类,则可以通过定义一个接受字符串参数的构造函数在构造函数中使用它
if you are setting property of a class, you can pass it as a parameter to a public method of the class or you can set it directly too. 如果要设置类的属性,则可以将其作为参数传递给该类的公共方法,也可以直接设置它。
let the list box is lstOptions. 让列表框为lstOptions。
So lstOptions.SelectedItems[0].text 所以lstOptions.SelectedItems [0] .text
Gives you first selected item's TEXT in the listbox. 在列表框中为您提供第一个选定项目的TEXT。
您可以将所选项目转换为目标类型,然后将其传递给另一个对象。
otherObject.PassObject(listBox1.SelectedItems as WhatEver);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.