Solved

how many time the enter key press was pressed

Posted on 2008-10-30
5
235 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now