要在Visual Basic中展示国家和城市的项目,你可以使用以下解决方案:
Public Class Country
Public Property Name As String
Public Property Population As Integer
End Class
Public Class City
Public Property Name As String
Public Property Country As Country
End Class
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim country1 As New Country()
country1.Name = "China"
country1.Population = 1400000000
Dim country2 As New Country()
country2.Name = "USA"
country2.Population = 330000000
Dim city1 As New City()
city1.Name = "Beijing"
city1.Country = country1
Dim city2 As New City()
city2.Name = "New York"
city2.Country = country2
ListBox1.Items.Add(country1.Name)
ListBox1.Items.Add(country2.Name)
ListBox2.Items.Add(city1.Name)
ListBox2.Items.Add(city2.Name)
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
ListBox2.Items.Clear()
If ListBox1.SelectedIndex = 0 Then
ListBox2.Items.Add("Beijing")
ElseIf ListBox1.SelectedIndex = 1 Then
ListBox2.Items.Add("New York")
End If
End Sub
这样,当用户选择一个国家时,城市列表框将显示属于该国家的城市。你可以根据实际需求进行修改和扩展。