Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit the characters in a multiline textbox

Status
Not open for further replies.

stephenmbell

IS-IT--Management
Jan 7, 2004
109
US
I am writing an application that has a multiline textbox on a form.

I am looking to make this textbox not be able to have any lines of text in it that are longer than 22 characters. The number of lines does not really matter. I turned off wordwrap and do now show any scroll bars -

how would I accomplish this?


thanks in advance
sb
 

The way I understand stephenmbell question - it needs to do this in a text box:
Code:
123456789 123456789 12
1234 678901 3456 89012
abc def ghj
12 456789012 45678 012
...
in other words, kind of word wrap with no more than 22 charactes in any line.

Have fun.

---- Andy
 
That is correct, I do not want to limit the number of rows of text in the text box, but I would like to limit the "width" or columns to 22 characters.

Would this maxlength property work? or would this limit my total length of the string inside the box to 22 chars?

thanks in advance
sb
 
I don't think the maxlength would help in this case. BUT...without any other information, it seems that you are separating the lines into records of some sort. It might be a lot easier to use a different control for this, such as a DataGridView. Instead of having the user type and line and press enter, just have them create records with a max length of 22 for the column--using a control that was designed for that type of data entry.
 
What I am attempting to accomplish is a vb app that will create a specially formatted text file to be printed on a special printer (similar to receipt paper - hence the 22 char width restriction)

So I have a multi-line text box on my form, and the user types in the text they would like to be printed:

"This is my text, and I want this to print."

(minus the quotes)

I want it to print on the receipt paper:

"This is my text, and I"
"want this to print."

(minus the quotes)

Thanks.

sb
 
typical management I guess, why didn't you say what you wanted in the original post?

the textbox can easily be restricted by having some code in the keypressed event.

something like

for i as integer = 0 to me.textbox1.lines.count
if me.textbox1.lines(i).length > 22 then
'some message here
end if
next

but better still is to let the user just enter the text and then format it for printing by dividing the legth of the text by 22 or something more fancy.

Christiaan Baes
Belgium

My Blog
 
You can also use the split function with the tab character as the delimeter, go through the array and dump elements longer than 22, then clear the textbox & reload with the remaining data in the array.

"Teamwork means never having to take all the blame yourself."
 
Code:
    Private Sub TextBox3_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox3.KeyDown
        Dim returns As Integer = CountReturns(TextBox3.Text)
        Dim x As Integer = TextBox3.Text.Length - returns
        Dim y As Integer = (TextBox3.Text.Length / 22)
        Dim z As Integer = (y * 22) + returns

        If z = x And x > 0 Then
            TextBox3.Text &= vbCrLf
            TextBox3.Select(TextBox3.Text.Length + 1, 0)
            TextBox3.SelectionLength = 0
        End If
    End Sub

    Private Function CountReturns(ByVal text As String) As Integer
        text = text.Replace(vbCrLf, vbCr)
        Dim chrAry As Char() = text.ToCharArray
        Dim cChr As Char
        Dim TotalReturns As Integer

        For Each cChr In chrAry
            If cChr = vbCr Then TotalReturns += 1
        Next

        Return TotalReturns
    End Function
There is likely another way to do this, but this is a close as I can get with the time I have. I don't have the time to figure out the rest of the math on this one. Right now this will work till you have 4 or 5 lines and then it allows 44 characters per line. I'm sure at 8 or 10 it would then be 66 (or 88).

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
In case you don't know you can get the number of lines with:
Code:
TextBox3.Lines.Length

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Oh and I agree with chrissie1 and you should really just format on the print.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Idea 1: Make the text wrap and set the font size carefully so it wraps at 22 characters. Let the user whack away. When they click submit, split every CR-terminated segment at every 22 characters and present it back to the user for confirmation.

Idea 2: make separate 22-character single-line text boxes that handle cursor keys and carriage returns appropriately. If in the last box and CR is pressed, add a new text box to the DOM tree.
 
What about
Code:
        Dim str2Return As String = ""
        Dim temp As String = TextBox1.Text
        While True
            If temp.Length > 22 Then
                str2Return &= Mid(temp, 1, 22) & Environment.NewLine
                temp = Mid(temp, 23)
            Else
                str2Return &= temp
                Exit While
            End If
        End While
        MsgBox(str2Return)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top