Solved

Enter key input on multiline VB.TextBox

Posted on 2001-07-11
17
901 Views
Last Modified: 2012-05-04
I have a VB.TextBox with multiline = true and I need the control to allow a user to input a carriage return in the text box.

I am having trouble capturing the enter key input (KeyDown, KeyUp, KeyPress... and KeyPreview = true).  Any help would be appreciated.
0
Comment
Question by:hyoon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +6
17 Comments
 
LVL 6

Expert Comment

by:JonFish85
ID: 6275215
Private Sub Form_KeyPress(KeyAscii As Integer)
  Select Case KeyAscii
    Case vbKeyReturn
      Text1.SelText = vbCrLf
  End Select
End Sub

is that what you're looking for?
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6275218
Try

In the KeyPress

If KeyAscii = Chr(10) Then
        KeyAscii = 0
        Text1.Text = Text1.Text + vbCrLf
End If

Vin
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6275225
but what happens when the cursor isnt at the end of the textbox? say its in the middle of the text, you would get the textboxtext + vbcrlf
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:hyoon
ID: 6275231
Here's my dilemma...

The enter key doesn't activate the keypress event.  The TextBox is contained in a PictureBox.  I've tried Form_KeyPress, Pic_KeyPress, and Txt_KeyPress, to which none of these events would be called when I press the enter key (the events do get called on other key presses).

Any ideas?
0
 

Author Comment

by:hyoon
ID: 6275236
Here's my dilemma...

The enter key doesn't activate the keypress event.  The TextBox is contained in a PictureBox.  I've tried Form_KeyPress, Pic_KeyPress, and Txt_KeyPress, to which none of these events would be called when I press the enter key (the events do get called on other key presses).

Any ideas?
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6275253
try it with KeyDown() maybe?
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6275254
Sorry John you are correct.

Just tried what you are doing and I get the CRLF whenever I press enter in the Text box regardless of the fact that it's in a picture box.

Vin.

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6275296
In order to get true MULTI-line entry in a textbox which has Multiline=True, the Enter key MUST be entered as Control-Enter (Control Key AND Enter Key, pressed together).
0
 
LVL 11

Expert Comment

by:arana
ID: 6275370
i am trying this WITHOUT keypreview under TEXTBOX keypress and works perfectly (under w2k) any issues different from win98?


Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Text1.SelText = vbCr   '(vbCrLf added one more line)
End If
End Sub
0
 
LVL 11

Expert Comment

by:arana
ID: 6275371
tha was using the textbox inside picturebox with win2k
vb6 sp5

form.keypreview=FALSE!!!
0
 
LVL 15

Accepted Solution

by:
ameba earned 100 total points
ID: 6275399
You have command button with Default property set to True.
When user enters your textbox, change Default property to False...

Private Sub TextBox_GotFocus()
     AllowEnterKey True
End Sub

Private Sub TextBox_LostFocus()
     AllowEnterKey False
End Sub

Public Sub AllowEnterKey(Allow As Boolean)
     ' set/reset property Default
     cmdOK.DEFAULT = Not Allow
End Sub
0
 

Expert Comment

by:bppraveen001
ID: 6275804
Hello hyoon,
     Even I too faced the same problem what you have faced, while building my application. What you can do is just put one lable below your Multiline text box saying that "Use Ctrl + Enter key to enter text in Multiple lines" (this is a inbuilt feature in vb that you can use while entering multiple lines text in a Text box having Multiline set to True) When you press Ctrl + Enter when you are in Multi line text box the Click Event of the Command Button will not generated even though the Default property of the Command button set to True.
     But if you follow ameba's code you should declare one more boolean variable to keep track of in which control or text box user is there and you need to keep changing the flag values and it may take more time to execute also...

bpp
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6275970
Similar to ameba but simple.

In Form Declarations:

Dim NoEnter as Boolen


Sub Text1_GotFocus()
' In all Multiline
NoEnter=True
End Sub


Sub Text1_LostFocus()
' In all Multiline
NoEnter=False
End Sub

In the form Keypress

e.g.

Sub Form1_KeyPress(KeyAscii, etc......
If NoEnter and KeyAscii=13 Then Exit Sub

' Now do normal processing


0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6276056
I don't know, if inthedark's comments would go ok, say if the user presses ENTER key, will the text box keypress event occur or the Default Command button click will occur, and what is the sequence.

Also, this could cause some LostFocus/SetFocus problem in VB. Not sure about this.

Or else, if inthedark's comments works, then you can try this also:(It is a compilation, of some of the above comments).

Sub Text1_KeyPress()
 If Screen.Activecontrol.Name = "Text1" And KeyAscii = 13 Then
    Text1.SelText = vbCr   '(vbCrLf added one more line)
 End If
End Sub

Cheers
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6276067
"Sub Text1_KeyPress()" change to
Sub Form_KeyPress()
0
 

Expert Comment

by:bppraveen001
ID: 6279438
Hello hyoon,
    There is no need to write any code to enter text in multiple lines in a text box for which Multiline Property set to True, try by pressing Ctrl and Enter when you want to enter text in multiple lines.
0
 

Author Comment

by:hyoon
ID: 6287910
Thank you for everyone's answers.  The main problem I was having was the fact that I had a command button with default = true that prevented me from adding carriage returns without holding down ctrl.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 10 hours left to enroll

623 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