Page 1 of 1

Linebreak in TextBox

Posted: 14 May 2019, 18:52
by hermie
Hello!

In Microsoft Word, a line break is defined by Chrw(13).

In order to show a line break in your textbox control, it's however necessary to have Chrw(13) + Chrw(10).

Else, no linebreak will be displayed.

I imagined I should play around with "SoftLineBreak" in order to provoke / show a linebreak at Chrw(13) only, but that wouldn't help.

Can anybody tell me how I could make it so that Chrw(13) is shown as a line break?

I wouldn't want to alter the text (for example by replacing Chrw(13) with Chrw(13) + Chrw(10)), as I don't think this would be the correct way to handle this.

Thank you for the help!

Re: Linebreak in TextBox

Posted: 17 May 2019, 19:40
by TiKu
Which version of Word do you use?
I can test only with Word 2010. I open Word, write "Line 1", press Enter, write "Line 2". Then I select all text, press Ctrl+C, go to a VB6 form with my Unicode TextBox on it (with the default properties, only Multiline set to True), set the focus to this text box and press Ctrl+V. It displays "Line 1" and "Line 2" on separate lines, just as expected.

Am I missing anything?

Best regards
TiKu

Re: Linebreak in TextBox

Posted: 04 Sep 2019, 12:19
by hermie
Yes, you're missing something. :-)

I'm using Word 2007.

I'm using the following code to get the text:

Code: Select all

Private Function GetText(ByVal uFile As String) As String

        Dim nWord As Object ' Word.Application
        Set nWord = CreateObject("Word.Application")
        
        'set some settings/properties
        With nWord
            .Visible = False
            .DisplayAlerts = 0   'wdAlertsNone
        End With
        
        'open the document
        Dim nDoc  As Object 'Word.document
        Set nDoc = nWord.Documents.Open(uFile)
        
        If nDoc Is Nothing Then
            Debug.Assert False
            'unable to get the document contents
            'handle this condition here...
        Else
            'read the contents from the Doc object
            GetText = nDoc.Content
        End If
        
        nDoc.Close
        Set nDoc = Nothing
        nWord.Quit False

        Set nWord = Nothing

End Function
If you get the text this way, the line break is represented by Chrw(13) only, not by vbCrLf.

Re: Linebreak in TextBox

Posted: 04 Sep 2019, 14:56
by hermie
I thought that "InsertSoftLinebreaks" would achieve what I want, but obviously not.
I can replace Chrw(13) with vbNewLine of course, but I thought that you would perhaps handle this case.

Re: Linebreak in TextBox

Posted: 04 Sep 2019, 20:27
by TiKu
Ok, I see. I cannot find a better solution other than parse the text and replace any vbCr. I could implement this in the control, but why? I would do the same as you currently already do in VB6. Unfortunately the Windows Edit control API does not seem to have any built-in feature that we could use here.

Re: Linebreak in TextBox

Posted: 05 Sep 2019, 09:59
by hermie
Thank you! Yes, I totally agree.
I have checked the .NET textbox control, and it behaves just like that as well.

Re: Linebreak in TextBox

Posted: 11 Sep 2019, 20:46
by TiKu
Maybe I have found something. Starting with Windows 10 1809 it seems to be possible to configure the native edit control in a way that allows a single CR or a single LF for line breaks.