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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limiting number of lines in a text box 3

Status
Not open for further replies.

Asspin

Technical User
Jan 17, 2005
155
US
Ok all, I have a text box. It cannot have more then 6 lines in it. I tried setting the max size to 468 charecters, as it can have up to 78 per line. However based on how long words are, the will drop the text to the next line (good), but then it can take it down to 7 lines (bad). Does anyone know how to fix this?
 
You can get the linecount using the EM_GETLINECOUNT message.



Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
How would I use that, I don't see it in VB help.
 
Try this:

Option Explicit
Private Const EM_GETLINECOUNT = &HBA
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private strOriginalTest As String

Private Sub Text1_Change()
Dim intLineCount As Long
intLineCount = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, ByVal 0&)
If intLineCount > 6 Then
Text1.Text = strOriginalTest
Text1.SelStart = Len(Text1.Text)
Else
strOriginalTest = Text1.Text
End If
End Sub



Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Or maybe even...
Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub Text1_Change()
  Dim x As Integer, y As Integer
  x = Text1.SelStart
  y = Text1.SelLength
  While SendMessage(Text1.hwnd, &HBA, 0, ByVal 0&) > 6
    Text1.Text = Left(Text1.Text, Len(Text1.Text) - IIf(Right(Text1.Text, 2) = vbCrLf, 2, 1))
  Wend
  Text1.SelStart = x
  Text1.SelLength = y
End Sub

Which will trim the end of the string to 6 lines, including pasted text, checks for VbCrLf's, and restores the cursor position.

*Note* Derived from DrJavaJoe's example...
I've never used that before... I'll give you a star... ;-)

Hope This Helps,
-Josh

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Ok this is what I have currently. I am getting the following error on the highlighted area.

Compile Error: Method or data member not found.

Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub txtMessage_Change()
    Dim x, y, intLineCount As Integer
    x = txtMessage.SelStart
    y = txtMessage.SelLength
    If txtMessage.TextLength > 0 Then
        cmdAdd.Enabled = True
        cmdSend.Enabled = True
    Else
        cmdAdd.Enabled = False
        cmdSend.Enabled = False
    End If
    While SendMessage(txtMessage[highlight].hwnd[/highlight], &HBA, 0, ByVal 0&) > 6
        txtMessage.Text = Left(txtMessage.Text, Len(txtMessage.Text) - IIf(Right(txtMessage.Text, 2) = vbCrLf, 2, 1))
    Wend
    txtMessage.SelStart = x
    txtMessage.SelLength = y
End Sub
 
Ahhhh, I see a TextLength property, let me guess .net?
This is the VB5/6 forum try forum796.



Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
I see, that is a VBA control or part of the Forms 2.0 library are you doing this in the Visual Basic IDE or an Office application such as Access or Excel?



Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Well the EM_GETLINECOUNT solution isn't going to work. You could go back to your original solution of setting the maxlength to 468 and switching your font to a fixed width font such as Courier New.



Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
It will still drop to the next line unless the words fit 78 letters per line. Is there any other way to do this? Maybe by adding some references?
 
On a side note... (Just FYI)

Code:
Dim [b]x, y,[/b] intLineCount As Integer

Only declares intLineCount as an integer...
x & y are both declared as Variants, and is the same as not declaring them at all...

By default, the above statment is interpreted as:
Code:
Dim [b]x As Variant, y As Variant,[/b] intLineCount As Integer

To test this...
You cannot assign a string to an integer...
So, with a simple textBox (Text1) and a Button (Command1) try this code:
Code:
Private Sub Command1_Click()
  Dim x, y As Integer
  x = "hello"
  Text1 = x
End Sub

where this will raise an error:
Code:
Private Sub Command1_Click()
  Dim x, y As Integer
[COLOR=black yellow]  y = "hello"[/color]
  Text1 = y
End Sub

To Assign the types correctly, you must specify each type...
Code:
Dim x [b]As Integer[/b], y [b]As Integer[/b], intLineCount As Integer

ON THE OTHER HAND...

You can use DefInt A-Z to set the default type to Integer, then you only have to say:
Code:
[b]DefInt A-Z[/b]
Sub Blah()
  Dim X, Y
  ...
End Sub

which is the equivalent of:
Code:
Sub Blah()
  Dim X [b]As Integer[/b], Y [b]As Integer[/b]
  ...
End Sub

variations of this include:
DefBool letterrange[, letterrange] . . .
DefByte letterrange[, letterrange] . . .
DefInt letterrange[, letterrange] . . .
DefLng letterrange[, letterrange] . . .
DefCur letterrange[, letterrange] . . .
DefSng letterrange[, letterrange] . . .
DefDbl letterrange[, letterrange] . . .
DefDec letterrange[, letterrange] . . .
DefDate letterrange[, letterrange] . . .
DefStr letterrange[, letterrange] . . .
DefObj letterrange[, letterrange] . . .
DefVar letterrange[, letterrange] . . .


the letterrange is a range of letters that assign the given type to any variable who's first letter is included in the range...
A-Z includes ALL variables...
You can specify specific ranges to different defaults, and multiple ranges to the same default, such as:
Code:
DefStr S-T
DefSng U-V
DefDbl W
DefInt A-C, I, X-Z

See Deftype Statements in help for more info...

In previous versions of Basic (such as QuickBasic aka QB) DefInt A-Z had to be placed immediately before any procudure they were to be applied to...
In VB, you just place them in the Declarations area...

As you might have guessed, DefVar A-Z is VB's default...
(*History: DefSng A-Z was QB's default)

Just thought you might want to know...
This might save some headaches in the future ;-)

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
hi Asspin. Take a look at my reply to your post in vba forum.
Hope that helps.
Bye.
Nick.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top