VB.NET: Using String to compare with the Combo Box

toytoy

Well-known member
Joined
Jul 16, 2004
Messages
46
Programming Experience
1-3
Hi...

Say i have this string call "data" in Form1, this string contains number "5" value....

how do i pass this string to the Form2 and compare with the combo box items...

The combo box DropDownStyle is set to DropDownList...
which means when i pass, the SelectedIndex must be subtract or add by 1 to get the correct items to display...

The Combo box items contain:
One, Two, Three, Four and Five...

How to i actually pass this string to Form2 and Form2 will compare the number 5 with the items and display Five instead in the combo box correctly..

Thanks
 
Last edited:
To give a clearer picture:

Form2 will be open only after i click on another button in Form1..

data "5" is already in the string and is waiting to be passed to Form2....

The "5" will be comapre accordingly with the items in one of the combobox in Form2.....

then the combox box in Form2 will display accordingly to the compare items...In this case which is the "Five" in the items instead of "5"..

That means the combo box will be able to check the items with the string and display accordingly...

Thanks
 
If the comboBox items always contains One, Two, Three, Four and Five... (ie starts with 1 and increments by 1, in order) then the selectedIndex will be the number minus 1.

As always with .NET. there are many ways to accomplish this.
One would be to declare a Public procedure that accepts an integer to set the selectedIndex of the comboBox:

VB.NET:
'in Form2:
Public Sub SetComboIndex(ByVal int As Integer)
    Try
        ComboBox1.SelectedIndex = int - 1
    Catch
        'error code
        MessageBox.Show("Value out of range")
    End Try
End Sub

'in Form1 (I have a textBox named TextBox1 to send the number):
Private frm2 As Form2
Private Sub cmdOpenComboBoxForm_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles cmdOpenComboBoxForm.Click
    If frm2 Is Nothing OrElse frm2.IsDisposed Then
        frm2 = New Form2()
    End If
    If IsNumeric(TextBox1.Text) Then
        frm2.SetComboIndex(CInt(TextBox1.Text))
        frm2.Show()
    Else
        MessageBox.Show("Value must be a number")
    End If
End Sub
 
Back
Top