1

First of all lets say that i am new to C#... I am using WinForms: I have tried using this code to change the value of the ComboBox with a shortcut. I have also tried using SelectedValue instead of SelectedIndex. Afterwards I tried to focus the ComboBox before or after it changes value. Finally i tried converting this to a stwitch statement, but every time i execute it nothing happens.
I am using Visual Studio 2017 - when i tried to debug it, the debugger showed me that after it executes the code inside the if statement it goes inside the else statement and executes the code there again...

private void Form_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.E)
    {
        //CBoxLimit.Focus();

        if (CBoxLimit.SelectedIndex == 0)
        {
            CBoxLimit.SelectedIndex = 1;
        }

        else
        {
            CBoxLimit.SelectedIndex = 0;
        }

        //CBoxLimit.Focus();
    }
}

Thanks in advance for your help...

Amittai Shapira
  • 3,749
  • 1
  • 30
  • 54
salkow
  • 29
  • 5
  • Try running the code with the help of breakpoint(s) and see if there is any exceptions that are generating. If so, then post what exception is being generated. –  Dec 11 '17 at 14:32
  • I know. By mistake I wrote the response as an answer. I thought I was posting a comment. I even deleted the post. –  Dec 11 '17 at 14:35
  • I tried setting breakpoints on the line where it changes the SelectedIndex... After it executes the code inside the if statement it goes inside the else statement and executes the code there again... – salkow Dec 11 '17 at 14:37
  • What is the `CBoxLimit.SelectedIndex` value before the execution goes into the if statement? –  Dec 11 '17 at 14:43
  • I have tried setting it to 0 or leave it with no value... – salkow Dec 11 '17 at 14:45
  • No, what I am trying to say is, while you are running your program using breakpoints, what is the value of `CBoxLimit.SelectedIndex` before the if statement is executed? –  Dec 11 '17 at 14:47
  • Are there any events couple to this combobox ? Any other code in this form that affects this combobox or its value ? – GuidoG Dec 11 '17 at 14:52
  • No there is nothing other that affects the value of the SelectedIndex... – salkow Dec 11 '17 at 14:59
  • The value of CBoxLimit.SelectedIndex before the if statement is executed is 0... I have tried setting it to 1 or nothing at all but nothing hapened... – salkow Dec 11 '17 at 15:00

1 Answers1

0

It seems that you should change the SelectedIndex on your instance of ComboBox and not on CBoxLimit. Also you should set the Form's KeyPreview property to True (see here):

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.E)
            {
                comboBox1.SelectedIndex = 1;
            }
        }
Amittai Shapira
  • 3,749
  • 1
  • 30
  • 54
  • I am sorry but i tried and i had the same result... Note that when i have the combobox open and i just hover the mouse over the desired index it just works... But only when i hover on it... Idk why... – salkow Dec 11 '17 at 14:49
  • 1
    Can you please add more details? about your code? the WinForms designer? anything that can clarify what is the issue will help... – Amittai Shapira Dec 11 '17 at 14:51
  • Changing the SelectedIndex just affects some labels in the form and some values inside the code... Nothing more... There is nothing other that affects the SelectedIndex inside the code... I am starting to think that i have found a bug inside Visual Studio... – salkow Dec 11 '17 at 14:58
  • I'll find it very hard to believe, as this is very common scenario... see my updated answer, it works great (using VS 2012) – Amittai Shapira Dec 11 '17 at 15:03
  • A short-cut should be able to activate even if the combo box doesn't have focus. So handling key event of the combo box is not a good idea. – Reza Aghaei Dec 11 '17 at 15:20
  • Right - it looks like the problem was not accessing the actual instance of the ComboBox... – Amittai Shapira Dec 11 '17 at 15:26