Solved

Disabling the Enter key in a textbox

Posted on 2003-10-26
12
494 Views
Last Modified: 2008-02-01
Hi, would like to disable the enter key in a text box using VB.NET, I tried

Private Sub txtFonte_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFonte.KeyDown
        If e.KeyCode = Keys.Enter e.Handled = True
End Sub

but it didn't work...
0
Comment
Question by:BUCHAS
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 4

Expert Comment

by:brother7
ID: 9622091
1 - Is this a multiline textbox?
2 - Do you have a default button on the form?
0
 
LVL 4

Expert Comment

by:brother7
ID: 9622150
I think you're having a problem because you've assigned an AcceptButton to your form.  In such a case, the Button_Click event will be raised if the Enter key is pressed.
In testing, I didn't have a problem with the Enter key as long as there wasn't an AcceptButton.  Without an AcceptButton, the TextBox keeps the focus when Enter is pressed.  I think that is the behavior you're wanting, right?
Your solution is to disable the AcceptButton while in the TextBox and enable it when you leave the TextBox.

--- Code Begin ---

   Private Sub txtFonte_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFonte.Enter
      Me.AcceptButton = Nothing
   End Sub

   Private Sub txtFonte_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFonte.Leave
      Me.AcceptButton = Me.Button1
   End Sub

--- Code End ---

(All this work for 46 pts!)
0
 
LVL 2

Author Comment

by:BUCHAS
ID: 9622156
1- Yes
2- No
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 2

Author Comment

by:BUCHAS
ID: 9622180
That's strange. My acceptbutton is set to none...

And yes, I want the textbox to retain the focus, I just doesn't want the textbox go to a new line by pressing enter
0
 
LVL 2

Accepted Solution

by:
Inteliscape earned 26 total points
ID: 9622185
Private Sub txtFonte_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFonte.KeyDown
         If e.KeyCode = Keys.Enter Then e.Handled = True
End Sub

'OR USE THIS

Private Sub txtFonte_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFonte.KeyPress
          If e.KeyChar = ChrW(13) Then e.Handled = True
End Sub
0
 
LVL 2

Author Comment

by:BUCHAS
ID: 9622186
When enter is pressed in the textbox I want that nothing happens
0
 
LVL 4

Expert Comment

by:brother7
ID: 9622196
Ah, so it's a multiline textbox.  I think that makes the solution a lot easier.  There's a TextBox property called AcceptsReturn.  Be sure that it's set to False.  Also, I think you want to set WordWrap to True.
See if that works.
0
 
LVL 4

Assisted Solution

by:brother7
brother7 earned 20 total points
ID: 9622239
OK, I think I've finally got an explanation.
When using a multi-line textbox, the AcceptsReturn setting is valid only if the form has a default AcceptButton.  From the documentation, "If there is no default button for the form, then the ENTER key will always create a new line of text in the control, no matter what the value of this property."
Since you haven't defined a default AcceptButton, then the AcceptsReturn property is ignored.

One possible solution is to define a default AcceptButton.  However, when the user hits Enter, that button's click event will be raised.  That's probably not the behavior you want.

The other workaround solution is this:
1- create a dummy button, called btnDummy
2- set the Visible property for btnDummy to False
3- be sure the AcceptsReturn property for the form is False

Now, create 2 events for your TextBox.
--- Code Start ---

   Private Sub txtFonte_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFonte.Enter
      Me.AcceptButton = btnDummy
   End Sub

   Private Sub txtFonte_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFonte.Leave
      Me.AcceptButton = Nothing
   End Sub

--- Code End ---

I tested this and it works.
0
 
LVL 4

Expert Comment

by:brother7
ID: 9622248
I'd like to add that the two code snippets that I gave both work, each under different circumstances.

The first code works for a single-line textbox with a default AcceptButton.
The second code works for a multi-line textbox with no default AcceptButton.
0
 
LVL 5

Expert Comment

by:arvindb1
ID: 9622434
0
 
LVL 2

Expert Comment

by:Inteliscape
ID: 9622912
ok here is a global and more efficient way to do what you ask
just a copy-paste & try code.

    Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Message) As Boolean
        Dim keyCode As Keys = CType(m.WParam.ToInt32, Keys) And Keys.KeyCode
        If keyCode = Keys.Enter Then Return False
        MyBase.ProcessKeyPreview(m)
    End Function
0
 
LVL 2

Author Comment

by:BUCHAS
ID: 9624781
Thank you all, inteliscape's code is the simplest and worked fine:

Private Sub txtFonte_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFonte.KeyPress
          If e.KeyChar = ChrW(13) Then e.Handled = True
End Sub

brother7 also has come with a valid solution, thanks
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
Capture logon name 13 75
VB.NET Repostiory Pattern 7 39
Switching Visual Studio 2015 to normal debug display 3 28
Convert datetime to time string 10 22
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

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