2

I'm developing a software and I have a little trouble. I have two combobox, the first one fills the second one but if the first combobox has a value which is equal than -1, then the second combobox is not enable. This is a peace of my code:

 private void FillComboGroup()
    {
        try
        {
             //here I initialize an object

            if (Convert.ToInt32(this.cboSede.SelectedValue) > -1)
            {
               //the code here just retrieves an object
            }
            else
            {
                this.cboEmpleado.Enabled = false;
                this.cboEmpleado.BackColor = Color.White;
            }
        }
        catch (Exception ex)
        {
            //here are some methods to save the exception in the log
        }

Well, the first time works without problems. When the form load for first time, the second combobox is not enabled, then I change the first combobox value and the second combo is enabled now. But if I put again the first combobox in the first value("Please select an option...", which its value is -1) then the second combo is enabled. I tried with the SelectedIndexChanged and SelectionChangeCommited but doesnt work. Any idea about how to solve it? Thanks in advance.

private void cboSede_SelectedIndexChanged(object sender, EventArgs e)
    {

            this.FillComboGroup();
    }
LordCommanDev
  • 922
  • 12
  • 38
  • 1
    Use the [SelectedIndex](https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex(v=vs.110).aspx) property of the ComboBox instead of `SelectedValue` to compare to -1. – Equalsk Nov 17 '15 at 17:14
  • 1
    "Please select an option..." Is **not** index -1 if its inside your `combobox` list. I would use `combobox1.ResetText();` After your operation, this will actually set your index at position -1 – Just Do It Nov 17 '15 at 17:46

2 Answers2

4

You're using SelectedValue instead of SelectedIndex when checking the index.

It should look like this:

private void FillComboGroup()
{
    try
    {
        if (cboSede.SelectedIndex > 0)
        {
           //the code here just retrieves an object

            // YOU'LL PROBABLY WANT TO ENABLE THE SECOND COMBO AGAIN HERE
        }
        else
        {
            cboEmpleado.Enabled = false;
            cboEmpleado.BackColor = Color.White;
        }
    }
    catch (Exception ex)
    {
        //here are some methods to save the exception in the log
    }
}
Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • 1
    I done what you say but I still have the same problem. I put a break point to compare the selected index with -1 and there is no problem when the form load for first time but in the second try, when the selected index is -1, jump to the else block and it supposed that the combobox it has to be disabled but nothing happen – LordCommanDev Nov 17 '15 at 17:41
  • *But if I put again the first combobox in the first value* This is telling us that OP is in index `0` not `-1` reason for which this answer won't be of much help, a simple edit can transform it into a correct answer(for this specific problem) – Just Do It Nov 17 '15 at 17:56
  • 1
    Thanks for the heads up. Have edited the answer to use 0 based index instead. – Equalsk Nov 17 '15 at 18:37
1

Your problem lies in the fact that the first object in a combobox's id is 0 not -1, -1 is the unselected value you should try something like this :

private void FillComboGroup()
{
    try
    {
        if (cboSede.SelectedIndex > 0)
        {
           //the code here just retrieves an object
        }
        else
        {
            cboEmpleado.Enabled = false;
            cboEmpleado.BackColor = Color.White;
        }
    }
    catch (Exception ex)
    {
        //here are some methods to save the exception in the log
    }
}
Le Woogush
  • 375
  • 2
  • 16