2015-11-26 61 views
0

所以,我在VB.net做一個控制檯。我決定以Lua類型的方式使用子串方式。但是,在執行此操作時,如果鍵入的內容不是命令,則會崩潰並顯示以下錯誤:「其他信息:索引和長度必須引用字符串中的位置。」命令problom VB

Public Class Form1 
    Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown 
     Dim cmd As String = TextBox2.Text 
     Dim l As Integer = Len(TextBox2.Text) 
     Dim param As String 
     If e.KeyCode = Keys.Enter Then 
      '0 Param command 
      If cmd = "script" Then 
       Script.Show() 
       TextBox2.Text = "" 

       'Trying to avoide error "Additional information: Index and length must refer to a location within the string." 
      ElseIf Not cmd.Substring(0, 5) = "echo;" Or Not cmd.Substring(0, 9) = "download;" Then 
       Return 

       '1 Param cmd 
      ElseIf cmd.Substring(0, 5) = "echo;" Then 
       param = cmd.Substring(5) 
       Me.TextBox1.Text = Me.TextBox1.Text + Environment.NewLine + param 
       TextBox2.Text = "" 

       '2 Param cmd 
      ElseIf cmd.Substring(0, 9) = "download;" Then 
       param = cmd.Substring(9) 
       Me.TextBox1.Text = Me.TextBox1.Text + Environment.NewLine + param.Substring(0, param.IndexOf(";"c)) + param.Substring(param.IndexOf(";"c) + 1) 
       TextBox2.Text = "" 

      End If 
     End If 
    End Sub 
End Class 

回答

1

您不能嘗試從不在字符串內的位置獲取子字符串。
然而,而不是試圖讓你可以使用一個簡單的方法也避免了在輸入文本

If cmd = "script" Then 
     ..... 
    ElseIf cmd.StartsWith("echo;") Then 
     ..... 
    ElseIf cmd.StartsWith("download;") Then 
     .... 
    Else 
     Return 
    End If 
+0

謝謝您的幫助首先檢查子。 –

+0

很高興能有所幫助。看到你的歷史,我建議,如果你願意,閱讀[如何接受答案的作品](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Steve