C# 在不循环的情况下在 ListBox 中设置所选项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/877519/
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
Setting selected item in a ListBox without looping
提问by SO User
I have a multi-select listbox which I am binding to a DataTable. DataTable contains 2 columns description and value.
我有一个多选列表框,我绑定到一个数据表。DataTable 包含 2 列描述和值。
Here's the listbox populating code:
这是列表框填充代码:
DataTable copytable = null;
copytable = GlobalTable.Copy(); // GlobalTable is a DataTable
copytable.Rows[0][0] = "--ALL--";
copytable.Rows[0][1] = "--ALL--";
breakTypeList.DataSource = copytable;
this.breakTypeList.DisplayMember = copytable.Columns[0].ColumnName; // description
this.breakTypeList.ValueMember = copytable.Columns[1].ColumnName; // value
this.breakTypeList.SelectedIndex = -1;
I am setting description as the DisplayMember and value as the ValueMember of the ListBox. Now depending on what the value is passed I need to set the selected item in the ListBox.
我将 description 设置为 DisplayMember,将 value 设置为 ListBox 的 ValueMember。现在,根据传递的值,我需要在 ListBox 中设置所选项目。
Here's my code:
这是我的代码:
ListBox lb = c as ListBox;
lb.SelectedValue = valuePassedByUser;
which is not working. Hence I have to resort to the code below (where I loop through all the items in the list box)
这是行不通的。因此我不得不求助于下面的代码(我遍历列表框中的所有项目)
for (int i = 0; i < lb.Items.Count; i++)
{
DataRowView dr = lb.Items[i] as DataRowView;
if (dr["value"].ToString() == valuePassedByUser)
{
lb.SelectedIndices.Add(i);
break;
}
}
I would like to know what is missing/ erroneous in my code. Why is lb.SelectedValue = valuePassedByUser; selecting incorrect items?
我想知道我的代码中缺少什么/错误。为什么是 lb.SelectedValue = valuePassedByUser; 选择不正确的项目?
采纳答案by SO User
Ok ... here comes hard-to-digest answer which I realized only yesterday. It's my mistake though that I didn't mention one important thing in my question because I felt it is irrelevant to problem at hand:
好的......这是我昨天才意识到的难以消化的答案。虽然我没有在我的问题中提到一件重要的事情是我的错误,因为我觉得它与手头的问题无关:
The data in the data table was not sorted. Hence I had set the listbox's Sorted property to true. Later I realized When the listbox's or even combo box's sorted property is set to true then the value member does not get set properly. So if I write:
数据表中的数据没有排序。因此,我已将列表框的 Sorted 属性设置为 true。后来我意识到当列表框甚至组合框的 sorted 属性设置为 true 时,值成员没有正确设置。所以如果我写:
lb.SelectedValue = valuePassedByUser;
it sets some other item as selected rather than settting the one whose Value is valuePassedByUser. In short it messes with the indexes.
它将一些其他项目设置为选中,而不是设置其值为 valuePassedByUser 的项目。简而言之,它混淆了索引。
For e.g. if my initial data is:
例如,如果我的初始数据是:
Index ValueMember DisplayMember
1 A Apple
2 M Mango
3 O Orange
4 B Banana
And I set sorted = true. Then the listbox items are:
我设置了 sorted = true。然后列表框项目是:
Index ValueMember DisplayMember
1 A Apple
2 B Banana
3 M Mango
4 O Orange
Now if I want to set Banana as selected, I run the stmt:
现在,如果我想将 Banana 设置为选中状态,则运行 stmt:
lb.SelectedValue = "B";
But instead of setting Banana as selected, it sets Orange as selected. Why? Because had the list not been sorted, index of Banana would be 4. So even though after sorting index of Banana is 2, it sets index 4 as selected, thus making Orange selected instead of Banana.
但不是将香蕉设置为选中状态,而是将橙色设置为选中状态。为什么?因为如果列表没有排序,Banana 的索引将是 4。所以即使在 Banana 的排序索引为 2 之后,它也会将索引 4 设置为选中,从而选择 Orange 而不是 Banana。
Hence for sorted listbox, I am using the following code to set selected items:
因此,对于排序列表框,我使用以下代码来设置所选项目:
private void SetSelectedBreakType(ListBox lb, string value)
{
for (int i = 0; i < lb.Items.Count; i++)
{
DataRowView dr = lb.Items[i] as DataRowView;
if (dr["value"].ToString() == value)
{
lb.SelectedIndices.Add(i);
break;
}
}
}
回答by scorpio
Try this:-
尝试这个:-
var listBox = c as ListBox;
var item = listBox.Items.FindByValue(fieldValue);
if (item != null)
listBox.SelectedValue = fieldValue;
回答by norlando
I think the only way you'll be able to select multiple items is by using a foreach loop. The SelectedValue property only seems to return 1 item. If you want to select more then 1 item you'll have to use :
我认为您能够选择多个项目的唯一方法是使用 foreach 循环。SelectedValue 属性似乎只返回 1 个项目。如果要选择多于 1 个项目,则必须使用:
var tempListBox = c As ListBox;
if (tempListBox != null)
(tempListBox.SelectedItems.Add(tempListBox.Items[tempListBox.FindStringExact(fieldValue)]);
Also the FindStringExact doesn't search through the Value fields it only looks through the displayed text. Also to cut down on code might want to cast a new variable as a listbox so you don't keep casting C as a listbox.
此外, FindStringExact 不搜索 Value 字段,它只查看显示的文本。此外,为了减少代码,您可能希望将新变量转换为列表框,这样您就不会继续将 C 转换为列表框。
回答by idleMind
You can use "FindByValue" like this:
您可以像这样使用“FindByValue”:
ListBox.SelectedIndex = ListBox.Items.IndexOf(ListBox.Items.FindByValue(fieldValue))
回答by Hisham Elsayad
this.Character.SetSelected(this.Character.Items.IndexOf(this.textBox1.Text),true);
回答by Ben Winding
Here's how I solved it, using winforms, DotNet 4.6
这是我解决它的方法,使用 winforms,DotNet 4.6
listBox1.SelectedIndex = listBox1.FindString(stringInList);
回答by MuhammadWaseem
If you do not want looping for selected items then retrieve selected value of list box from the listBox_SelectedIndexChanged
event and add that value in global array. Then by accessing that array you would get desire selected items value of itemlist
with out any loop.
如果您不想循环选定项目,则从listBox_SelectedIndexChanged
事件中检索列表框的选定值并将该值添加到全局数组中。然后通过访问该数组,您将itemlist
在没有任何循环的情况下获得所需的选定项目值。