Selecting from a Combo Box

I have a combo box with 5 pre-defined items. When I execute my code, no matter which item I select from the drop-down, the code always executes the first selection.

cbxLength contains the values 10, 15, 20, 25, 30 which represents the # of years, and I multiple their selection by 12 to calculate the number of months. Why is the code always selection (0) as the index

Thanks for any help you can provide.

If cbxLength.Items(0) Then

NumberPayment = 10 * 12

ElseIf cbxLength.Items(1) Then

NumberPayment = 15 * 12

ElseIf cbxLength.Items(2) Then

NumberPayment = 20 * 12

ElseIf cbxLength.Items(3) Then

NumberPayment = 25 * 12

ElseIf cbxLength.Items(4) Then

NumberPayment = 30 * 12

End If

txtNumberPmt.Text = NumberPayment.ToString

MessageBox.Show("Number Payments = " + txtNumberPmt.Text)



Answer this question

Selecting from a Combo Box

  • craigw33333

    Hello,

    Here is a link to work with combobox in VB.NET

    http://www.startvbdotnet.com/controls/combo.aspx


    Regards

    Kuldeep Deokule


  • Luc Morin

    To see what item is selected you should use the SelectedItem property.

    NumberPayment = cbxLength.SelectedItem * 12

    You should be sure that it actually is something selected by only accessing the SelectedItem property inside a block that has checked that it is not nothing.

    If Not cbxLength.SelectedItem Is Nothing Then
    NumberPayment = cbxLength.SelectedItem * 12
    End If




  • DongGyoun

    Excellent advice ... Thank you.
  • Selecting from a Combo Box