Ken Ken - 4 months ago 30
SQL Question

how to handle end of a comboBox

I have a comboBox in my winforms application that populates based on data from my SQL Express Database, it increments by one each time the user clicks the "next record" button. Which has this simple code that does the operation:

comboBox4.SelectedIndex = comboBox4.SelectedIndex + 1;

How would I handle the exception when it has reached the end of the comboBox? Because when it reaches the end there is no value to go to next after the code above runs. How would I let's say have a MessageBox pop up that says "you have reached the end of your comboBox?"


You can check with Items.Count of ComboBox before incrementing the SelectedIndex.

if (comboBox1.SelectedIndex < comboBox1.Items.Count - 1)
    comboBox1.SelectedIndex += 1;