Solved

Disabling the Enter key in a textbox

Posted on 2003-10-26
12
491 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

14 Experts available now in Live!

Get 1:1 Help Now