1

been searching for a solution for this problem, found plenty solutions, but nothing changed my code's behaviour. This is in 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".

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):

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. My problem is that the ComboBox doesn't start at the value that the Node is already set at. Essentially the line

Type.SelectedValue = node.Type;

doesn't do anything. I've tried using SelectedItem which didn't change anything, and

Type.SelectedIndex = (int)node.Type;

Which caused an ArgumentOutOfRangeException.

So, my question is: how do I set the start value of the ComboBox?

Fisker
  • 41
  • 7
  • Possible duplicate of [Binding an enum to a WinForms combo box, and then setting it](http://stackoverflow.com/questions/906899/binding-an-enum-to-a-winforms-combo-box-and-then-setting-it) – RomCoo Apr 26 '16 at 19:35

2 Answers2

1

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). Which in turn means SelectedValue cannot be used and you need to use SelectedItem instead.

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. In order to fix that, you need to move the data initialization part to your form Load event.

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:

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

Type.DataSource = Enum.GetValues(typeof(Node.NodeType));
Type.SelectedItem = node.Type;
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
0

Type.SelectedIndex = index from your enum, for example the enum is {"apple", "pear", "pineapple"}, and you want pear by deafult, so Type.SelectedIndex = 1

  • This is what I tried by doing Type.SelectedIndex = (int)node.Type; which causes an ArgumentOutOfRangeException, even if I just enter 2 instead of (int)node.Type, samething happens. – Fisker Apr 26 '16 at 18:16
  • if you want to select Light, try Type.SelectedValue = node.Type.Light – Jeisson Malaver Apr 26 '16 at 18:19
  • Tried: Type.SelectedValue = Node.NodeType.Light; The ComboBox still starts at "Yield", the first value in the enumerator. – Fisker Apr 26 '16 at 18:29