ListBox 从索引中获取值
ListBox get value from index
我正在编写一个程序,您可以在其中在列表框中插入一些数字,然后单击按钮应该从列表框中获取值并检查它们是正数还是负数并在文本框中显示彼此的计数。
我尝试通过以下方式获取值:string x = listBox1.Items[index].Value;
但它似乎不起作用。
如果您将项目添加到列表框中:
listBox1.Items.Add(textBox1.Text);
然后您可以按如下方式从给定索引中检索项目:
string x = listBox1.Items[index];
索引器正在返回值,在这种情况下它是一个字符串。可能您可能需要将其转换为字符串,因为索引器实际上是 returns 对象 - 请参阅此处:ListBox.ObjectCollection.Item Property :
string x = (string)listBox1.Items[index];
你也可以试试
string s = (string)listbox1.Items.GetItemAt(index);
您必须将其转换为字符串,因为它返回一个对象
我正在编写一个程序,您可以在其中在列表框中插入一些数字,然后单击按钮应该从列表框中获取值并检查它们是正数还是负数并在文本框中显示彼此的计数。
我尝试通过以下方式获取值:string x = listBox1.Items[index].Value;
但它似乎不起作用。
如果您将项目添加到列表框中:
listBox1.Items.Add(textBox1.Text);
然后您可以按如下方式从给定索引中检索项目:
string x = listBox1.Items[index];
索引器正在返回值,在这种情况下它是一个字符串。可能您可能需要将其转换为字符串,因为索引器实际上是 returns 对象 - 请参阅此处:ListBox.ObjectCollection.Item Property :
string x = (string)listBox1.Items[index];
你也可以试试
string s = (string)listbox1.Items.GetItemAt(index);
您必须将其转换为字符串,因为它返回一个对象