Results 1 to 6 of 6

Thread: RichTextBox Multiline to RichTextBox Singleline

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    RichTextBox Multiline to RichTextBox Singleline

    Hi all , I have a small problem, I'm loading a file to RichTextBox1:

    Number: 1' Location: DVD' NrLocation: 1' Name: Movie 1' Year: 2013' Number: 2' Location: DVD' NrLocation: 2' Name: Movie 2' Year: 2013' Number: 3' Location: DVD' NrLocation: 3' Name: Movie 3' Year: 2013' Number: 4' Location: DVD' NrLocation: 4' Name: Movie 4' Year: 2013' Number: 5' Location: DVD' NrLocation: 5' Name: Movie 5' Year: 2013' Number: 6' Location: DVD' NrLocation: 6' Name: Movie 6' Year: 2013' Number: 7' Location: DVD' NrLocation: 7' Name: Movie 7' Year: 2013' Number: 8' Location: DVD' NrLocation: 8' Name: Movie 8' Year: 2013' Number: 9' Location: DVD' NrLocation: 9' Name: Movie 9' Year: 2013' Number: 10' Location: DVD' NrLocation: 10' Name: Movie 10' Year: 2013' Number: 11' Location: DVD' NrLocation: 11' Name: Movie 11' Year: 2013' Number: 12' Location: DVD' NrLocation: 12' Name: Movie 12'
    I want to copy all numbers to RichTextBox2 and all names to RichTextBox3, so, my RichTextBox2 should look like this:

    Code:
    1
    2
    3
    ...
    and my RichTextBox3 should look like this:

    Code:
    Movie 1
    Movie 2
    Movie 3
    ...
    so far I got only the first number and the first name, but I need all of the numbers and all names.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: RichTextBox Multiline to RichTextBox Singleline

    Ah yes, Movie 2, always loved that one!

    Split on '[space] and loop through the resulting array checking for Number or Name would appear to be the obvious solution.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: RichTextBox Multiline to RichTextBox Singleline

    Quote Originally Posted by dunfiddlin View Post
    Ah yes, Movie 2, always loved that one!

    Split on '[space] and loop through the resulting array checking for Number or Name would appear to be the obvious solution.
    Do you have any examples?

    I even found this but still nothing:

    Code:
    Dim lines() As String = RichTextBox1.Lines
    RichTextBox2.Text = Array.Find(lines, Function(s) s.StartsWith("Number: ")).Split(New String() {":"}, StringSplitOptions.None)(1).TrimEnd("'"c)
    RichTextBox3.Text = Array.Find(lines, Function(s) s.StartsWith("Name: ")).Split(New String() {":"}, StringSplitOptions.None)(1).TrimEnd("'"c)

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: RichTextBox Multiline to RichTextBox Singleline

    vb.net Code:
    1. Dim s As String = "Number: 1' Location: DVD' NrLocation: 1' Name: Movie 1' Year: 2013' Number: 2' Location: DVD' NrLocation: 2' Name: Movie 2' Year: 2013' Number: 3' Location: DVD' NrLocation: 3' Name: Movie 3' Year: 2013' Number: 4' Location: DVD' NrLocation: 4' Name: Movie 4' Year: 2013' Number: 5' Location: DVD' NrLocation: 5' Name: Movie 5' Year: 2013' Number: 6' Location: DVD' NrLocation: 6' Name: Movie 6' Year: 2013' Number: 7' Location: DVD' NrLocation: 7' Name: Movie 7' Year: 2013' Number: 8' Location: DVD' NrLocation: 8' Name: Movie 8' Year: 2013' Number: 9' Location: DVD' NrLocation: 9' Name: Movie 9' Year: 2013' Number: 10' Location: DVD' NrLocation: 10' Name: Movie 10' Year: 2013' Number: 11' Location: DVD' NrLocation: 11' Name: Movie 11' Year: 2013' Number: 12' Location: DVD' NrLocation: 12' Name: Movie 12'"
    2.  
    3.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    4.         Dim splits = s.Split({"' "}, StringSplitOptions.RemoveEmptyEntries)
    5.         For Each st In splits
    6.             If st.StartsWith("Number:") Then
    7.                 RichTextBox2.AppendText(st.Split(" "c)(1) & vbCrLf)
    8.             ElseIf st.StartsWith("Name:") Then
    9.                 RichTextBox3.AppendText(st.Split({": "}, StringSplitOptions.None)(1) & vbCrLf)
    10.             End If
    11.         Next
    12.     End Sub
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: RichTextBox Multiline to RichTextBox Singleline

    Another way using regular expressions.

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class MainForm
    4.  
    5.     Private ReadOnly m_source As String = "Number: 1' Location: DVD' NrLocation: 1' Name: Movie 1' Year: 2013'" & _
    6.                                           "Number: 2' Location: DVD' NrLocation: 2' Name: Movie 2' Year: 2013'" & _
    7.                                           "Number: 3' Location: DVD' NrLocation: 3' Name: Movie 3' Year: 2013' " & _
    8.                                           "Number: 4' Location: DVD' NrLocation: 4' Name: Movie 4' Year: 2013' " & _
    9.                                           "Number: 5' Location: DVD' NrLocation: 5' Name: Movie 5' Year: 2013' " & _
    10.                                           "Number: 6' Location: DVD' NrLocation: 6' Name: Movie 6' Year: 2013' " & _
    11.                                           "Number: 7' Location: DVD' NrLocation: 7' Name: Movie 7' Year: 2013' " & _
    12.                                           "Number: 8' Location: DVD' NrLocation: 8' Name: Movie 8' Year: 2013' " & _
    13.                                           "Number: 9' Location: DVD' NrLocation: 9' Name: Movie 9' Year: 2013' " & _
    14.                                           "Number: 10' Location: DVD' NrLocation: 10' Name: Movie 10' Year: 2013' " & _
    15.                                           "Number: 11' Location: DVD' NrLocation: 11' Name: Movie 11' Year: 2013' " & _
    16.                                           "Number: 12' Location: DVD' NrLocation: 12' Name: Movie 12' Year: 2013'"
    17.  
    18.     Private ReadOnly m_pattern As String = "Number\: (?<Number>.+?)\' Location\: DVD\' " & _
    19.                                            "NrLocation: \d{1,}' Name\: (?<Name>.+?)\' Year\: \d{4}\'"
    20.     Private ReadOnly m_match As New Regex(m_pattern, RegexOptions.IgnoreCase Or RegexOptions.Compiled)
    21.  
    22.  
    23.     Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    24.  
    25.         Dim matches = Me.m_match.Matches(Me.m_source)
    26.  
    27.         For Each m As Match In matches
    28.             Me.RichTextBox1.AppendText(m.Groups("Number").Value & Environment.NewLine)
    29.             Me.RichTextBox2.AppendText(m.Groups("Name").Value & Environment.NewLine)
    30.         Next
    31.  
    32.     End Sub
    33.  
    34. End Class

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: RichTextBox Multiline to RichTextBox Singleline

    Thanks guys, you are the best. Best forum ever.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width