Limit a multi-line textbox to 10 lines ? 
Author Message
 Limit a multi-line textbox to 10 lines ?

A newbie (groan) using VB 6 Pro

Anyone have any idea how to limit a text box set up as multi-line (with
vertical scroll bar) to no more than 10 lines of text ?

Your help and guidance would be greatly appreciated !


Tue, 12 Aug 2003 05:08:30 GMT  
 Limit a multi-line textbox to 10 lines ?
I can only tell you how to count the number of lines...

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal
hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, lParam As Any) As

Dim r As Long
r = SendMessage(Text1.hWnd, &H400 + 10, 0, 0&) - 1
MsgBox "Text1 has " & r & " lines."

This code was given to me by Otser.

Tue, 12 Aug 2003 06:05:35 GMT  
 Limit a multi-line textbox to 10 lines ?
This is sort of a kludge, but it does work. Start a new project and put a
TextBox on the Form (remember to set its MultiLine property to True). Then
paste this code at the end of this message into the Form's code window. The
MaxLines variable is set to 10 in the Form_Load event. The commented out
code for the MaxLines setting will do confine the text to the visible height
of the TextBox (i.e., no scroll bars).


Dim MaxLines As Long

Private Declare Function SendMessageLong Lib _
    "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, _
     ByVal wMsg As Long, _
     ByVal wParam As Long, _
     ByVal lParam As Long) As Long

Function CursorLine(TextBoxIn As TextBox) As Long
    CursorLine = SendMessageLong(TextBoxIn.hwnd, &HC9, -1&, 0&) + 1
End Function

Private Sub Form_Load()
  With Text1
    Set Me.Font = .Font
    MaxLines = 10   '(.Height - 6 * .BorderStyle *
                              '  Screen.TwipsPerPixelY) \
                              '  Me.TextHeight("A")
  End With
End Sub

Private Sub Text1_Change()
  Static LastText As String
  Static LastCaretPos As Long
  Static SecondTime As Boolean
  Dim CurrentCaretPos As Long
  With Text1
    If Not SecondTime Then
      CurrentCaretPos = .SelStart
      .SelStart = Len(.Text)
      If CursorLine(Text1) > MaxLines Then
        SecondTime = True
        .Text = LastText
        .SelStart = LastCaretPos
        LastText = .Text
        .SelStart = CurrentCaretPos
        LastCaretPos = .SelStart
      End If
    End If
  End With
  SecondTime = False
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
  With Text1
    LastPosition = .SelStart
    'Place any other KeyPress checking code here
  End With
End Sub

Tue, 12 Aug 2003 13:01:35 GMT  
 Limit a multi-line textbox to 10 lines ?

> This code was given to me by Otser.

I thought it looked familiar.  :)

Regards, Otser.

Tue, 12 Aug 2003 13:27:23 GMT  
 [ 4 post ] 

 Relevant Pages 

1. Multi-Line Textbox limits

2. HELP:limiting # of lines in a Multiline Textbox

3. Line limit in Multiline Textbox

4. Multiline Textboxes - Limiting Characters Per Line

5. Limit Size of Multi-line Textbox

6. Read a MultiLine textbox line by line?

7. Limitting line length of each line within multiline textbox

8. No of lines in a multi line TextBox ?

9. Limiting X number of lines in TextBox

10. Limiting X number of lines in TextBox

11. Finding multi-line paragraphs (wrapped lines)

12. Printing other lines from Multi-Line text box


Powered by phpBB® Forum Software