2014-12-04 39 views
0

1.當我從列表框1中選擇一個項目時,它顯示有關列表框2中項目的信息。但是,當我點擊listbox1中的一個項目時,它會向我顯示信息並在列表框2中將它添加到它的末尾。我如何擺脫0,或者我在做什麼導致這個?我認爲它可能顯示索引號。這裏是我的代碼 -0當列表框1中選擇的項目時顯示0顯示

If ListBox1.SelectedIndex = 2 Then ListBox2.Items.Add("60137" & ListBox2.Items.Add("60138")) 

2.Also,我怎麼會清除列表2當我選擇列表1中不同的項目,使他們不都在同一時間填充列表2?

+0

你爲什麼使用ListBox作爲rela特德項目?列表框用於顯示用戶可以從中選擇的多個項目。它看起來像一個標籤將是一個更好的選擇。 – 2014-12-04 03:36:54

回答

1

*忽略了一個事實,這是一個可怕的設計...

變化:

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 
+0

我知道這很粗糙。你會用什麼來代替這個?它仍然增加了一個0例如「601370」我願意學習:)謝謝你。 – King96 2014-12-04 03:47:25

相關問題