2013-05-05 63 views
0

現在我正在構建一種腳本語言(EngineScript)。目前您只能輸入一個命令/功能(IE:sout "Hello world"),但我想擴展它。如何在VB.Net中換行後得到文本框的字符

所以我想知道你們是否知道任何代碼,以便在VB2012.Net中將換行後的字符輸入到單獨的變量中。感謝:-)

這裏是我的代碼:

Public Class Form1 

    Dim strvar0 As String 
    Dim strvar1 As String 
    Dim strvar2 As String 
    Dim strvar3 As String 
    Dim numvar As Integer 


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

     Dim first4Chars As String = TextBox1.Text.Substring(0, 4) 
     Dim restOfChars As String = TextBox1.Text.ToString.Substring(5) 

     Select Case first4Chars 
      Case "sout" 
       Select Case restOfChars 
        Case "box:str0" 
         TextBox2.Text = strvar0 & vbCrLf & vbCrLf & vbCrLf & vbCrLf & "COMMAND //sout\\ has been successfully used" 
        Case Else 
         TextBox2.Text = restOfChars & vbCrLf & vbCrLf & vbCrLf & vbCrLf & "COMMAND //sout\\ has been successfully used" 
       End Select 

      Case "str0" 
       strvar0 = restOfChars 

      Case Else 
       TextBox2.Text = "Error".ToString 

     End Select 

    End Sub 



    Private Sub HelpToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HelpToolStripMenuItem.Click 
     Dim a As New Help 
    End Sub 

    Private Sub RunToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RunToolStripMenuItem.Click 
     Button1.PerformClick() 
    End Sub 

End Class 

...我做的,這是一個Windows窗體

@Jamby當我用你的代碼:

Public Class Form1 
    Dim strvar0 As String 
    Dim strvar1 As String 
    Dim strvar2 As String 
    Dim strvar3 As String 
    Dim numvar As Integer 



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     For Each Line In TextBox1.Text.Split(vbNewLine) 

      Dim first4Chars As String = TextBox1.Text.Substring(0, 4) 
      Dim restOfChars As String = TextBox1.Text.ToString.Substring(5) 

      Select Case first4Chars 
       Case "sout" 
        Select Case restOfChars 
         Case "box:str0" 
          TextBox2.Text = strvar0 & vbCrLf & vbCrLf & vbCrLf & vbCrLf & "COMMAND //sout\\ has been successfully used" 
         Case Else 
          TextBox2.Text = restOfChars & vbCrLf & vbCrLf & vbCrLf & vbCrLf & "COMMAND //sout\\ has been successfully used" 
        End Select 

       Case "str0" 
        strvar0 = restOfChars 


       Case "exit" 
        Me.Close() 

       Case "lofi" 

        Select Case restOfChars 
         Case "box:str0" 
          Process.Start(strvar0) 
         Case Else 
          Process.Start(restOfChars) 
        End Select 


       Case "inet" 
        Process.Start("iexplore.exe" + restOfChars) 
       Case Else 
        TextBox2.Text = "Error".ToString 


      End Select 
     Next 

     TextBox1.Clear() 

    End Sub 



    Private Sub HelpToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HelpToolStripMenuItem.Click 
     Dim a As New Help 
    End Sub 

    Private Sub RunToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RunToolStripMenuItem.Click 
     Button1.PerformClick() 


    End Sub 

    Private Sub MenuStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked 

    End Sub 
End Class 

它不會產生錯誤,但當我輸入

sout Hello 換行符 sout World

以下應該從我的輸出窗口輸出:

Hello 



COMMAND //sout\\ has been successfully used 
World 




COMMAND //sout\\ has been successfully used 

但不是這種情況:

Hello 
sout World 



COMMAND //sout\\ has been successfully used 

幫助!

+0

您錯誤地複製了我的代碼。看看first4Chars As String = Line.Substring(0,4) – Jamby 2013-05-08 07:23:39

回答

0

我認爲你想使用的是你的表單中的多行文本框(對於多行文本框有一個屬性/複選框)。

然後,您可以從文本框中獲取字符串,並在其上執行split('\n');。 (在VB語法當然)

這將返回一個字符串數組,這是你的線。

分裂看起來像代碼:(當然,在C#反正)

string contents = MyTextBox.Text; 
string[] lines = contents.Split('\n'); // or Environment.NewLine instead of '\n' 
+0

我會試試這個並回復給你 – 2013-05-05 04:29:41

+0

我不確定如何去編碼它......我會更新我的問題 – 2013-05-05 04:34:19

+1

'stringVariable.Split Environment.NewLine)' – Origin 2013-05-05 06:07:37

0

您可以用這種方法改變你的代碼:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    For Each Line in TextBox1.Text.Split(vbNewLine) 
     Dim first4Chars As String = Line.Substring(0, 4) 
     Dim restOfChars As String = Line.Substring(5) 

     Select Case first4Chars 
      Case "sout" 
       Select Case restOfChars 
        Case "box:str0" 
         TextBox2.Text = strvar0 & vbCrLf & vbCrLf & vbCrLf & vbCrLf & "COMMAND //sout\\ has been successfully used" 
        Case Else 
         TextBox2.Text = restOfChars & vbCrLf & vbCrLf & vbCrLf & vbCrLf & "COMMAND //sout\\ has been successfully used" 
       End Select 
      Case "str0" 
       strvar0 = restOfChars 
      Case Else 
       TextBox2.Text = "Error".ToString 
     End Select 
    Next 
End Sub 

您也應該檢查Line.Lenght到大於5,否則你會得到一個ArgumentOutOfRangeException。

+0

此代碼不起作用??? – 2013-05-06 06:47:46

+0

你是什麼意思與「不工作」? – Jamby 2013-05-06 08:28:42

相關問題