Results 1 to 16 of 16

Thread: How to wrap to a text file? (RESOLVED)

  1. #1

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200

    How to wrap to a text file? (RESOLVED)

    How to wrap a 240 length textbox to a 80 length line and print it on a text file?

    Thanks.
    Last edited by AlvaroF1; Sep 22nd, 2004 at 10:20 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Something like this to limit the lines in a multi-line textbox on a line
    by line basis.


    CodeBank
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    Can't you just use multi-line?
    I know the textbox's have a basic word-wrap built in if you have some certain properties enabled.

    A wrapped box, may not save wrapped thought
    It may put the NULL character at places where the linefeed is.
    NULL character is that blank rectangle u see now and then.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    No you cant use multi-line alone because the poster wants to
    limit each lines length to 80 characters. The code allows the enter
    key to make the line breaks. Then you can output to a text file
    like so...(note: this ex. is limited to 10 chrs per line).

    VB Code:
    1. Option Explicit
    2.  
    3. 'API Declarations:
    4. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    5.   (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    6.   lParam As Any) As Long
    7.  
    8. 'Constant Delcarations
    9. Private Const EM_LINEINDEX = &HBB
    10. Private Const EM_LINELENGTH = &HC1
    11.  
    12. Private Sub Command1_Click()
    13.  
    14.     Dim i As Integer
    15.     Dim ar() As String
    16.    
    17.     ar = Split(Text1.Text, vbKeyReturn)
    18.     Open "C:\Test.txt" For Output As #1
    19.         For i = 0 To UBound(ar)
    20.             Print #1, ar(i)
    21.         Next
    22.     Close #1
    23.    
    24. End Sub
    25.  
    26. Private Sub Text1_KeyPress(KeyAscii As Integer)
    27.     Dim lLen As Long
    28.  
    29.     'Get the length of the current line:
    30.     lLen = SendMessage(Text1.hwnd, EM_LINELENGTH, EM_LINEINDEX, 0&)
    31.    
    32.     'If length is 10, don't allow more characters:
    33.     If lLen = 10 And KeyAscii <> vbKeyReturn And KeyAscii <> vbKeyBack Then
    34.         KeyAscii = 0
    35.     End If
    36.  
    37. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    not true. blank rects depend on the font

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Actually the little squares represent an "Un-Supported" character.
    Depending on either the font, encoding, or the actual character.

    A backspace character of ASCII 8 is shown as a square because it
    can't be displayed as a visual character.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    Well so my assumtion of it being at least a null character is fairly true because it is techinally nothing..or error.

    Thanks for the explanation though, I did not know that.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    No prob.

    I hate those little squares!
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482
    What I would do is take the whole string, read 80 characters, back up to the nearest space so you don't split a word and replace that space witha vbCrLF (vb Carriage Return/Line Feed) character and repeat until you reach the end of the string. Then write the whole string to the text file.

    If I have time I will update this post later with some code.

    The Mav

  10. #10

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    Originally posted by Maverickz
    What I would do is take the whole string, read 80 characters, back up to the nearest space so you don't split a word and replace that space witha vbCrLF (vb Carriage Return/Line Feed) character and repeat until you reach the end of the string. Then write the whole string to the text file.

    If I have time I will update this post later with some code.

    The Mav
    I think you are right. I wrote a code but it isn't working correctly:

    VB Code:
    1. Dim i As Integer
    2. Select Case Len(Text1)
    3.     Case Is <= 80
    4.         Print #1, Text1
    5.     Case Is <= 160
    6.         For i = 80 To 1 Step -1
    7.             If Mid$(Text1, i, 1) = " " Then
    8.                 Print #1, Left$(Text1, i - 1)
    9.                 Print #1, Mid$(Text1, i + 1, Len(Text1))
    10.                 Exit For
    11.             End If
    12.         Next i
    13.     Case Else
    14.         Dim z As Integer
    15.         For i = 80 To 1 Step -1
    16.             If Mid$(Text1, i, 1) = " " Then
    17.                 Print #1, Left$(Text1, i - 1)
    18.                 z = i + 1
    19.                 Exit For
    20.             End If
    21.         Next i
    22.         For i = 160 To 81 Step -1
    23.             If Mid$(Text1, i, 1) = " " Then
    24.                 Print #1, Mid$(Text1, z, i - 81)
    25.                 Print #1, Mid$(Text1, i + 1, 80)
    26.                 Exit For
    27.             End If
    28.         Next i
    29. End Select

  11. #11
    you cant use a select case you need to use a loop and midstr.

  12. #12
    VB Code:
    1. Dim I As Long
    2.  
    3. For I = 0 to Len(Text1.Text) Step 80
    4.  Mid$(Text1.Text, I, I + 1) = vbCrLf
    5. Next I

  13. #13
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    nareth, besides errorneus behavior, that wouldn't work: you can't do Mid$(Text1.Text, I, I + 1) = vbCrLf, because you can do like that only to a variable, not a property of a textbox.

  14. #14
    i forgot :P

    dont mind me im feeling sick

  15. #15
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Hmm... I just remembered: you can use DrawTextW so that it won't draw the text, but instead looks for the size of it. You can also limit the width it draws to and I believe it might even be able to alter the string so that it adds the linechanges. Thus you could do it with one API call (if you use a fixed width font, such as FixedSys). It wouldn't work if there would be lines with too long continuous content, like 90 = characters one after another.

    If someone wants to, I can take a look if it can do it.

  16. #16

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200

    Smile RESOLVED!

    VB Code:
    1. Private Function QuebrarTexto(ByVal Texto As String, ByVal Tamanho As Integer) As String
    2.     Tamanho = Tamanho + 1
    3.     Texto = Trim$(Texto)
    4.     Dim MeioEnter As String, Enter As String, NovaLinha As String, l As Integer, s As String, c As String, NovoTexto As String
    5.     MeioEnter = Chr$(13)
    6.     Enter = Chr$(13) & Chr$(10)
    7.     Do
    8.         l = Len(NovaLinha)
    9.         s = InStr(Texto, " ")
    10.         c = InStr(Texto, MeioEnter)
    11.         If c Then
    12.             If l + c <= Tamanho Then
    13.                 NovoTexto = NovoTexto & NovaLinha & Left$(Texto, c)
    14.                 NovaLinha = ""
    15.                 Texto = Mid$(Texto, c + 1)
    16.                 GoTo Loopar
    17.             End If
    18.         End If
    19.         If s Then
    20.             If l + s <= Tamanho Then
    21.                 NovaLinha = NovaLinha & Left$(Texto, s)
    22.                 Texto = Mid$(Texto, s + 1)
    23.             ElseIf s > Tamanho Then
    24.                 NovoTexto = NovoTexto & Enter & Left$(Texto, Tamanho)
    25.                 Texto = Mid$(Texto, Tamanho + 1)
    26.             Else
    27.                 NovoTexto = NovoTexto & NovaLinha & Enter
    28.                 NovaLinha = ""
    29.             End If
    30.         Else
    31.             If l Then
    32.                 If l + Len(Texto) > Tamanho Then
    33.                     NovoTexto = NovoTexto & NovaLinha & Enter & Texto & Enter
    34.                 Else
    35.                     NovoTexto = NovoTexto & NovaLinha & Texto & Enter
    36.                 End If
    37.             Else
    38.                 NovoTexto = NovoTexto & Texto & Enter
    39.             End If
    40.             Exit Do
    41.         End If
    42. Loopar:
    43.     Loop
    44.     QuebrarTexto = NovoTexto
    45. End Function
    46.  
    47. Private Sub Command1_Click()
    48.     Open "C:\MeuArquivo.txt" For Output As #1
    49.         Print #1, QuebrarTexto(Text1, 80)
    50.     Close #1
    51. 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