The ComboBox and Selecting Items

Excel Chart Project: For C# and VB NET Students


Just a short lesson, this one. We need to get which item was selected in the combo box. We can then use this to get the correct chart data for a staff member.

Go back to the Form's Design View. Click on your Combo Box to select it. In the properties area on the right of Visual Studio, click the lightning symbol to display a list of events. Locate the SelectedIndexChanged event

The SelectedIndexChanged event highlighted in the Properties area of Visual Studio

Double click to create a code stub.
There's only one line of code we need to add. Before doing so, add this line near the top of your code, just under the arrayData line:


int getRowNumber;


Dim getRowNumber As Integer

We'll need this variable in the button code, which is why it's at the top.

To get which item was selected, add this line in C#:

getRowNumber = cmbStaff.SelectedIndex;

And this in VB:

getRowNumber = cmbStaff.SelectedIndex

Your code should look like this in C#:

C# code showing the SelectedIndexChanged event in use

And this in Visual Basic .Net:

VB Net code showing the SelectedIndexChanged event being used

Every time a member of the sales staff is selected, the SelectedIndexChanged event fires. You can use SelectedIndex to get which items was selected from your ComboBox. The first item on your list will be Index 0, the second is Index 1, etc.

That's all we need for the combo box code. We can start on the button now. We'll do that in the next lesson.

Coding to Get the Chart >>

Back to the C# NET Contents Page

Back to the VB NET Contents Page