[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1101
  • Last Modified:

Linefeeds, textboxes and alternatives

If I add a textbox to an app in VB.NET and then given a large amount of text I insert linefeeds (vbLF) the textbox displays those linefeeds as a little box (so does Notepad).

I have tried vbCrLf  vbNewline  vbLf  and vbCr - all with no success.

How do a insert a linebreak in a string such that it will simply view as a linebreak in the Textbox (and Notepad) instead of actually displaying a small box?
0
Kitsune
Asked:
Kitsune
  • 5
  • 4
1 Solution
 
AlexFMCommented:
vbCrLf works in my test. Ensure that textbox has Multiline property set to True.
0
 
rrbeckerCommented:
Hi Kitsune

This is the approach I use:

-------------snippet-----------------------
Imports Microsoft.Win32

Dim SomeTextString as String

SomeTextString = "" '<<--- I do this only to make sure that I start with Nothing

SomeTextString = SomeTextString & "Now add abunch of text and when you want" & vbnewline
SomeTextString = SomeTextString & "a line break just add  'vbnewline' at the end." & vbnewline

----------end snippet---------------------

Hope this helps

rrbecker
0
 
KitsuneAuthor Commented:
I have a textbox - txtOriginal and the user can either type in here or paste text from Word, Notepad, whatever.

txtFinal copies the text from txtOriginal but inserts vbLF or vbCrLf but the display of txtFinal shows up as small boxes. If I copy this text into Outlook it looks great which is the intended output but it looks like small boxes in Notepad and probably other mail programs.

AlexFM - I am using the multiline property set to True.


Following is the source code for my function, which does work properly, but I want the display in txtFinal to appear with an actual linebreak rather than a small box.



'========================================================
    'Generate
    '========================================================
    Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
        Dim t1 As String
        Dim t2 As String
        Dim final As String
        Dim linebreak As String
        Dim max As Integer
        Dim spotreturn As Integer
        Dim startpos As Long


        'get original email text
        t1 = txtOriginal.Text

        'get maximum characters per line
        Try
            max = CInt(txtMax.Text)
        Catch
            max = 65
        End Try

        'Look for a comma in the very first snippet and insert
        'a code. This is usually the Dear John, Hi Jane, etc.
        t2 = Mid(t1, 1, max)
        If InStr(t2, ",") Then
            spotreturn = InStr(t2, ",")
            Try
                t1 = Mid(t1, 1, spotreturn) & "#SPECIALTOP#" & Mid(t1, spotreturn + 1)
            Catch ex As Exception
            End Try
        End If

        'replace the newlines that follow a period with a code
        t1 = Replace(t1, "." & vbCrLf, "#SPECIAL#")
        'strip out all other carriage returns and line feeds
        t1 = Replace(t1, vbCrLf, " ")
        t1 = Replace(t1, vbLf, " ")

        'set our linebreak
        linebreak = txtLineBreak.Text

        'change our code back to a linebreak
        t1 = Replace(t1, "#SPECIAL#", "." & linebreak)
        t1 = Replace(t1, "#SPECIALTOP#", linebreak)

        startpos = 1
        'keep looping through the text one snippet at a time
        While (startpos + max) < Len(t1)
            'get the maximum number of characters
            'from the current starting position
            '(a snippet)
            t2 = Mid(t1, startpos, max)

            If InStr(t2, linebreak) Then
                'there is a carriage return in the snippet
                spotreturn = InStr(t2, linebreak)
                t2 = Mid(t2, 1, spotreturn)
                startpos = startpos + spotreturn + Len(linebreak) - 1
            Else
                'there is no carriage return so break on a
                'space character if possible or just be ruthless
                'and break after the max characters reached
                If InStr(t2, " ") Then
                    t2 = StrReverse(t2)
                    t2 = Replace(t2, " ", vbLf, 1, 1)
                    t2 = StrReverse(t2)
                Else
                    t2 = t2 & vbLf
                End If
                startpos = startpos + max
            End If

            final = final + t2
        End While

        txtFinal.Text = final
        txtFinal.Refresh()
    End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rrbeckerCommented:
Hi Kitsune,

Try something silly.

In the 'Properties' panel for your text box, look for the 'AcceptsReturn' property and set it to true.

In the mean time I will look at you code more closely....

rrbecker
0
 
rrbeckerCommented:
Hi Kitsune,

I haven't actually copied and run you code   but I had a similar problem in VB6. I am including then VB6 code here that you may want to convert to VB.NET and try it out.

It is very simple and should convert very easily with very little change or effort.

Let me know it this helps. If it does not, then I'll open a project and try your specific example.

--------------------VB6 function--------------------------------------------
Function StripBadChars(strSearch As String) As String

       On Error GoTo MyTmpError
       '
       ' This function searches for vbNewLine, vbCr, vbCrLf, vbFormFeed
       ' vbLf, vbTab and vbVerticalTab and strips them from the current
       ' string
       '

       Dim iLen As Integer, I As Integer
       Dim RetValue As String
       Dim NoQuotes As String
       Dim CharValue As String
       Dim BitBucket As String
       Dim NL
       
       NL = Chr(10)
       
   
       If IsNull(strSearch) Or IsEmpty(strSearch) Then
          RetValue = ""
          StripBadChars = RetValue$
          Exit Function
       Else
   
          iLen = Len(strSearch)
          I = 1
          'vbTab,
          Do
            CharValue = Mid(strSearch, I, 1)
            Select Case CharValue$
                Case vbNewLine
                    RetValue = RetValue$ & " "
                Case vbCr
                    RetValue = RetValue$ & " "
                Case vbTab
                    RetValue = RetValue$ & " "
                Case vbVerticalTab
                    RetValue = RetValue$ & " "
                Case vbNullChar
                    RetValue = RetValue$ & " "
                Case vbNullString
                    RetValue = RetValue$ & " "
                'Case vbLf ' add back in just for this test
                    'RetValue = RetValue$ & NL
                Case vbCrLf
                    RetValue = RetValue$ & " "
                Case vbFormFeed
                    RetValue = RetValue$ & " "
                Case Else
                    RetValue = RetValue$ & CharValue$
            End Select
               
             I = I + 1
          Loop While I <= iLen
       End If
       
       'NoQuotes = StripQuotes(RetValue$)
       'StripBadChars = NoQuotes$
       StripBadChars = RetValue$
       Exit Function
   
MyTmpError:
    Beep
    MsgBox ("Error Occured: " & Err & " " & Err.Description)
    Screen.MousePointer = vbDefault
    Exit Function

End Function
-------------------end VB6 code--------------------------------------
0
 
KitsuneAuthor Commented:
rrbecker,

I had already tried setting the AcceptsReturn property to true. It does not "appear" to make any difference. I can get by with the function the way that it is but it will forever gnaw away at me that I let such a problem best me. ;)

If I type manually in the textbox and hit the Enter key text goes to the next line without showing any square box characters.

I am aware of the following facts:

Unix: uses a linefeed character - vbLf
Windows:uses a newline character - vbCrLf or vbNewline
Mac: uses a carriage return - vbCr

When I copy the contents and paste it into Outlook 2003 vbLf works the best and behaves as expected. Also, I don't see the square boxes that represent the linefeed characters it just does a linefeed as desired. Putting in multiple vbCrLf characters does not seem to work as well. I would hazard an educated guess that they are stripped down to a single newline character by Outlook. It certainly appears that way.

I've programmed VB6 since 1998 so I don't mind looking over that code. ;)

P.S. I've increased the points as this is not as easy as I thought it should be. I thought it would be something simple like the AcceptsReturn property.
0
 
KitsuneAuthor Commented:
rrbecker,

If you are looking more closely at the code, then replace the following

 'set our linebreak
 linebreak = txtLineBreak.Text

with

 'set our linebreak
 linebreak = vbLf & vbLf

This line is the one that I have tried lots of variations. I was doing a small test in my code at the time of posting.
0
 
KitsuneAuthor Commented:
rrbecker,

Your example VB6 code made sense but was dealing with the exact opposite situation. You are extracting those characters while I need to insert them without having them display as a square box in my textbox.

I'll do some more manual tests and post back.
0
 
KitsuneAuthor Commented:
Problem has been solved.

I did have to use vbCrLf but I had to use it with the AcceptsReturn property set to True.

I had been testing all of the vbCrLf, vbLf, VBCr, etc. codes BEFORE I changed that property. AFTER I changed that property I had been just using vbLf.

I am awarding the points to rrbecker for his help.
0
 
rrbeckerCommented:
Hi Kitsune

Sorry I wasn't around for your earlier responses, it was getting late for me and I went to bed.

I am glad you solved your problem and I am glad that I was at least somewhat helpful in resolving the issue. Thank you very mich for the points and grade.

Thanks again,

rrbecker
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now