Solved

how many time the enter key press was pressed

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

707 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

13 Experts available now in Live!

Get 1:1 Help Now