2015-02-06 59 views
0

在我目前的項目中,我正在製作一款紙牌遊戲。基本上它是如何工作的是我有一個名爲「lstdeck」的列表框。這是你找到所有卡片的地方。第二個被稱爲「lsthand」。這是您的手牌,當5張牌從「背板」中取出並放入您的「手牌」時創建。需要幫助顯示不同列表框中的相同列表框項目信息vb.net

當從列表框(甲板或手)中選擇一張卡時,卡信息應顯示在「txtdescription」中。但我不知道如何讓多個listbox.selecteditem調用相同的布爾值。這是我希望能夠工作的片段,但是失敗了。

Dim CurrentList as string 
Private Sub lsthand_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lsthand.SelectedIndexChanged 
    Currentlist = "lsthand" 
    CheckCards() 
End Sub 

Private Sub CheckCards() 
    Dim FindSelection As String = ".selecteditem" 
    If Currentlist & FindSelection = "Face Card 1" Then 
     txtdescription.text = "This is the first face card." 

在上面的例子中,名爲「Face Card 1」的卡片(顯示在列表框中)被選中。不管它在哪個列表框中。爲了添加兩個字符串,一個是「lsthand」和一個「.selecteditem」,結果是:「lsthand.selecteditem」。不幸的是,這不起作用。有人知道這個解決方案嗎?

此外,我意識到我可以做一個「lstdeck.selecteditem」Private Sub來檢查列表框,還有一個用於「lsthand」。但是我使用了大約180張不同的卡片和6個列表框。任何幫助將不勝感激!

+0

我首先想到的:你是如何處理的180卡? – Hockenberry 2015-02-06 07:57:24

回答

0

只要做一個處理所有SelectedIndexChanged的子。

我想你想somethine這樣的:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    ListBox1.Items.AddRange(New String() {"dog", "cat", "rabbit", "tux"}) 
    ListBox2.Items.AddRange(New String() {"earth", "fire", "water", "air"}) 
    ListBox3.Items.AddRange(New String() {"Green Day", "Limp Bizkit", "Donots", "Guano Apes"}) 
End Sub 

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged, ListBox2.SelectedIndexChanged, ListBox3.SelectedIndexChanged 
    If DirectCast(sender, ListBox).SelectedItem = "cat" Then 
     MsgBox("best animal!") 
    Else 
     MsgBox("something else...") 
    End If 
End Sub 
相關問題