Solved

how many time the enter key press was pressed

Posted on 2008-10-30
5
236 Views
Last Modified: 2012-05-05
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
Comment
Question by:cano63
  • 3
5 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 22846634
NumberLines = Len(txtInput) - Len(Replace(txtInput, vbCRLF, vbNullString)) / 2 + 1

Kevin
0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 250 total points
ID: 22846656
Correction:

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

Kevin
0
 
LVL 1

Author Comment

by:cano63
ID: 22846935
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
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 22846995
Not in VB6/VBA.

Kevin
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22850207
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vb.net 2008 2 60
Need VBS version of Excel date conversion 4 71
vbModal 12 53
Automatic Email Reminder 4 40
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question