[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

how many time the enter key press was pressed

I have a Textbox that is a multiline, i want to limit the user so he can press the enter key just10 times. my idea is that the multiline textbox don,t have more than 10 lines. is there any function for that ? any idea
0
cano63
Asked:
cano63
  • 3
1 Solution
 
zorvek (Kevin Jones)ConsultantCommented:
NumberLines = Len(txtInput) - Len(Replace(txtInput, vbCRLF, vbNullString)) / 2 + 1

Kevin
0
 
zorvek (Kevin Jones)ConsultantCommented:
Correction:

NumberLines = (Len(txtInput) - Len(Replace(txtInput, vbCRLF, vbNullString))) / 2 + 1

Kevin
0
 
cano63Author Commented:
IS there any other Way

i know vbcrfl give the 13 and 10 that are enter , are a way that i can use it to know what i,m looking

Your example work fine,

is there something like this

x = text1.vbcrfl
0
 
zorvek (Kevin Jones)ConsultantCommented:
Not in VB6/VBA.

Kevin
0
 
HooKooDooKuCommented:
I would suggest something like tracking how many times the enter key has been pressed with the KeyPress event.  But the flaw in any plan like that is the fact that the user can copy and paste text into the TextBox, and that text could have more than 10 lines in it.

So zorvek is steering you in the right direction, at least in concept.
What you really might have to do is that on the CHANGE event, drop all text that occurs after the 10th CrLf.  See if the following code snippet won't do all you're looking for.

Note the extra code of tracking SelStart is needed because the current selection point jumps if text is added that results in text getting chopped off.
Private Sub Text1_Change()
Dim MyText As String
Dim Start As Long
Dim Pos As Long
Dim Count As Long
Dim SelStart As Long
Dim SelLen As Long
 
    SelStart = Text1.SelStart
    SelLen = Text1.SelLength
    
    MyText = Text1.Text
    Count = 0
    Start = 1
    Pos = InStr(Start, MyText, vbCrLf)
    Do While Pos
        Count = Count + 1
        If Count = 10 Then
            Text1.Text = Left$(MyText, Pos - 1)
            If SelLen = 0 Then
                Text1.SelStart = SelStart
            End If
            Exit Do
        End If
        
        Start = Pos + 2
        Pos = InStr(Start, MyText, vbCrLf)
    Loop
            
End Sub

Open in new window

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now