none
Reading MultiLine TextBox line by line

    Question

  • Hi

    i have one text box on my application with Multiline = True proeprty.
    how can i read its conternts line by line ??
    actually i need to print the text as it is apperaing in the text box (3 or 4 lines....)

    when i use the command :

    e.Graphics.DrawString((textbox1.text), ENFont, Brushes.Black, 600.0F, 290.0F, string_format)

    it is printing all lines on one line

    thanks
    Monday, July 06, 2009 1:27 PM

Answers

  • This problem isn't as easy as you might think, or in my oppinion it should be, since the text box automatically wraps text for you. You have determine where the text will wrap and then, force it to the next line when you are printing it. Here is an example: (Thanks to JhonWein for the Code!)

    Public Class Form1
      Dim WithEvents Btn As New Button
      Dim tb as New TextBox
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Btn.Parent = Me
        tb.Parent = Me
        tb.Top = Btn.Bottom + 3
        tb.Multiline = True
        tb.Width = 100
        tb.Height = 100
      End Sub
      Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn.Click
        Dim T As String = "Now is the time for all good men to come to the aid of their countrymen."
        tb.Text = T
        Dim G As Graphics = tb.CreateGraphics
        Dim CR(0) As CharacterRange
        Dim SF As New StringFormat()
        Dim Rgn() As Region
        Dim W, LastW As Single
        Do
          CR(0).Length += 1
          If CR(0).Length > T.Length Then Exit Do
          SF.SetMeasurableCharacterRanges(CR)
          Rgn = G.MeasureCharacterRanges(T, tb.Font, New Rectangle(0, 0, tb.Width, tb.Height), SF)
          W = Rgn(0).GetBounds(G).Width
          If W > LastW Then
            LastW = W
            Continue Do
          End If
          Console.WriteLine(T.Substring(CR(0).First, CR(0).Length - 1))
          T = T.Substring(CR(0).Length)
          CR(0).Length = 0
          LastW = 0
        Loop
        Console.WriteLine(T.Substring(CR(0).First, CR(0).Length - 1))
      End Sub
    End Class
    It writes the lines to the output window. Hope this helps.
    Programming is easy, understanding how is not.
    • Marked as answer by YasinN Tuesday, July 07, 2009 11:43 AM
    • Unmarked as answer by YasinN Tuesday, July 07, 2009 11:43 AM
    • Marked as answer by YasinN Tuesday, July 07, 2009 11:45 AM
    Monday, July 06, 2009 1:59 PM

All replies

  •     Dim tb As New TextBox
        tb.Multiline = True
        tb.Text = "line1" + vbCrLf + "line2"
    
        For Each ln As String In tb.Lines
          MsgBox(ln)
        Next

    FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial
    • Marked as answer by YasinN Tuesday, July 07, 2009 11:42 AM
    • Unmarked as answer by YasinN Tuesday, July 07, 2009 11:43 AM
    Monday, July 06, 2009 1:45 PM
  • This problem isn't as easy as you might think, or in my oppinion it should be, since the text box automatically wraps text for you. You have determine where the text will wrap and then, force it to the next line when you are printing it. Here is an example: (Thanks to JhonWein for the Code!)

    Public Class Form1
      Dim WithEvents Btn As New Button
      Dim tb as New TextBox
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Btn.Parent = Me
        tb.Parent = Me
        tb.Top = Btn.Bottom + 3
        tb.Multiline = True
        tb.Width = 100
        tb.Height = 100
      End Sub
      Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn.Click
        Dim T As String = "Now is the time for all good men to come to the aid of their countrymen."
        tb.Text = T
        Dim G As Graphics = tb.CreateGraphics
        Dim CR(0) As CharacterRange
        Dim SF As New StringFormat()
        Dim Rgn() As Region
        Dim W, LastW As Single
        Do
          CR(0).Length += 1
          If CR(0).Length > T.Length Then Exit Do
          SF.SetMeasurableCharacterRanges(CR)
          Rgn = G.MeasureCharacterRanges(T, tb.Font, New Rectangle(0, 0, tb.Width, tb.Height), SF)
          W = Rgn(0).GetBounds(G).Width
          If W > LastW Then
            LastW = W
            Continue Do
          End If
          Console.WriteLine(T.Substring(CR(0).First, CR(0).Length - 1))
          T = T.Substring(CR(0).Length)
          CR(0).Length = 0
          LastW = 0
        Loop
        Console.WriteLine(T.Substring(CR(0).First, CR(0).Length - 1))
      End Sub
    End Class
    It writes the lines to the output window. Hope this helps.
    Programming is easy, understanding how is not.
    • Marked as answer by YasinN Tuesday, July 07, 2009 11:43 AM
    • Unmarked as answer by YasinN Tuesday, July 07, 2009 11:43 AM
    • Marked as answer by YasinN Tuesday, July 07, 2009 11:45 AM
    Monday, July 06, 2009 1:59 PM

  • your code is amzing it is helpd me a lot, thaaaaaaaanks

    i just modifed it like this

    z1 = T.Substring(CR(0).First, CR(0).Length - 1) + vbCrLf

    thannnks

    Tuesday, July 07, 2009 11:42 AM
  • Thanks, but I can't take credit for the code. I only modified it, from a RichTextBox to a Multiline TextBox, from some code that JhonWein posted on a thread of mine. He is the real genius. :) Glad I could help.
    Programming is easy, understanding how is not.
    Tuesday, July 07, 2009 12:21 PM