CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2008
    Posts
    111

    Exclamation c# programatically selecting dropdown value

    I am new at C# and it seems the following code below does not seem to select my combobox value:
    Code:
        private void button1_Click(object sender, EventArgs e)
        {
           cbPortNumber.SelectedIndex = 3;
        }
    The dropdown looks like this:


    All code above does not seem to select HDMI 4 on the list... The error i have is:

    System.ArgumentOutOfRangeException: InvalidArgument=Value of '2' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)

    Any help would be great!

    **update showing combobox**


    Code:
            // 
            // 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);
    Code:
        #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;
          }
        }
    And this is what fires the action after selecting something in that dropdown:
    Code:
        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);
            }
          }
    Last edited by StealthRT; May 11th, 2013 at 12:27 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: c# programatically selecting dropdown value

    It's actually firing multiple times. Try the CLICK event instead
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured