简体   繁体   English

设置枚举 ComboBox 的起始值

[英]Setting the start value of an enum ComboBox

been searching for a solution for this problem, found plenty solutions, but nothing changed my code's behaviour.一直在寻找这个问题的解决方案,找到了很多解决方案,但没有改变我的代码的行为。 This is in WinForms.这是在 WinForms 中。

I am loading a form with a ComboBox, that contains the values and names of this enum, that is inside a class named "Node".我正在加载一个带有 ComboBox 的表单,该表单包含该枚举的值和名称,位于名为“Node”的类中。

class Node
{
    public enum NodeType { Yield, Home, Parking, Light, None, Inbound, Outbound }
    public NodeType Type;
}

Then in my form, I have a ComboBox named "Type", which is set up like so (from the constructor):然后在我的表单中,我有一个名为“Type”的 ComboBox,它是这样设置的(来自构造函数):

Node node = new Node();
node.Type = Node.NodeType.Home;

Type = new ComboBox();
Type.Location = new Point(77, 41);
Type.Size = new Size(121, 24);
Type.DropDownStyle = ComboBoxStyle.DropDownList;
Type.DisplayMember = "Name";
Type.ValueMember = "Value";
Type.DataSource = Enum.GetValues(typeof(Node.NodeType));
Type.SelectedValue = node.Type;
Controls.Add(Type);

When the program runs, the list shows all the names, and on closing the form I am able to retrieve the selected value via.程序运行时,列表显示所有名称,关闭表单后,我可以通过检索所选值。 Type.SelectedValue.类型.SelectedValue。 My problem is that the ComboBox doesn't start at the value that the Node is already set at.我的问题是 ComboBox 不是从节点已经设置的值开始。 Essentially the line本质上是线

Type.SelectedValue = node.Type;

doesn't do anything.什么都不做。 I've tried using SelectedItem which didn't change anything, and我试过使用没有改变任何东西的 SelectedItem,并且

Type.SelectedIndex = (int)node.Type;

Which caused an ArgumentOutOfRangeException.这导致了 ArgumentOutOfRangeException。

So, my question is: how do I set the start value of the ComboBox?所以,我的问题是:如何设置 ComboBox 的起始值?

There are several mistakes in that code.该代码中有几个错误。

First, enum does not have Name and Value properties (in fact it does not have any property), so DisplayMember and ValueMember cannot be used and should be left blank (default).首先, enum没有NameValue属性(实际上它没有任何属性),因此不能使用DisplayMemberValueMember ,应该留空(默认)。 Which in turn means SelectedValue cannot be used and you need to use SelectedItem instead.这反过来又意味着SelectedValue无法使用,你需要使用SelectedItem代替。

Second, you are using list data bound mode for the list portion of your ComboBox by setting DataSource property instead of populating Items , which is fine, but data binding occurs later in the process, so inside the constructor the Items property is empty and SelectedItem has no effect.其次,您通过设置DataSource属性而不是填充Items来为ComboBox的列表部分使用列表数据绑定模式,这很好,但数据绑定发生在该过程的后期,因此在构造函数中, Items属性为空,而SelectedItem具有没有效果。 In order to fix that, you need to move the data initialization part to your form Load event.为了解决这个问题,您需要将数据初始化部分移动到您的表单Load事件中。

So, in your form constructor you'll have this:因此,在您的表单构造函数中,您将拥有以下内容:

Type = new ComboBox();
Type.Location = new Point(77, 41);
Type.Size = new Size(121, 24);
Type.DropDownStyle = ComboBoxStyle.DropDownList;
Controls.Add(Type);

and in your form Load event - this:并在您的表单Load事件中 - 这个:

Node node = new Node();
node.Type = Node.NodeType.Home;

Type.DataSource = Enum.GetValues(typeof(Node.NodeType));
Type.SelectedItem = node.Type;

Type.SelectedIndex = 枚举中的索引,例如枚举是 {"apple", "pear", "pineapple"},并且您想要默认为 pear,所以 Type.SelectedIndex = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM