Results 1 to 30 of 30

Thread: Characters textbox line

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Characters textbox line

    How can I only allow 35 charachters per line in a text box? I am making a program for the user to enter data and then hit a button to copy that data and will be pasted into a program called Seagull and it only allows 40 characters per line and it does not wrap. I have to hit enter or tab twice to go to the next line

    what about something like this i found? I am not exactly sure what it means but i guess it is supposed to work


    Code:
    Function FormatByLength(Expression As String, Length As Long) _
       As String
    
       Dim BufferCrLf() As String
       Dim BufferSpace() As String
       Dim Buffer As String
       Dim k As Long
       Dim j As Long
       Dim count As Long
    On Error GoTo FormatByLengthError
       BufferCrLf() = Split(Expression, vbCrLf)
       For k = 0 To UBound(BufferCrLf())
           If Len(BufferCrLf(k)) <= Length Then
              Buffer = Buffer & BufferCrLf(k) & vbCrLf
           Else
              BufferSpace() = Split(BufferCrLf(k), " ")
              For j = 0 To UBound(BufferSpace())
                  count = count + Len(BufferSpace(j)) + 1
                  If (count <= Length) Then
                     Buffer = Buffer & BufferSpace(j) & " "
                  Else
                     count = 0
                     Buffer = Buffer & vbCrLf & BufferSpace(j) & " "
                     count = Len(BufferSpace(j)) + 1
                  End If
              Next j
              Buffer = Buffer & vbCrLf
           End If
       Next k
       FormatByLength = Buffer
       Exit Function
    FormatByLengthError:
        
    End Function
    or


    Code:
    textbox2.Text = FormatByLength(textbox2.Text, 72)

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Characters textbox line

    you could set the maxlength property of the textbox to 35
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    actually that is only to set the max for thetexbox limit total. I am looking for a way to set it per line in the textbox before the cursor moves to the next line.

  4. #4
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Characters textbox line

    Here's a little something I made.
    Code:
    Private Function TrimString(Text As String, ML As Long) As String
    On Error Resume Next
    TrimString = ""
    Dim TextLines() As String
    Dim Lines As Long
    For i = 1 To Len(Text)
        If (Mid(Text, i, 2) = vbCrLf) Then Lines = Lines + 1
    Next i
    TextLines() = Split(Text, vbCrLf)
    For i = 0 To Lines + 1
        If (i <> Lines) Then
            TrimString = TrimString & Left(TextLines(i), ML) & vbNewLine
        Else
            TrimString = TrimString & Left(TextLines(i), ML)
            Exit For
        End If
    Next i
    End Function
    You can use it like this:
    Code:
    Private Sub Command1_Click()
    Text1.Text = TrimString(Text1.Text, 35)
    End Sub
    It goes through every line and cuts off how many letters you want. And then returns it in the function. Simple really.. I don't know if it's what you really want...
    Last edited by Vanasha; Sep 9th, 2007 at 11:17 AM.

  5. #5
    Hyperactive Member
    Join Date
    Dec 1999
    Posts
    321

    Re: Characters textbox line

    You could also use this in the Text1_Change() procedure.

    Code:
    Private Sub Text1_Change()
    Text1.Text = TrimString(Text1.Text, 35)
    Text1.SelStart = Len(Text1)
    End Sub
    This would force the user to press enter to keep typing after reaching 35 chars on a single line.
    Signed, Rodik ([email protected])
    Programmer,usesVB6ED
    ===========================
    Copyright©RodikCo,2002.

    Dont mind this signature ;] Its old

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    but is there a way to do it automatically?

  7. #7
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Characters textbox line

    Yes, my code does that.
    It goes through and chops off the letters that cross your barrier.
    Just before doing the command that does whatever, put this line in.
    Code:
    Text1.Text = TrimString(Text1.Text, 35)
    Quote Originally Posted by Vanasha
    Sorry, i'm slow.


  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    I have a problem with it i get error
    compile error - expected end sub


    Code:
    Private Function TrimString(Text As String, ML As Long) As String
    On Error Resume Next
    TrimString = ""
    Dim TextLines() As String
    Dim Lines As Long
    For i = 1 To Len(Text)
        If (Mid(Text, i, 2) = vbCrLf) Then Lines = Lines + 1
    Next i
    TextLines() = Split(Text, vbCrLf)
    For i = 0 To Lines + 1
        If (i <> Lines) Then
            TrimString = TrimString & Left(TextLines(i), ML) & vbNewLine
        Else
            TrimString = TrimString & Left(TextLines(i), ML)
            Exit For
        End If
    Next i
    End Function
    
    
    
    Private Sub CopyBtn_Click()
    CommentsTxt.Text = TrimString(Text1.Text, 35)
    
      Dim strCopy As String
      Clipboard.Clear
      If cpniyeschk.Value = vbChecked Then
      strCopy = "CPNI Yes - " & Timebox.Text & vbCrLf
      End If
      If cpninochk.Value = vbChecked Then
      strCopy = "CPNI No - " & Timebox.Text & vbCrLf
      End If
      strCopy = strCopy & Nametxt.Text & vbCrLf
      strCopy = strCopy & CommentsTxt.Text
      Clipboard.SetText strCopy, vbCFText
      MsgBox strCopy, , "This Will Be Pasted"
      
      End Function

  9. #9
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Characters textbox line

    Code:
    Private Sub CopyBtn_Click()
    CommentsTxt.Text = TrimString(Text1.Text, 35)
    
      Dim strCopy As String
      Clipboard.Clear
      If cpniyeschk.Value = vbChecked Then
      strCopy = "CPNI Yes - " & Timebox.Text & vbCrLf
      End If
      If cpninochk.Value = vbChecked Then
      strCopy = "CPNI No - " & Timebox.Text & vbCrLf
      End If
      strCopy = strCopy & Nametxt.Text & vbCrLf
      strCopy = strCopy & CommentsTxt.Text
      Clipboard.SetText strCopy, vbCFText
      MsgBox strCopy, , "This Will Be Pasted"
      
      End Function
    It's a sub not a function. Replace it with "End Sub".
    Quote Originally Posted by Vanasha
    Sorry, i'm slow.


  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    i changed it but it only copies the first line from commentstxt.text


    Code:
    private Sub CopyBtn_Click()
    CommentsTxt.Text = TrimString(CommentsTxt.Text, 35)
    
      Dim strCopy As String
      Clipboard.Clear
      If cpniyeschk.Value = vbChecked Then
      strCopy = "CPNI Yes - " & Timebox.Text & vbCrLf
      End If
      If cpninochk.Value = vbChecked Then
      strCopy = "CPNI No - " & Timebox.Text & vbCrLf
      End If
      strCopy = strCopy & Nametxt.Text & vbCrLf
      strCopy = strCopy & CommentsTxt.Text
      Clipboard.SetText strCopy, vbCFText
      MsgBox strCopy, , "This Will Be Pasted"
      
      End Sub
    Last edited by percussiontech; Sep 10th, 2007 at 10:54 AM.

  11. #11
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Characters textbox line

    Hmm, thats odd. It works for me. You haven't messed with the function at all..?
    Perhaps it's something to do with all of those other values you're adding on.
    Quote Originally Posted by Vanasha
    Sorry, i'm slow.


  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    ok this is my entire code: maybe this will help you?

    Code:
    Private Function TrimString(Text As String, ML As Long) As String
    On Error Resume Next
    TrimString = ""
    Dim TextLines() As String
    Dim Lines As Long
    For i = 1 To Len(Text)
        If (Mid(Text, i, 2) = vbCrLf) Then Lines = Lines + 1
    Next i
    TextLines() = Split(Text, vbCrLf)
    For i = 0 To Lines + 1
        If (i <> Lines) Then
            TrimString = TrimString & Left(TextLines(i), ML) & vbNewLine
        Else
            TrimString = TrimString & Left(TextLines(i), ML)
            Exit For
        End If
    Next i
    End Function
    
    Private Sub ClearBtn_Click()
    Me.cpniyeschk = False
    Me.cpninochk = False
    Me.Nametxt = ""
    Me.CommentsTxt = ""
    Me.Timebox = ""
    Clipboard.Clear
    End Sub
    
    Private Sub CopyBtn_Click()
    CommentsTxt.Text = TrimString(CommentsTxt.Text, 35)
    
      Dim strCopy As String
      Clipboard.Clear
      If cpniyeschk.Value = vbChecked Then
      strCopy = "CPNI Yes - " & Timebox.Text & vbCrLf
      End If
      If cpninochk.Value = vbChecked Then
      strCopy = "CPNI No - " & Timebox.Text & vbCrLf
      End If
      strCopy = strCopy & Nametxt.Text & vbCrLf
      strCopy = strCopy & CommentsTxt.Text
      Clipboard.SetText strCopy, vbCFText
      MsgBox strCopy, , "This Will Be Pasted"
      
      End Sub
    
      
      
    Private Sub cpninochk_Click()
    If cpninochk.Value = vbChecked Then
    cpniyeschk.Value = False
    End If
    End Sub
    
    Private Sub cpniyeschk_Click()
    If cpniyeschk.Value = vbChecked Then
    cpninochk.Value = False
    End If
    End Sub
    
    Private Sub Picture1_Click()
    
    End Sub
    
    Public Function MyTime() As String
      MyTime = Format(Now, "MM-DD-yy HHmm")
    End Function
    
    Private Sub Time_Click()
    Timebox.Text = MyTime & " Hrs"
    End Sub

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    ok I changed the code a little am still getting error sub or function not defined. I added at nother textbox to pull all other info into that one and format that one to 35 charachters per line. can I pm you my project and you can look at it?

  14. #14
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Characters textbox line

    I suggest you remove the On Error Resume Next or you wont debug the error. Does that line of code really needed? Come to think of it, VB is an a** sometimes.

  15. #15
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Characters textbox line

    You sure can pm me your project. I'll try editing my code when I see what error it's coming out with.
    Quote Originally Posted by Vanasha
    Sorry, i'm slow.


  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    Ok I loaded the form for everyone to look at and test
    Last edited by percussiontech; May 27th, 2008 at 11:46 PM.

  17. #17
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Characters textbox line

    That will not help him. Let him remove that block of code and post the offending line of code. Let us start with good habits.

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    what block of code?

  19. #19
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Characters textbox line

    On Error Resume Next.

  20. #20
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Characters textbox line

    Ok, sorry to be a bother. But can you please explain to me what you actually want to do in your program?
    I don't understand what you want to do..?

    Quote Originally Posted by zynder
    On Error Resume Next.
    Sorry, i'm not really experienced myself.
    Code:
    Private Function TrimString(Text As String, ML As Long) As String
    TrimString = ""
    Dim TextLines() As String
    Dim Lines As Long
    For i = 1 To Len(Text)
        If (Mid(Text, i, 2) = vbCrLf) Then Lines = Lines + 1
    Next i
    TextLines() = Split(Text, vbCrLf)
    For i = 0 To Lines + 1
        If (i <> Lines) Then
            TrimString = TrimString & Left(TextLines(i), ML) & vbNewLine
        Else
            TrimString = TrimString & Left(TextLines(i), ML)
            Exit For
        End If
    Next i
    End Function
    Ok i've tested TrimString on a block of text with a 35 ML, etc. And it works perfectly. So the error lies within the project.
    Last edited by Vanasha; Sep 10th, 2007 at 02:48 PM.
    Quote Originally Posted by Vanasha
    Sorry, i'm slow.


  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    ok I am trying to click the time button to dispay the current date and time in the timetxt.text box t
    then user clicks the
    cpniyeschk.checkbox or cpninochk.checkbox
    then the user enters the callers name into the
    nametxt.text box
    then the user enters the call comments into the
    commentstxt.text box

    then i have a button that copies the informtation into the
    text1.text box
    and to the clipboard so that i can copy it to another program

    however the text1.text box needs to be formated so that there is no more that 35 characters per line
    so the code that i have is as follows:

    Code:
    Private Function TrimString(Text As String, ML As Long) As String
    
    TrimString = ""
    Dim TextLines() As String
    Dim Lines As Long
    For i = 1 To Len(Text)
        If (Mid(Text, i, 2) = vbCrLf) Then Lines = Lines + 1
    Next i
    TextLines() = Split(Text, vbCrLf)
    For i = 0 To Lines + 1
        If (i <> Lines) Then
            TrimString = TrimString & Left(TextLines(i), ML) & vbNewLine
        Else
            TrimString = TrimString & Left(TextLines(i), ML)
            Exit For
        End If
    Next i
    End Function
    
    Private Sub ClearBtn_Click()
    Me.cpniyeschk = False
    Me.cpninochk = False
    Me.Nametxt = ""
    Me.CommentsTxt = ""
    Me.timetxt = ""
    Clipboard.Clear
    End Sub
    
    Private Sub CopyBtn_Click()
    Text1.Text = TrimString(Text1.Text, 35)
       If cpniyeschk.Value = vbChecked Then
      Text1.Text = "CPNI Yes - " & timetxt.Text & vbCrLf
      End If
      If cpninochk.Value = vbChecked Then
      Text1.Text = "CPNI No - " & timetxt.Text & vbCrLf
      End If
      Text1.Text = Nametxt.Text & vbCrLf
      Text1.Text = CommentsTxt.Text
      Clipboard.SetText vbCFText
      MsgBox strCopy, , "This Will Be Pasted"
      End Sub
      
    Private Sub cpninochk_Click()
    If cpninochk.Value = vbChecked Then
    cpniyeschk.Value = False
    End If
    End Sub
    
    Private Sub cpniyeschk_Click()
    If cpniyeschk.Value = vbChecked Then
    cpninochk.Value = False
    End If
    End Sub
    
    Private Sub Picture1_Click()
    
    End Sub
    
    Public Function MyTime() As String
      MyTime = Format(Now, "MM-DD-yy HHmm")
    End Function
    
    Private Sub Time_Click()
    timetxt.Text = MyTime & " Hrs"
    End Sub
    Last edited by percussiontech; May 27th, 2008 at 11:46 PM.

  22. #22
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Characters textbox line

    Ok I fixed it! And I made it easier for me to understand a little.
    Attached Files Attached Files
    Quote Originally Posted by Vanasha
    Sorry, i'm slow.


  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    ok as i thought if you run the program and tyope into the commentstxt.txt box you can type fore ever but as soon as you click the copy button it cuts off the commentstxt.text box at 35 characters on the first line and only takes that and puts it into the text1.text box. also the commentstxt.text box field needs to have multiline enabled like it was and when the copy button is cliked it needs to copy all the information from timebox.text. cpniyeschk checkbox, cpninochk checkbox, name checkbox and commentstxt text box.

  24. #24
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Characters textbox line

    Quote Originally Posted by percussiontech
    ok as i thought if you run the program and tyope into the commentstxt.txt box you can type fore ever but as soon as you click the copy button it cuts off the commentstxt.text box at 35 characters on the first line and only takes that and puts it into the text1.text box. also the commentstxt.text box field needs to have multiline enabled like it was and when the copy button is cliked it needs to copy all the information from timebox.text. cpniyeschk checkbox, cpninochk checkbox, name checkbox and commentstxt text box.
    Can you please explain more in detail what you need help for?

    1) All of the text boxes have multi-line enabled.
    2) So you only want it to be cut on the first line?
    3) If i'm not mistaken; I didn't remove any of your code, if you added the code it should still be there.

    If you're a beginner then you should start on smaller projects.

    Here's the code for automaticially cutting off the text.

    Code:
    Private Sub CommentsTxt_Change()
    Dim Temp As Integer
    Temp = CommentsTxt.SelStart
    CommentsTxt.Text = TrimString(CommentsTxt.Text, 35)
    CommentsTxt.SelStart = Temp
    End Sub
    Last edited by Vanasha; Sep 11th, 2007 at 10:47 AM.
    Quote Originally Posted by Vanasha
    Sorry, i'm slow.


  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    ok the user need to be able to type in the commentstxt text box and when the user gets to 35 characters it needs to go to the next line - then when the user clicks the copy button all fields need to copy to text1.text and/or the clip board as follows:

    cpniyeschk or cpninochk
    nametxt.text
    commentstxt.text (being multiple lines at 35 characters per line)

    this is so i can paste it into another program that only allows 40 characters per line and then i have to hit enter to go to the next line
    Last edited by percussiontech; Sep 11th, 2007 at 11:11 AM.

  26. #26
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Characters textbox line

    Replace the current "Private Sub CopyBtn_Click()" with this.
    Code:
    Private Sub CopyBtn_Click()
    CommentsTxt = TrimString(CommentsTxt, 35)
    If cpniyeschk.Value = vbChecked Then
        Text1.Text = "CPNI Yes - " & timetxt.Text & vbCrLf
    End If
    If cpninochk.Value = vbChecked Then
        Text1.Text = "CPNI No - " & timetxt.Text & vbCrLf
    End If
    Text1.Text = Text1.Text & Nametxt.Text & vbCrLf & CommentsTxt.Text
    Clipboard.SetText Text1.Text
    MsgBox Text1.Text, , "This Will Be Pasted"
    End Sub
    How it works:
    It first trims the comment.
    It just adds lines and the information you want added. With lines accordingly.
    Then sets the clipboard data to the whole data collected.
    Quote Originally Posted by Vanasha
    Sorry, i'm slow.


  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    ok i did that and it copies the timetxtx and the nametxt and the first line of the comments txt but when i type in the commentstxt it does not go tot he next line after 35 character - it will allow me to type any amount of characters on the first line and does not ever go to any other lines and when i click copy it cuts everything off after 35 characters,

    also although it copies to the text1.text box it does not put it on the clipboard to paste anywhere

  28. #28
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Characters textbox line

    So everything works apart from the clipboard..?
    It should work. >_<
    Quote Originally Posted by Vanasha
    Sorry, i'm slow.


  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Characters textbox line

    no besides the clipboard not working
    if you type in the commentstxt box as you type all the characters stay on the same line and never wraps

    also when i go to copy, because of the above mentioned problem, i only get the 1st 35 characters of the comments box paseted into the text1 box

  30. #30
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Characters textbox line

    Try it yourself.
    Quote Originally Posted by Vanasha
    Sorry, i'm slow.


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