Disabling the Enter key in a textbox

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...
LVL 2
BUCHASAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brother7Commented:
1 - Is this a multiline textbox?
2 - Do you have a default button on the form?
0
brother7Commented:
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
BUCHASAuthor Commented:
1- Yes
2- No
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

BUCHASAuthor Commented:
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
InteliscapeCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BUCHASAuthor Commented:
When enter is pressed in the textbox I want that nothing happens
0
brother7Commented:
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
brother7Commented:
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
brother7Commented:
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
arvindb1Commented:
0
InteliscapeCommented:
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
BUCHASAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.