*忽略了一個事實,這是一個可怕的設計...
變化:
If ListBox1.SelectedIndex = 2 Then ListBox2.Items.Add("60137" & ListBox2.Items.Add("60138"))
要:
If ListBox1.SelectedIndex = 2 Then
ListBox2.Items.Clear
ListBox2.Items.Add("60137")
End If
這是一種替代方法:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim petA As New Pet
petA.Name = "Puss in Boots"
petA.Species = "Cat"
ListBox1.Items.Add(petA)
Dim petB As New Pet
petB.Name = "Nemo"
petB.Species = "Fish"
ListBox1.Items.Add(petB)
Dim petC As New Pet
petC.Name = "Rango"
petC.Species = "Lizard"
ListBox1.Items.Add(petC)
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex <> -1 Then
Dim P As Pet = DirectCast(ListBox1.SelectedItem, Pet)
Label1.Text = P.Species
End If
End Sub
End Class
Public Class Pet
Public Name As String
Public Species As String
Public Overrides Function ToString() As String
Return Name
End Function
End Class
你爲什麼使用ListBox作爲rela特德項目?列表框用於顯示用戶可以從中選擇的多個項目。它看起來像一個標籤將是一個更好的選擇。 – 2014-12-04 03:36:54