Solved

Disabling the Enter key in a textbox

Posted on 2003-10-26
12
495 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 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