-
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)
-
Re: Characters textbox line
you could set the maxlength property of the textbox to 35
-
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.
-
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...
-
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.
-
Re: Characters textbox line
but is there a way to do it automatically?
-
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)
-
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
-
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".
-
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
-
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.
-
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
-
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?
-
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.
-
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. ;)
-
Re: Characters textbox line
Ok I loaded the form for everyone to look at and test
-
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.
-
Re: Characters textbox line
-
Re: Characters textbox line
-
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. ;)
-
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
-
1 Attachment(s)
Re: Characters textbox line
Ok I fixed it! And I made it easier for me to understand a little. :)
-
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.
-
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
-
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
-
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.
-
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
-
Re: Characters textbox line
So everything works apart from the clipboard..?
It should work. >_<
-
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
-
Re: Characters textbox line