How do I add a newline to a windows-forms TextBox?

后端 未结 14 431
温柔的废话
温柔的废话 2021-01-01 09:37

I am trying to add a line of text to a TextBox component in VB.net, but I cannot figure out for the life of me how to force a new line. Right now it just adds onto what I ha

相关标签:
14条回答
  • 2021-01-01 10:27

    The richtextbox also has a "Lines" property that is an array of strings. Each item in this array ends in an implicit line break and will be displayed on its own line.

    If your text is static or has an initial value and you are using the designer in Visual Studio you can simply add lines directly there.

    0 讨论(0)
  • 2021-01-01 10:28

    Try vbCrLf.

    For example:

    TextBox1.text = "line_one" & vbCrLf & "line_two"
    
    0 讨论(0)
  • 2021-01-01 10:33
    TextBox2.Text = "Line 1" & Environment.NewLine & "Line 2"
    

    or

    TextBox2.Text = "Line 1"
    TextBox2.Text += Environment.NewLine
    TextBox2.Text += "Line 2"
    

    This, is how it is done.

    0 讨论(0)
  • 2021-01-01 10:37

    Took this from JeffK and made it a little more compact.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Dim Newline As String = System.Environment.NewLine
    
        TextBox1.Text = "This is a test"
        TextBox1.Text += Newline + "This is another test"
    
    End Sub
    
    0 讨论(0)
  • 2021-01-01 10:38

    Try something like

    "Line 1" & Environment.NewLine & "Line 2"
    
    0 讨论(0)
  • 2021-01-01 10:40

    Try using Environment.NewLine:

    Gets the newline string defined for this environment.

    Something like this ought to work:

    textBox.AppendText("your new text" & Environment.NewLine)
    
    0 讨论(0)
提交回复
热议问题