question

WilfredEghenedji-9495 avatar image
0 Votes"
WilfredEghenedji-9495 asked WilfredEghenedji-9495 commented

Combobox (Items.Add), not working...

The following seems well-coded, but don't why it didn't work. The project runs successfully, yet no item displays, clicking the combobox dropdown arrow. Please, help check the problem. Thank you very much!


 Public Class Form1
            
 Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    ComboBox1.Items.Add("Books")
    ComboBox1.Items.Add("PCs")
    ComboBox1.Items.Add("Countries")
 End Sub
            
 Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
    If ComboBox1.SelectedItem = "Books" Then
       ComboBox2.Items.Add("Textbooks")
       ComboBox2.Items.Add("Note-books")
       ComboBox2.Items.Add("Drawing books")
    Else
          If ComboBox1.SelectedItem = "PCs" Then
             ComboBox2.Items.Add("HP")
             ComboBox2.Items.Add("DELL")
             ComboBox2.Items.Add("Lenovo")
          Else
             If ComboBox1.SelectedItem = "Countries" Then
                ComboBox2.Items.Add("USA")
                ComboBox2.Items.Add("Nigeria")
                ComboBox2.Items.Add("Ukrain")
          End If
       End If
    End if
 End Sub
                  
 End Class

ATTEMPTS

  1. I have attempted changing the SelectedIndexChanged event to DisplayMemberChanged

  2. And also, Clear comboboxs before Adding Items,...

but, all to no avail.








dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LesHay-2099 avatar image
0 Votes"
LesHay-2099 answered WilfredEghenedji-9495 commented

Hi

Here is one way to do what I think you are wanting. This version is bare bones but shows that very little code is needed to achieve it. Give it a try as a stand alone example.

 Public Class Form1
     Dim dic As New Dictionary(Of String, String())
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         With dic
             .Add("Books", {"Textbooks", "Note-books", "Drawing books"})
             .Add("PC2", {"HP", "Dell", "Lenovo"})
             .Add("Countries", {"UK", "USA", "Nigeria", "Ukraine"})
         End With
         ComboBox1.Items.AddRange(dic.Keys.ToArray)
     End Sub
     Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
         ComboBox2.Items.Clear()
         ComboBox2.Items.AddRange(dic(ComboBox1.SelectedItem.ToString))
     End Sub
     Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
         Dim s As String = ComboBox1.SelectedItem.ToString
         Dim s2 As String = ComboBox2.SelectedItem.ToString
         MessageBox.Show("Tou have selected: " & s & " - " & s2)
     End Sub
 End Class
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I decided to use the click event, and it seems to work. But then, I feel my solution is not as much reliable and standard as yours. Hence, am using yours. See my solution below:

 Private Sub ComboBox2_Click(sender As Object, e As EventArgs) Handles ComboBox2.Click
  If ComboBox1.SelectedItem = "Books" Then
    ComboBox2.Items.Clear()
    ComboBox2.Items.Add("Textbooks")
    ComboBox2.Items.Add("Note-books")
    ComboBox2.Items.Add("Drawing books")
    Else
       If ComboBox1.SelectedItem = "PCs" Then
          ComboBox2.Items.Clear()
          ComboBox2.Items.Add("HP")
          ComboBox2.Items.Add("DELL")
          ComboBox2.Items.Add("Lenovo")
             Else
                If ComboBox1.SelectedItem = "Countries" Then
                   ComboBox2.Items.Clear()
                   ComboBox2.Items.Add("USA")
                   ComboBox2.Items.Add("Nigeria")
                   ComboBox2.Items.Add("Ukrain")
              End If
       End If
 End If
 End Sub
    
 Private Sub ComboBox1_Click(sender As Object, e As EventArgs) Handles ComboBox1.Click
    If ComboBox2.Text.Length > 1 Then
       ComboBox2.Text = ""
    End If
    ComboBox1.Items.Clear()
    ComboBox1.Items.Add("Books")
    ComboBox1.Items.Add("PCs")
    ComboBox1.Items.Add("Countries")
 End Sub



0 Votes 0 ·
DewayneBasnett-7583 avatar image
0 Votes"
DewayneBasnett-7583 answered DewayneBasnett-7583 edited

If there aren't items in the ComboBoxes when the app starts then the events can never fire.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LesHay-2099 avatar image
0 Votes"
LesHay-2099 answered

Hi

There is something wrong with the logic you are using.
The first Sub, where ComboBox1 SelectedIndexChanged is invoked works as coded and adds the 3 items to ComboBox1.
The second Sub, where ComboBox2 SelectedIndexChanged is invoked works as coded and adds the items to ComboBox2.

BUT..................

These Subs are only invoked if/when they already have initial values in them.

AND ...............

In ComboBox2, an item needs to be selected before adding the expected items - in other words, the logic is incorrect.

Try this code and you will see what I mean.

 Public Class Form1
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         ComboBox1.Items.Add("A")
         ComboBox1.Items.Add("B")
         ComboBox1.Items.Add("C")
         ComboBox1.Items.Add("E")
         ComboBox1.Items.Add("F")
    
         ComboBox2.Items.Add("AA")
         ComboBox2.Items.Add("BB")
         ComboBox2.Items.Add("CC")
         ComboBox2.Items.Add("EE")
         ComboBox2.Items.Add("FF")
    
     End Sub
     Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
         ComboBox1.Items.Add("Books")
         ComboBox1.Items.Add("PCs")
         ComboBox1.Items.Add("Countries")
     End Sub
    
     Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
         If ComboBox1.SelectedItem = "Books" Then
             ComboBox2.Items.Add("Textbooks")
             ComboBox2.Items.Add("Note-books")
             ComboBox2.Items.Add("Drawing books")
         Else
             If ComboBox1.SelectedItem = "PCs" Then
                 ComboBox2.Items.Add("HP")
                 ComboBox2.Items.Add("DELL")
                 ComboBox2.Items.Add("Lenovo")
             Else
                 If ComboBox1.SelectedItem = "Countries" Then
                     ComboBox2.Items.Add("USA")
                     ComboBox2.Items.Add("Nigeria")
                     ComboBox2.Items.Add("Ukrain")
                 End If
             End If
         End If
     End Sub
 End Class


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.