Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将包含文本/索引项的组合框设置为特定项_C#_.net_Winforms - Fatal编程技术网

C# 如何将包含文本/索引项的组合框设置为特定项

C# 如何将包含文本/索引项的组合框设置为特定项,c#,.net,winforms,C#,.net,Winforms,据我所知,Windows窗体中的组合框只能包含一个值。我需要一个文本和索引,所以我创建了这个小类: public class ComboboxItem { public string Text { get; set; } public object Value { get; set; } public override string ToString() { return Text; } } 我向组合框添加一个项目,如下所示:

据我所知,Windows窗体中的组合框只能包含一个值。我需要一个文本和索引,所以我创建了这个小类:

public class ComboboxItem { 
    public string Text { get; set; } 
    public object Value { get; set; } 
    public override string ToString() 
    { 
        return Text; 
    }
}
我向组合框添加一个项目,如下所示:

ComboboxItem item = new ComboboxItem()
{
    Text = select.Item1,
    Value = select.Item2
};

this.comboBoxSelektion.Items.Add(item);
this.comboBoxSelektion.SelectedItem = (from ComboBoxItem i in this.comboBoxSelektion.Items where i.Value == 1 select i).FirstOrDefault();
现在我的问题是:如何将组合框设置为特定项? 我试过这个,但没用:

this.comboBoxSelektion.SelectedItem = new ComboboxItem() { Text = "Text", Value = 1};

您提供的上一个代码示例不起作用,因为
组合框中的项
和您通过
new
创建的项是不同的实例(=内存引用),它们不相同(两个不同的内存指针),即使它们相等(它们的成员具有相同的值)。仅仅因为两个对象包含相同的数据并不能使它们成为相同的对象,而是使它们成为两个相等的不同对象

这就是为什么通常在
o1==o2
o1.Equals(o2)之间有很大的区别

示例:

ComboboxItem item1 = new ComboBoxItem() { Text = "Text", Value = 1 };
ComboboxItem item2 = new ComboBoxItem() { Text = "Text", Value = 1 };
ComboboxItem item3 = item1;

item1 == item2      => false
item1.Equals(item2) => true, if the Equals-method is implemented accordingly
item1 == item3      => true!! item3 "points to the same object" as item1
item2.Equals(item3) => true, as above
您需要做的是查找添加到列表中的相同实例。您可以尝试以下方法:

ComboboxItem item = new ComboboxItem()
{
    Text = select.Item1,
    Value = select.Item2
};

this.comboBoxSelektion.Items.Add(item);
this.comboBoxSelektion.SelectedItem = (from ComboBoxItem i in this.comboBoxSelektion.Items where i.Value == 1 select i).FirstOrDefault();

这将从分配给值为
1
的组合框的项目中选择第一个项目,并将其设置为所选项目。如果没有此类项,
null
被设置为
SelectedItem

谢谢。几乎完美。在比较中,只需将i.value强制转换为int即可。@Luke:这是因为
value
声明为
object