-
Jun 7th, 2010, 09:17 AM
#1
Thread Starter
New Member
-
Jun 7th, 2010, 09:49 AM
#2
Addicted Member
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.
-
Jun 7th, 2010, 10:03 AM
#3
Thread Starter
New Member
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.
-
Jun 7th, 2010, 10:08 AM
#4
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
-
Jun 7th, 2010, 10:19 AM
#5
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
If you find a response helpful then remember to Rate it
Personal website Sam Jenkins
-
Jun 7th, 2010, 10:20 AM
#6
Thread Starter
New Member
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.
-
Jun 7th, 2010, 10:20 AM
#7
Addicted Member
Re: how to limit multiline textbox perline length in vb.net?
Originally Posted by XBMY
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.
-
Jun 7th, 2010, 10:23 AM
#8
Re: how to limit multiline textbox perline length in vb.net?
@XBMY is the textbox a fixed width or is it possible that the width will change?
If you find a response helpful then remember to Rate it
Personal website Sam Jenkins
-
Jun 7th, 2010, 10:25 AM
#9
Thread Starter
New Member
Re: how to limit multiline textbox perline length in vb.net?
Satal Keto. when I execute your code the program hangs.
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
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()
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.
-
Jun 7th, 2010, 10:29 AM
#10
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.
If you find a response helpful then remember to Rate it
Personal website Sam Jenkins
-
Jun 7th, 2010, 10:47 AM
#11
Re: how to limit multiline textbox perline length in vb.net?
Maybe this simple one-liner could be of some help.
vb Code:
IO.File.WriteAllLines("C:\temp\test.txt", TextBox1.Lines)
-
Jun 7th, 2010, 10:56 AM
#12
Addicted Member
Re: how to limit multiline textbox perline length in vb.net?
Originally Posted by Pradeep1210
Maybe this simple one-liner could be of some help.
vb Code:
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.
-
Jun 7th, 2010, 11:11 AM
#13
Addicted Member
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.
-
Jun 7th, 2010, 11:12 AM
#14
Thread Starter
New Member
-
Jun 7th, 2010, 11:19 AM
#15
Re: how to limit multiline textbox perline length in vb.net?
Originally Posted by XBMY
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
-
Jun 7th, 2010, 11:30 AM
#16
Re: how to limit multiline textbox perline length in vb.net?
ok. This should work:
vb Code:
Dim lines As New List(Of String) Dim sourceText As String = TextBox1.Text For i = 0 To 9999999 Dim pos1 As Integer = TextBox1.GetFirstCharIndexFromLine(i) Dim pos2 As Integer = TextBox1.GetFirstCharIndexFromLine(i + 1) If pos2 > pos1 Then lines.Add(sourceText.Substring(pos1, pos2 - pos1)) Else If pos1 < sourceText.Length Then lines.Add(sourceText.Substring(pos1)) Exit For End If Next IO.File.WriteAllLines("C:\temp\test.txt", lines.ToArray)
-
Jun 7th, 2010, 11:45 AM
#17
Re: how to limit multiline textbox perline length in vb.net?
Originally Posted by Pradeep1210
ok. This should work:
vb Code:
Dim lines As New List(Of String)
Dim sourceText As String = TextBox1.Text
For i = 0 To 9999999
Dim pos1 As Integer = TextBox1.GetFirstCharIndexFromLine(i)
Dim pos2 As Integer = TextBox1.GetFirstCharIndexFromLine(i + 1)
If pos2 > pos1 Then
lines.Add(sourceText.Substring(pos1, pos2 - pos1))
Else
If pos1 < sourceText.Length Then lines.Add(sourceText.Substring(pos1))
Exit For
End If
Next
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
-
Jun 7th, 2010, 12:15 PM
#18
Addicted Member
Re: how to limit multiline textbox perline length in vb.net?
Learned something new here again today!
-
Jun 7th, 2010, 12:22 PM
#19
Re: how to limit multiline textbox perline length in vb.net?
Pradeep1210 +rep for really useful answer, as above, learnt something new today
If you find a response helpful then remember to Rate it
Personal website Sam Jenkins
-
Jun 7th, 2010, 12:37 PM
#20
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.
-
Jun 7th, 2010, 07:17 PM
#21
Thread Starter
New Member
-
Jun 8th, 2010, 04:55 AM
#22
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.
-
Jun 8th, 2010, 06:36 AM
#23
Thread Starter
New Member
-
Jun 8th, 2010, 06:43 AM
#24
Re: how to limit multiline textbox perline length in vb.net?
This might do the trick:
vb Code:
Dim lines As New List(Of String) Dim sourceText As String = TextBox1.Text For i = 0 To 9999999 Dim pos1 As Integer = TextBox1.GetFirstCharIndexFromLine(i) Dim pos2 As Integer = TextBox1.GetFirstCharIndexFromLine(i + 1) If pos2 > pos1 Then lines.Add(sourceText.Substring(pos1, pos2 - pos1)) Else If pos1 < sourceText.Length Then lines.Add(sourceText.Substring(pos1)) Exit For End If Next ''remove extra line-breaks to match textbox formatting Dim outputText As String = Join(lines.ToArray, Chr(1)) outputText = outputText.Replace(vbCrLf & Chr(1), Chr(1)) outputText = outputText.Replace(Chr(1), vbCrLf) IO.File.WriteAllText("C:\temp\test.txt", outputText)
-
Jun 8th, 2010, 06:55 AM
#25
Re: [RESOLVED] how to limit multiline textbox perline length in vb.net?
You could also use TrimEnd, it should also work.
vb Code:
Dim lines As New List(Of String) Dim sourceText As String = TextBox1.Text For i = 0 To 9999999 Dim pos1 As Integer = TextBox1.GetFirstCharIndexFromLine(i) Dim pos2 As Integer = TextBox1.GetFirstCharIndexFromLine(i + 1) If pos2 > pos1 Then lines.Add(sourceText.Substring(pos1, pos2 - pos1).Trimend()) Else If pos1 < sourceText.Length Then lines.Add(sourceText.Substring(pos1).TrimEnd()) Exit For End If Next 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
-
Jun 8th, 2010, 06:58 AM
#26
Re: [RESOLVED] how to limit multiline textbox perline length in vb.net?
Originally Posted by stlaural
You could also use TrimEnd, it should also work.
vb Code:
Dim lines As New List(Of String) Dim sourceText As String = TextBox1.Text For i = 0 To 9999999 Dim pos1 As Integer = TextBox1.GetFirstCharIndexFromLine(i) Dim pos2 As Integer = TextBox1.GetFirstCharIndexFromLine(i + 1) If pos2 > pos1 Then lines.Add(sourceText.Substring(pos1, pos2 - pos1).Trimend()) Else If pos1 < sourceText.Length Then lines.Add(sourceText.Substring(pos1).TrimEnd()) Exit For End If Next IO.File.WriteAllLines("C:\temp\test.txt", lines.ToArray)
Won't that remove the spaces, tabs etc. from the end of line?
-
Jun 8th, 2010, 06:59 AM
#27
Thread Starter
New Member
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.
-
Jun 8th, 2010, 07:23 AM
#28
Re: [RESOLVED] how to limit multiline textbox perline length in vb.net?
Originally Posted by Pradeep1210
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
-
Jun 8th, 2010, 07:46 AM
#29
Thread Starter
New Member
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.
-
Jun 8th, 2010, 08:55 AM
#30
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:
Dim lines As New List(Of String) Dim sourceText As String = TextBox1.Text For i = 0 To 9999999 Dim pos1 As Integer = TextBox1.GetFirstCharIndexFromLine(i) Dim pos2 As Integer = TextBox1.GetFirstCharIndexFromLine(i + 1) If pos2 > pos1 Then lines.Add("#" & sourceText.Substring(pos1, pos2 - pos1).Trimend() & "#") Else If pos1 < sourceText.Length Then lines.Add("#" & sourceText.Substring(pos1).TrimEnd() & "#") Exit For End If Next 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
-
Jun 8th, 2010, 09:09 AM
#31
Thread Starter
New Member
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
-
Jan 11th, 2018, 02:39 AM
#32
Registered User
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|