I have some code that checks a value in a combo box before it executes but it does not appear to be seeing the value selected in the combo box until I select the item in the combo box for a second time. Note the value in the combo box was set in a form and saved to a database. Now if I open that form again in pulls up the value previously set in the combo box from the database but it is as if it is not seen by the case statement unless I reselect the value.Im using a case statement when determining what to do based on the values in the combo box.

My code looks like this:

private void optypCB_Click(object sender, EventArgs e)
        {
            switch (optypCB.Text)
            {
                case "Draw":
                    wearTB.Text = "4";
                    pinsTB.Text = "0";
                    nitrogenLowerCheckBox.Checked = true;
                    densityTB.Text = Convert.ToString(.35); break;
                case "Trim":
                    wearTB.Text = Convert.ToString(4);
                    pinsTB.Text = Convert.ToString(4);
                    densityTB.Text = Convert.ToString(.35); break;
                case "Form":
                    wearTB.Text = Convert.ToString(4);
                    pinsTB.Text = Convert.ToString(4);
                    densityTB.Text = Convert.ToString(.35); break;
                case "Pierce":
                    wearTB.Text = Convert.ToString(4);
                    pinsTB.Text = Convert.ToString(4);
                    densityTB.Text = Convert.ToString(.35); break;
                case "Blank":
                    wearTB.Text = Convert.ToString(4);
                    pinsTB.Text = Convert.ToString(4);
                    densityTB.Text = Convert.ToString(.45); break;                    
                default:
                    throw new Exception("Unknown selection");
            }
        }

Dani AI

Generated

Short diagnosis: the combo box Click handler is not the right place for logic that must run when a saved selection is restored. Click fires only on a user mouse click. Use the selection events instead and ensure the saved text actually becomes a selected item (not just the Text).

Move the UI-update logic out of the Click handler into a small helper method (for example, UpdateFieldsForOperation) and invoke that from the appropriate event. For user changes use SelectionChangeCommitted; for both programmatic and user changes use SelectedIndexChanged. If the control is data-bound, use SelectedValue/SelectedValueChanged. See ComboBox.SelectedIndexChanged and ComboBox.SelectionChangeCommitted.

When restoring the saved value from the database, set the combo’s selected item explicitly so the selection events behave as expected. Example pattern:

int idx = optypCB.FindStringExact(savedValue);
if (idx >= 0)
  optypCB.SelectedIndex = idx;   // will fire SelectedIndexChanged
else
  /* handle missing item or default */

(FindStringExact documentation: ComboBox.FindStringExact.)

Troubleshooting tips tied to ’s suggestion: log or append the firing of SelectedIndexChanged, SelectionChangeCommitted, Click, DropDownClosed and TextChanged to a ListBox or multiline TextBox to confirm order. Also verify items are populated before setting SelectedIndex, and guard for SelectedIndex == -1 to avoid unexpected behavior. This will explain why ’s saved Text appears but the original Click handler did not react until a second selection.

Recommended Answers

All 9 Replies

thats coz you're using the wrong event

Im sorry but I don't understand what event should I use?

Why not find out what order they fire in, and when. take a text box, and a combo box, and assign an append line to the text box for each event in the combo box.

(you will need a multiline text box)

Liz im sorry but im not very experienced. I know how to create a multiline text box but how do I assing an append line for each event in the combo box? Thanks in advance.

have a try first..

have a try first..

I really have tried different things and searching the internet. But I can't figure out how to assign the events to a multiline text box.

show me

I really have tried different things and searching the internet. But I can't figure out how to assign the events to a multiline text box.

Events for the Textbox control1:

void textBox1_LostFocus(object sender, System.EventArgs e)

{

listBox1.Items.Add("LostFocus event called of TextBox1");

}

void textBox1_GotFocus(object sender, System.EventArgs e)

{

listBox1.Items.Add("GotFocus event called of TextBox1");

}

void textBox1_Leave(object sender, System.EventArgs e)

{

listBox1.Items.Add("Leave event called of TextBox1");

}

void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)

{

listBox1.Items.Add("KeyUp event called of TextBox1");

}

void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

{

listBox1.Items.Add("KeyPress event called of TextBox1");

}

void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)

{

listBox1.Items.Add("KeyDown event called of TextBox1");

}

void textBox1_Enter(object sender, System.EventArgs e)

{

listBox1.Items.Add("Enter event called of TextBox1");

}

other than no code tags, using a listbox would work.
So, now you should see what events are triggered when, and know the reason for your first problem..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.