Link to home
Create AccountLog in
Avatar of RobertoFreemano
RobertoFreemanoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Auto close carriage returns/space between paragraghs - VB.NET 2010

HI Experts,

I have a laborious task in work of pasting text into notepad and manually closing-up any extra carriage-returns in the format... I'd like advice on how to paste into a Text Box (win form) and it auto closes spaces...

From: e.g. (currently)
xxxxxx

xxxx

xxxxxxxxx

TO: e.g. (new solution)
xxxxxx
xxxx
xxxxxxxxx

 

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Try

textbox1.text = textbox1.text.replace(vbcrlf & vbcrlf, vbcrlf)
Avatar of RobertoFreemano

ASKER

Hi CC,

Your code almost works... it leaves 1 space line between cetain text... please see my pic... i've numbered the CR lines.

It is possible to complete close CR?

Thanks,
Roberto
untitled.JPG
CC,

This is what your code does ;)
untitled1.JPG
You can try using Trim

Dim txt As String
For each line in textbox1.Lines
   If string.IsNullOrEmpty(line) = False Then
    txt &= line.trim() & vbcrlf
   End If
Next
Textbox1.text = txt.Trim().Trim(vbcrlf).Trim()
this had no effect - can you check my code - is it something i've done?
Dim txt As String
    
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        For Each line In TextBox1.Lines
            If String.IsNullOrEmpty(line) = False Then
                txt &= line.Trim() & vbCrLf
            End If
        Next
        TextBox1.Text = txt.Trim().Trim(vbCrLf).Trim()

    End Sub

Open in new window

You are using the wrong event by the way. This will cause an infinite loop. What happens if you use a button or if you use two textboxes?
Do you mean <TextBox1_TextChanged> this bit?
I can't see any alternate in Declarations... do you know which one will work please CC?
What I meant was that you are changing the text of textbox in its text_changed event which means the event will fire again, text will be changed again, event will fire again .....

Use one textbox to put text in and another to put modified text in.
Sorry CC, my brain hurts :(
Do you mean you did not understand me?
HI CC,

Sorry Yes! :(
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
I think the main difference is I added the trim in below line

If String.IsNullOrEmpty(line.Trim) = False Then
Sorry CC,

I took an unexpected holiday... back now... will try you code - thanks!

Roberto
Thanks CC,

Sorry for the slow response.

;)