none
How to spilt mlutiline Textbox to two lines only with first Space condition RRS feed

  • Question

  • hello every one

    i have textbox with multiline= true it's contian for example and there is  5 spaces

    I like to be the best

    and i would like to be shown like that

    make new line after first space

    first line: I

    second line : the remaining words

    I 
    like to be the best

    My code is working only with 4 words, if it was less or more than 4 words i get error

    How to make new line after first space only with unlimited words in line2

    My code

    Dim str() As String = TextBox1.Text.Split(" ")
           TextBox2.AppendText(str(0) + vbCrLf + str(1) + " " + str(2) + " " + str(3))
     





    Monday, September 2, 2019 12:07 AM

Answers

  • Hi

    Here is one way to do it. This one uses a Button1 to change the TextBox2 one line into 2 lines.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	TextBox2.Text = TextBox2.Text.Split(" "c)(0) & vbCrLf & TextBox2.Text.Substring(TextBox2.Text.IndexOf(" "c) + 1, TextBox2.Text.Length - TextBox2.Text.Split(" "c)(0).Length - 1)
    End Sub


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, September 2, 2019 12:47 AM
    • Marked as answer by Mostafa Salaheldien Monday, September 2, 2019 1:09 AM
    Monday, September 2, 2019 12:47 AM

All replies

  • Hi

    Here is one way to do it. This one uses a Button1 to change the TextBox2 one line into 2 lines.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	TextBox2.Text = TextBox2.Text.Split(" "c)(0) & vbCrLf & TextBox2.Text.Substring(TextBox2.Text.IndexOf(" "c) + 1, TextBox2.Text.Length - TextBox2.Text.Split(" "c)(0).Length - 1)
    End Sub


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, September 2, 2019 12:47 AM
    • Marked as answer by Mostafa Salaheldien Monday, September 2, 2019 1:09 AM
    Monday, September 2, 2019 12:47 AM
  • Here is a version to try

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim test = TextBox1.Text.Split(" "c)
            If test.Length > 0 Then
                TextBox2.AppendText($"{test(0)}{Environment.NewLine}{String.Join(" ", test.Skip(1).Take(test.Length - 1))}{Environment.NewLine}")
            End If
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, September 2, 2019 12:50 AM
    Moderator
  • Thank You so much leshay it's working Perfect




    Monday, September 2, 2019 1:12 AM
  • Thank You so much Karen it's working Perfect also
    Monday, September 2, 2019 1:13 AM
  • Thank You so much Karen it's working Perfect also
    Then you should consider marking mine as "answered" also as there can be more than one answer to a question.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, September 2, 2019 2:01 AM
    Moderator