1

I am new at C# and it seems the following code below does not seem to select my combobox value:

private void button1_Click(object sender, EventArgs e)
{

   cbPortNumber.SelectedValue = 3;

or

   cbPortNumber.setValue("3");

or

   cbPortNumber.SelectedIndex = cbPortNumber.FindString("3");

or

   cbPortNumber.SelectedIndex = cbPortNumber.Items.IndexOf(cbPortNumber.Items.FindByValue("HDMI 4"));

}

The dropdown looks like this: enter image description here

All code above does not seem to select HDMI 4 on the list... I dont have any errors but i also don't have it being selected.

Any help would be great!

update showing combobox enter image description here

UPDATE 2

    // 
    // cbPortNumber
    // 
    this.cbPortNumber.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Append;
    this.cbPortNumber.Enabled = false;
    this.cbPortNumber.FormattingEnabled = true;
    this.cbPortNumber.Location = new System.Drawing.Point(174, 40);
    this.cbPortNumber.Name = "cbPortNumber";
    this.cbPortNumber.Size = new System.Drawing.Size(133, 21);
    this.cbPortNumber.TabIndex = 11;
    this.cbPortNumber.Text = "global_hdmi_port";
    this.helpPortNumber.SetToolTip(this.cbPortNumber, "The HDMI port number, to which you connected your USB-CEC adapter.");
    this.cbPortNumber.SelectedIndexChanged += new System.EventHandler(this.cbPortNumber_SelectedIndexChanged);

#region Global settings
public CECSettingByte HDMIPort
{
  get
  {
    if (!_settings.ContainsKey(KeyHDMIPort))
    {
      CECSettingByte setting = new CECSettingByte(KeyHDMIPort, "HDMI port", 1, _changedHandler) { LowerLimit = 1, UpperLimit = 15, EnableSetting = EnableHDMIPortSetting };
      setting.Format += delegate(object sender, ListControlConvertEventArgs args)
      {
        ushort tmp;
        if (ushort.TryParse((string)args.Value, out tmp))
          args.Value = "HDMI " + args.Value;
      };

      Load(setting);
      _settings[KeyHDMIPort] = setting;
    }
    return _settings[KeyHDMIPort].AsSettingByte;
  }
}

Update 3

And this is what fires the action after selecting something in that dropdown:

private void OnSettingChanged(CECSetting setting, object oldValue, object newValue)
{
  if (setting.KeyName == CECSettings.KeyHDMIPort)
  {
    CECSettingByte byteSetting = setting as CECSettingByte;
    if (byteSetting != null)
    {
      if (!Settings.OverridePhysicalAddress.Value)
        Config.HDMIPort = byteSetting.Value;
      CECActions.SetConnectedDevice(Settings.ConnectedDevice.Value, byteSetting.Value);
    }
  }
0

1 Answer 1

1

So this code is working fine for me:

private void button1_Click(object sender, EventArgs e)
{
    comboBox1.SelectedIndex = 2;
}

you can not access a ItemSource if there are no items to access. The simple way is to Init the items over the Desinger

enter image description here

( Sory for the nonlocalized IDE ) than you can set the Property SelectedIndex to a Index that exits. The other way is to Add all HDMI items with the Combobox1.Items.Add function.

If you ever used Forms in VB ... its still the same

public Form1()
{
    InitializeComponent();

    var hdmi = "HDMI";

    for (int i = 1; i < 15; i++)
    {
        comboBox1.Items.Add( hdmi + i);
    }
}

private void button1_Click(object sender, EventArgs e)
{
    if (comboBox1.Items.Count >= 2)
        comboBox1.SelectedIndex = 2;
}
Sign up to request clarification or add additional context in comments.

12 Comments

Adding breakpoints at the start of the event handlers will show you if/when they're being triggered.
Using your code i get this: System.ArgumentOutOfRangeException: InvalidArgument=Value of '2' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)
be sure that you jusing the right combobox! set a breakpoint to the assignment and look whats inside the ItemSouce
I am using the correct combobox. See updated OP for an image.
How do you fill your ItemSource?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.