Results 1 to 32 of 32

Thread: [RESOLVED] how to limit multiline textbox perline length in vb.net?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    12

    Resolved [RESOLVED] how to limit multiline textbox perline length in vb.net?

    Although set textbox width can fit my request.but when writes textbox.text to a file it's actually in one line. for example .
    "Scientists say our ability to focus is being undermined by bursts of information from e-mail and other interruptions. "

    is actually one line text.but it shows 2 lines in my textbox. I want when it write to a .txt file it keep what is looks like in textbox. But it's actually one line in textbox. hope I make my question clear.

    is there any method to do what I say?

    what I think is add a Environment.NewLine at each row end. or limit length in textbox. not total maxlength but maxlength perline. for example. if I choose to limit N char per line. when words exceeded N in a line textbox will start a newline.(without break the word) can anyone tell how to make it? I did actually search for a long time.. but I didn't find any useful information for my request. thank u.
    Attached Images Attached Images  

  2. #2
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: how to limit multiline textbox perline length in vb.net?

    Code:
    TextBox2.Text = "Scientists say our ability to focus is being undermined by bursts of information from e-mail and other interruptions. " & Environment.NewLine
    TextBox2.AppendText("Scientists say our ability to focus is being undermined by bursts of information from e-mail. " & Environment.NewLine)
    For Each s In TextBox2.Text.Split(Environment.NewLine)
          MessageBox.Show(s)
    Next
    One problem with this is that the last line has nothing in it since I've put the NewLine at the end of each entry into the textbox. This should give you something to work with though.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    12

    Re: how to limit multiline textbox perline length in vb.net?

    first thanks for your reply.Perhaps you misunderstand my question.
    when I process your code. textbox2.text become
    Scientists say our ability to focus is being undermined by bursts of information from e-mail and other interruptions.
    Scientists say our ability to focus is being undermined by bursts of information from e-mail.

    it doesn't addnewline when it should.

    What I want is
    Scientists say our ability to focus is being undermined by bursts of
    information from e-mail and other interruptions.

    btw. it's a general textbox contains not onlythe text I post.

    Thanks again.

  4. #4
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: how to limit multiline textbox perline length in vb.net?

    I think what the OP meant is that his Textbox is multiline and its WordWrap property is set to true. So he does not need to add System.Environment.Newline for the TextBox do display its Text on multiple line, as the WordWrap is set to true.

    Now he wants to copy the text of his TextBox to a text file but he would like to keep the formating that the Textbox did.

    I don't know any way to do that as you can't use the Split method because of the WordWrap property, it doesn't seem to be inserting any newline character.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  5. #5
    Fanatic Member Satal Keto's Avatar
    Join Date
    Dec 2005
    Location
    Me.Location
    Posts
    518

    Re: how to limit multiline textbox perline length in vb.net?

    Perhaps this will help
    Code:
            Dim fw As New FileStream("C:\test.txt", FileMode.Create)
            Dim sw As New StreamWriter(fw)
    
            Dim arr As New ArrayList
            arr = New ArrayList(TextBox1.Text.Split(Environment.NewLine))
    
            For Each s As String In arr
                sw.WriteLine(s.Trim)
            Next
    
            sw.Close()
            fw.Close()
    UPDATE:
    Oops damn another person who mis-understood the question

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    12

    Re: how to limit multiline textbox perline length in vb.net?

    To stlaural, That's exactly what I want.

    what if split the string at nth character? That perhaps can solve the problem?

    Thank you all for helping me.

  7. #7
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: how to limit multiline textbox perline length in vb.net?

    Quote Originally Posted by XBMY View Post
    first thanks for your reply.Perhaps you misunderstand my question.
    when I process your code. textbox2.text become
    Scientists say our ability to focus is being undermined by bursts of information from e-mail and other interruptions.
    Scientists say our ability to focus is being undermined by bursts of information from e-mail.

    it doesn't addnewline when it should.

    What I want is
    Scientists say our ability to focus is being undermined by bursts of
    information from e-mail and other interruptions.

    btw. it's a general textbox contains not onlythe text I post.

    Thanks again.
    Ah, sorry, I misunderstood. Didn't read it carefully enough.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  8. #8

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    12

    Re: how to limit multiline textbox perline length in vb.net?

    Satal Keto. when I execute your code the program hangs.
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim fw As New FileStream("C:\test.txt", FileMode.Create)
    3.         Dim sw As New StreamWriter(fw)
    4.  
    5.         Dim arr As New ArrayList
    6.         arr = New ArrayList(TextBox1.Text.Split(Environment.NewLine))
    7.  
    8.         For Each s As String In arr
    9.             sw.WriteLine(s.Trim)
    10.         Next
    11.  
    12.         sw.Close()
    13.         fw.Close()
    14.  
    15.  
    16.  
    17.     End Sub

    the vs2010 highlight at arr = New ArrayList(TextBox1.Text.Split(Environment.NewLine)) error msg is
    System.NullReferenceException


    textbox has a fixed width I will got a network problem after mins. so I can't view this post untill 8hrs later..sorry for all who will help.
    Last edited by XBMY; Jun 7th, 2010 at 11:12 AM.

  10. #10
    Fanatic Member Satal Keto's Avatar
    Join Date
    Dec 2005
    Location
    Me.Location
    Posts
    518

    Re: how to limit multiline textbox perline length in vb.net?

    I can't see why it would hang, unless you have some incredibly large amount of data in textbox1, but even then it shouldn't be too much of a delay.
    To be honest you might want to forget it as I miss understood what you meant so that wont achieve what you want.

    If the textbox is always going to be a fixed width, then we might be able to do some (not very pretty) work to try and manually go through the text and try and figure out where the word wrap would have split up the sentences. This would require you to use a fixed width font (which doesn't look very nice in itself) and then a fair amount of code.

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: how to limit multiline textbox perline length in vb.net?

    Maybe this simple one-liner could be of some help.

    vb Code:
    1. IO.File.WriteAllLines("C:\temp\test.txt", TextBox1.Lines)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: how to limit multiline textbox perline length in vb.net?

    Quote Originally Posted by Pradeep1210 View Post
    Maybe this simple one-liner could be of some help.

    vb Code:
    1. IO.File.WriteAllLines("C:\temp\test.txt", TextBox1.Lines)
    I think this causes the same result as the other suggestions. Doesn't wrap the text as the OP wanted.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  13. #13
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: how to limit multiline textbox perline length in vb.net?

    Here's a way to approximate the length of a line to write

    Code:
            
            Dim g As Graphics = TextBox2.CreateGraphics
            Dim word As String
            Dim test As String = ""
            Dim words() As String
            Dim oneLine As String
            oneLine = "Scientists say our ability to focus is being undermined by bursts of information from e-mail and other interruptions. "
            words = oneLine.Split(" ")
            For Each word In words
                test = test & word
                If g.MeasureString(test, TextBox2.Font).Width > TextBox2.Width Then
                    MessageBox.Show("string to write to your file = " & Environment.NewLine & test)
                    test = ""
                Else
                    test = test & " "
                End If
            Next
    You'd have to add some code to loop through all the lines of the textbox as well as to write when the end of a line is reached. It's close but not exact. You would add code to write a line to your file at the point where I've displayed the message.
    Last edited by dlscott56; Jun 7th, 2010 at 11:14 AM.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  14. #14

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    12

    Re: how to limit multiline textbox perline length in vb.net?

    I use another computer now. in another network, the network which my computer connected to cannot be reach atm.Sorry to Satal Keto I didn;t check the code clearly right now. I didn't change textbox to mine name. Now I make it right.after use your code the test.txt stay in one line also. because there isn't any newline in textbox .just textbox makes it show as 2 line. sorry for my poor english.hope you guys can understand my situation now. seems u split the word by Environment.NewLine. but what I want is to split the word at every nth char. and add a Environment.NewLine ,as what my textbox shows.
    for the futher reply I can't test the code now. sorry. to Pradeep1210 and dlscott56
    I will test yout code when my computer can reach the net and report the result here. perhaps after 6-8hrs

    thanks again to everyone who helps me. Thank you for giving a hand to a newbie
    Last edited by XBMY; Jun 7th, 2010 at 11:16 AM.

  15. #15
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: how to limit multiline textbox perline length in vb.net?

    Quote Originally Posted by XBMY View Post
    To stlaural, That's exactly what I want.

    what if split the string at nth character? That perhaps can solve the problem?

    Thank you all for helping me.
    I don't think that spliting at a specific position (nth character) would help as you can't really know at what position the line ends. The textbox splits your text on multiple line if your text exceeds the textbox width but it does not necessarily always split at the same position, it depends on the position of spaces and the length of the words.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  16. #16
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: how to limit multiline textbox perline length in vb.net?

    ok. This should work:
    vb Code:
    1. Dim lines As New List(Of String)
    2. Dim sourceText As String = TextBox1.Text
    3. For i = 0 To 9999999
    4.     Dim pos1 As Integer = TextBox1.GetFirstCharIndexFromLine(i)
    5.     Dim pos2 As Integer = TextBox1.GetFirstCharIndexFromLine(i + 1)
    6.     If pos2 > pos1 Then
    7.         lines.Add(sourceText.Substring(pos1, pos2 - pos1))
    8.     Else
    9.         If pos1 < sourceText.Length Then lines.Add(sourceText.Substring(pos1))
    10.         Exit For
    11.     End If
    12. Next
    13. IO.File.WriteAllLines("C:\temp\test.txt", lines.ToArray)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  17. #17
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: how to limit multiline textbox perline length in vb.net?

    Quote Originally Posted by Pradeep1210 View Post
    ok. This should work:
    vb Code:
    1. Dim lines As New List(Of String)
    2. Dim sourceText As String = TextBox1.Text
    3. For i = 0 To 9999999
    4.     Dim pos1 As Integer = TextBox1.GetFirstCharIndexFromLine(i)
    5.     Dim pos2 As Integer = TextBox1.GetFirstCharIndexFromLine(i + 1)
    6.     If pos2 > pos1 Then
    7.         lines.Add(sourceText.Substring(pos1, pos2 - pos1))
    8.     Else
    9.         If pos1 < sourceText.Length Then lines.Add(sourceText.Substring(pos1))
    10.         Exit For
    11.     End If
    12. Next
    13. IO.File.WriteAllLines("C:\temp\test.txt", lines.ToArray)
    Yeah I just tested it and it does work, I guess that GetFirstCharIndexFromLine() is one of the rare methods that counts lines event if its a wrapped one.

    Nice work.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  18. #18
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: how to limit multiline textbox perline length in vb.net?

    Learned something new here again today!
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  19. #19

  20. #20
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: how to limit multiline textbox perline length in vb.net?

    Thanks

    That code is not optimized however. It was a quick rig up to show a demo. It uses the TextBox1.GetFirstCharIndexFromLine two times where once would have sufficed. On the next iteration, the same value would be retrieved in one of them. So we could save it to some integer variable and use it instead of calling the function twice.
    I leave that to you to optimize the code.
    Last edited by Pradeep1210; Jun 7th, 2010 at 12:42 PM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  21. #21

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    12

    Re: how to limit multiline textbox perline length in vb.net?

    to Pradeep1210. It works like a charm. It's awesome man.


    I really learn a lots of things today from here. Thanks to everyone again

    PS:Just a little thing but it doesn't matter at all.If I press Enter at the end of row in manual instead of let the textbox do goes to next line..and enter something. when open the output file. it has double newline char. anyway this doesn't matter. I will just replace double newline to one.
    Attached Images Attached Images  
    Last edited by XBMY; Jun 7th, 2010 at 07:32 PM.

  22. #22
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: how to limit multiline textbox perline length in vb.net?

    It might be hard to determine which enters to remove and which ones to not. You want to remove only if that enter is at the wrapping point and not otherwise, Isn't it?

    The best way I think would be not to insert them at all. Insert them only where you really require a hard break.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  23. #23

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    12

    Re: how to limit multiline textbox perline length in vb.net?

    You want to remove only if that enter is at the wrapping point and not otherwise, Isn't it? I just want to keep the txt file as what textbox shown.:P
    Sometimes I do really need hard break.check the pic below btw. I replace your 9999999 to a linecount

    Private Const EM_GETLINECOUNT As Integer = &HBA
    Dim LineCount As Integer = SendMessageINT(TextBox1.Handle, EM_GETLINECOUNT, 0, IntPtr.Zero)
    For i = 0 To LineCount - 1


    anyway the method you are giving is the best way to solve the problem as far as I concern. and when the textbox has only oneline it insert newline too but it doesn't matter
    Attached Images Attached Images  
    Last edited by XBMY; Jun 8th, 2010 at 06:43 AM.

  24. #24
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: how to limit multiline textbox perline length in vb.net?

    This might do the trick:
    vb Code:
    1. Dim lines As New List(Of String)
    2. Dim sourceText As String = TextBox1.Text
    3. For i = 0 To 9999999
    4.     Dim pos1 As Integer = TextBox1.GetFirstCharIndexFromLine(i)
    5.     Dim pos2 As Integer = TextBox1.GetFirstCharIndexFromLine(i + 1)
    6.     If pos2 > pos1 Then
    7.         lines.Add(sourceText.Substring(pos1, pos2 - pos1))
    8.     Else
    9.         If pos1 < sourceText.Length Then lines.Add(sourceText.Substring(pos1))
    10.         Exit For
    11.     End If
    12. Next
    13.  
    14. ''remove extra line-breaks to match textbox formatting
    15. Dim outputText As String = Join(lines.ToArray, Chr(1))
    16. outputText = outputText.Replace(vbCrLf & Chr(1), Chr(1))
    17. outputText = outputText.Replace(Chr(1), vbCrLf)
    18.  
    19. IO.File.WriteAllText("C:\temp\test.txt", outputText)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  25. #25
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: [RESOLVED] how to limit multiline textbox perline length in vb.net?

    You could also use TrimEnd, it should also work.
    vb Code:
    1. Dim lines As New List(Of String)
    2. Dim sourceText As String = TextBox1.Text
    3. For i = 0 To 9999999
    4.     Dim pos1 As Integer = TextBox1.GetFirstCharIndexFromLine(i)
    5.     Dim pos2 As Integer = TextBox1.GetFirstCharIndexFromLine(i + 1)
    6.     If pos2 > pos1 Then
    7.         lines.Add(sourceText.Substring(pos1, pos2 - pos1).Trimend())
    8.     Else
    9.         If pos1 < sourceText.Length Then lines.Add(sourceText.Substring(pos1).TrimEnd())
    10.         Exit For
    11.     End If
    12. Next
    13. IO.File.WriteAllLines("C:\temp\test.txt", lines.ToArray)
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  26. #26
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [RESOLVED] how to limit multiline textbox perline length in vb.net?

    Quote Originally Posted by stlaural View Post
    You could also use TrimEnd, it should also work.
    vb Code:
    1. Dim lines As New List(Of String)
    2. Dim sourceText As String = TextBox1.Text
    3. For i = 0 To 9999999
    4.     Dim pos1 As Integer = TextBox1.GetFirstCharIndexFromLine(i)
    5.     Dim pos2 As Integer = TextBox1.GetFirstCharIndexFromLine(i + 1)
    6.     If pos2 > pos1 Then
    7.         lines.Add(sourceText.Substring(pos1, pos2 - pos1).Trimend())
    8.     Else
    9.         If pos1 < sourceText.Length Then lines.Add(sourceText.Substring(pos1).TrimEnd())
    10.         Exit For
    11.     End If
    12. Next
    13. IO.File.WriteAllLines("C:\temp\test.txt", lines.ToArray)
    Won't that remove the spaces, tabs etc. from the end of line?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  27. #27

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    12

    Re: [RESOLVED] how to limit multiline textbox perline length in vb.net?

    I learn a lots from your guys code Thanks. really saves my days.

  28. #28
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: [RESOLVED] how to limit multiline textbox perline length in vb.net?

    Quote Originally Posted by Pradeep1210 View Post
    Won't that remove the spaces, tabs etc. from the end of line?
    Yes it will, I should have mentioned it
    I tried the TrimEnd as it is simple and I didn't see any reason why to keep trailing whitespace characters.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  29. #29

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    12

    Re: [RESOLVED] how to limit multiline textbox perline length in vb.net?

    :)Sorry for one more request. how to add some certain char at everyline beginning and end.

    eg. the char I want to add is #.
    original text

    Visual Basic .NET.
    Visual Basic 6

    after
    #Visual Basic .NET.#
    #Visual Basic 6#

    is this possible? Thanks.I'm a newbie in coding.
    just started learning it months ago. Please don't blame my silly question:P

    I use outputText = Regex.Replace(outputText, "^", "#") it only add at firstline
    Last edited by XBMY; Jun 8th, 2010 at 08:46 AM.

  30. #30
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: [RESOLVED] how to limit multiline textbox perline length in vb.net?

    String Concatenation would be the simplest way to go I guess.
    vb Code:
    1. Dim lines As New List(Of String)
    2. Dim sourceText As String = TextBox1.Text
    3. For i = 0 To 9999999
    4.     Dim pos1 As Integer = TextBox1.GetFirstCharIndexFromLine(i)
    5.     Dim pos2 As Integer = TextBox1.GetFirstCharIndexFromLine(i + 1)
    6.     If pos2 > pos1 Then
    7.         lines.Add("#" & sourceText.Substring(pos1, pos2 - pos1).Trimend() & "#")
    8.     Else
    9.         If pos1 < sourceText.Length Then lines.Add("#" & sourceText.Substring(pos1).TrimEnd() & "#")
    10.         Exit For
    11.     End If
    12. Next
    13. IO.File.WriteAllLines("C:\temp\test.txt", lines.ToArray)
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  31. #31

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    12

    Re: [RESOLVED] how to limit multiline textbox perline length in vb.net?

    yeah.that's the simple way.I forget that we can add string base on the current code

  32. #32
    Registered User
    Join Date
    Nov 2017
    Posts
    1

    Re: [RESOLVED] how to limit multiline textbox perline length in vb.net?

    this is the simple way

    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    Dim a As String '//////////////////////////////////////////////////////////////////
    a = TextBox1.Text '///ini untuk menghentikan KNFLIK MULTILINE DAN MAXLENGTH
    If TextBox1.TextLength >= TextBox1.MaxLength Then TextBox1.Text = a : Exit Sub '/////////////////
    end sub

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