2014-04-16 31 views
0

製作基於文本的遊戲。我有一個命令提示符,它由richtextbox作爲outputbox和 用於inputtextbox的文本框。我需要做一些像「cls」「dir」「config」這樣的命令。我的列表中有很多更多的命令。我只是堅持如何以及如何解決方案。 這裏是我的代碼,我用選擇案例的方法對其中的一些'ty'進行了分類,但它太原始了。如何創建我的命令,除了select case方法vb.net

Private Sub Output(s As String) 
    If s <> "" Then 
     nCounter = nCounter + 1 
     sCounter = Convert.ToString(nCounter) 
     consoleoutputbox.AppendText(vbCrLf & sCounter & " " & s) 
    End If 
End Sub 
Private Sub consoleinputbox_KeyDown(sender As Object, e As KeyEventArgs) Handles consoleinputbox.KeyDown 
    Dim Command As String = consoleinputbox.Text 
    If e.KeyCode = Keys.Enter Then 
     If Command <> "" Then 
      Select Case Command 
       Case "cls" 
        consoleoutputbox.Clear() 
        consoleinputbox.Clear() 
        consoleinputbox.Focus() 
        nCounter = 0 
       Case "help" 
        Output("Welcome to help section. Avaliable commands:") 
        Output("help, cls") 
       Case Else 
        Output(Command) 
        consoleinputbox.Clear() 
        consoleinputbox.Focus() 
      End Select 
     End If 
    End If 
End Sub 
+1

你還在尋找什麼?在一天結束時,它將最終像這樣或每個命令在它自己的子然後案件選擇子,無論哪種方式的代碼必須寫,所以它知道該怎麼辦 – bensonsearch

回答

0

也許Dictionary(Of String, Action)會幫助。把每一件事情()你想完成的成子例程和它們與命令的鍵添加到字典:

Dim Commands As New Dictionary(Of String, Action) 
Commands.Add("test1", New Action(AddressOf test)) 

然後,只需將字符串傳遞到字典作爲索引並調用子程序

Commands("test1").Invoke() 
+0

謝謝tinstaafl我不知道字典我會盡快開始研究它。如果我理解正確,我可以添加命令字典,然後我可以運行它們與調用權? – Corviuse

+0

是的,根據我的例子 – tinstaafl

0

一個選項可能是定義一個模塊並添加以命令命名的方法。然後,您可以使用Reflection來查找和調用用戶輸入的名稱作爲命令的方法。