0

I have to set different values of a variable through the selection of a Combobox. But even if the clause reported in the Combobox is selected, the value is 0. Here is what I wrote:

In the form where the Combobox is located:

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ComboBox1.SelectedItem = "Ultra-fast"
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
End Sub

In the form where I have to set the value of the variable (calling the Combobox):

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim growth as Integer
If Form2.ComboBox1.SelectedItem = "Ultra-fast" Then
            growth = 75
ElseIf Form2.ComboBox1.SelectedItem = "Fast" Then
            growth = 150
ElseIf Form2.ComboBox1.SelectedItem = "Medium" Then
            growth = 300
ElseIf Form2.ComboBox1.SelectedItem = "Slow" Then
            growth = 600
End If
End Sub

Where am I doing wrong? I've also checked that the form where the Combobox is opened and it remains active when I call the Combobox. I've setted a label to write the value of "growth" but it always remains equal to zero. Thanks all are gonna answer me. Best regards.

  • This is not likely to be related to your issue by why would you be setting the `DropDownStyle` in the `SelectedIndexChanged` event handler? Why would you not set it in the designer? – jmcilhinney Nov 30 '20 at 13:05
  • How EXACTLY are you displaying `Form2`? – jmcilhinney Nov 30 '20 at 13:06
  • Probably you are asking to do the change to the wrong form instance. See https://stackoverflow.com/questions/4698538/why-is-there-a-default-instance-of-every-form-in-vb-net-but-not-in-c – Steve Nov 30 '20 at 13:06
  • @jmcilhinney thanks for commenting. I'm not a VB.net expert programmer, so I found something on the internet and I've found that command to block editing from user. I didn't know that this property could be set in the designer. – telemaco10399 Nov 30 '20 at 13:07
  • @jmcilhinney Could you explain me what "display" means? Form2 is the screen from which I'm importing data to create graphs but when I recall the Combobox2, Form2 remains open – telemaco10399 Nov 30 '20 at 13:09
  • Please turn on Option Strict. This is a 2 part process. First for the current project - In Solution Explorer double click My Project. Choose Compile on the left. In the Option Strict drop-down select ON. Second for future projects - Go to the Tools Menu -> Options -> Projects and Solutions -> VB Defaults. In the Option Strict drop-down select ON. This will save you from bugs at runtime. You will see that `.SelectedItem` is an `Object`. – Mary Nov 30 '20 at 18:48

1 Answers1

0

On Form2 fill your combo with KeyValue pairs. Then set the display and value members. The data source is the actual items in the combo.

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ComboBox1.Items.Add(New KeyValuePair(Of String, Integer)("Ultra-fast", 75))
    ComboBox1.Items.Add(New KeyValuePair(Of String, Integer)("Fast", 150))
    ComboBox1.Items.Add(New KeyValuePair(Of String, Integer)("Medium", 300))
    ComboBox1.Items.Add(New KeyValuePair(Of String, Integer)("Slow", 600))

    ComboBox1.DisplayMember = "Key"
    ComboBox1.ValueMember = "Value"
    ComboBox1.DataSource = ComboBox1.Items
End Sub

In Form1 retrieve the select value. Remember this returns an Object but you can cast it back to its underlying type.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim growth = CInt(Form2.ComboBox1.SelectedValue)
    MessageBox.Show(growth.ToString)
End Sub
Mary
  • 14,926
  • 3
  • 18
  • 27