Tommehh
Tommehh

Reputation: 956

SelectedItem of a variable combobox

There is a combobox which i fill with this loop

foreach (Machine.Types machine in machineList)
{
    cbMachineGUI.Items.Add(machine);
}

after that i want the selected index to be one specific machine.

string machineComboBox = SettingsManager.getParameter("MachineType");
cbMachineGUI.SelectedItem = machineComboBox;

The parameter is correct and set, but the selecteditem of the combobox is always nothing.

if i set the machines in the properties of the combobox (not via the loop) it works. but i need the combobox to be variable.

any suggestions?

Upvotes: 1

Views: 1041

Answers (4)

svinja
svinja

Reputation: 5576

The problem is that what you put in Items and what you set SelectedItem to are different types.

You are filling the Items collection with Machine.Types instances, and setting SelectedItem to a string instance.

Using IndexOf like other answers suggest will not help, as this will not do anything that setting SelectedItem does not already do. It still won't find machineComboBox in the Items collection, just like it can't find it now.

You need to use matching types, so do one of these things (depending on how else you use the values in the combobox):

  1. Convert Machine.Types to a string when filling the collection:

    cbMachineGUI.Items.Add(machine.ToString());
    
  2. Convert machineComboBox into an instance of Machine.Types that will match the one in Items when setting SelectedItem - how to do it depends on what Machine.Types is

  3. Find the correct item yourself when setting SelectedItem:

    cbMachineGUI.SelectedItem = cbMachineGUI.Items
                                            .OfType<Machine.Types>()
                                            .FirstOrDefault(item => item.ToString() == machineComboBox);
    

Either way, you must make a conversion between these two types somewhere.

Upvotes: 1

Rahul
Rahul

Reputation: 77926

It could be possible that the item you are trying to set doesn't present in combobox item list and since you haven't actually selected anything it sets to nothing. To check if the item does exist do below

string machineComboBox = SettingsManager.getParameter("MachineType");
if(cbMachineGUI.Items.IndexOf(machineComboBox) >= 0)
 cbMachineGUI.SelectedItem = machineComboBox;

Quoting from MSDN documentation:

When you try to set the SelectedItem property to an object, the ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index. If the object does not exist in the list, the SelectedIndex property is left at its current value.The ComboBox class searches for the specified object by using the IndexOf method.

Check ComboBox.SelectedItem for more information.

Upvotes: 0

roemel
roemel

Reputation: 3297

You could try the following:

cbMachineGUI.SelectedIndex = cbMachineGUI.Items.IndexOf("MachineType"); // or whatever you want to select

Upvotes: 0

oppassum
oppassum

Reputation: 1775

Instead of setting SelectedItem, I suggest you find the item's index and set the selected index.

Something like this:

string machineComboBox = SettingsManager.getParameter("MachineType");
int itemIndex = cbMachineGUI.Items.IndexOf(machineComboBox);
cbMachineGUI.SelectedIndex = itemIndex;

Upvotes: 0

Related Questions