c# 如何在字符串中有值名称时选择列表框项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1137083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# How do I select a list box item when I have the value name in a string?
提问by
I have a string 'item3' and a listbox with 'item1,item2,item3,item4', how do I select item3 in the list box when I have the item name in a string?
我有一个字符串 'item3' 和一个带有 'item1,item2,item3,item4' 的列表框,当我在字符串中有项目名称时,如何在列表框中选择 item3?
Thanks
谢谢
采纳答案by Nescio
int index = listBox1.FindString("item3");
// Determine if a valid index is returned. Select the item if it is valid.
if (index != -1)
listBox1.SetSelected(index,true);
回答by Lazarus
Isn't SelectedValue read/write?
SelectedValue 不是读/写吗?
回答by Patrick McDonald
listBox.FindStringExact("item3");
Returns the index of the first item found, or ListBox.NoMatches if no match is found.
返回找到的第一个项目的索引,如果没有找到匹配项,则返回 ListBox.NoMatches。
you can then call
然后你可以打电话
listBox.SetSelected(index, true);
to select this item
选择此项
回答by bruno conde
Try with ListBox.SetSelected
method.
用ListBox.SetSelected
方法试试。
回答by Micha? Ziober
Maybe like this:
也许是这样的:
public bool SelectItem(ListBox listBox, string item)
{
bool contains = listBox.Items.Contains(item);
if (!contains)
return false;
listBox.SelectedItem = item;
return listBox.SelectedItems.Contains(item);
}
Test method:
测试方法:
public void Test()
{
string item = "item1";
if (!SelectItem(listBox, item))
{
MessageBox.Show("Item not found.");
}
}
回答by SO User
SelectedValue will work only if you have set the ValueMember for the listbox.
只有在您为列表框设置了 ValueMember 时,SelectedValue 才会起作用。
Further, even if you do set the ValueMember, selectedValue will not work if your ListBox.Sorted = true.
此外,即使您确实设置了 ValueMember,如果您的 ListBox.Sorted = true,selectedValue 也不会起作用。
Check out my post on Setting selected item in a ListBox without looping
查看我关于在 ListBox 中设置所选项目而不循环的帖子
You can try one of these approaches:
您可以尝试以下方法之一:
lb.SelectedValue = fieldValue;
lb.SelectedIndex = lb.FindStringExact(fieldValue);
This is a generic method for all listboxes. Your implementation will change based on what you are binding to the list box. In my case it is DataTable.
private void SetSelectedIndex(ListBox lb, string value) { for (int i = 0; i < lb.Items.Count; i++) { DataRowView dr = lb.Items[i] as DataRowView; if (dr["colName"].ToString() == value) { lb.SelectedIndices.Add(i); break; } } }
lb.SelectedValue = fieldValue;
lb.SelectedIndex = lb.FindStringExact(fieldValue);
这是所有列表框的通用方法。您的实现将根据您绑定到列表框的内容而变化。就我而言,它是 DataTable。
private void SetSelectedIndex(ListBox lb, string value) { for (int i = 0; i < lb.Items.Count; i++) { DataRowView dr = lb.Items[i] as DataRowView; if (dr["colName"].ToString() == value) { lb.SelectedIndices.Add(i); break; } } }
回答by Four
static class ControlHelper
{
public static void SelectExactMatch(this ComboBox c, string find)
{
c.SelectedIndex = c.FindStringExact(find, 0);
}
}
回答by AKV
CheckBoxList.Items.FindByValue("Value").Selected = true;