Solved

Disabling the Enter key in a textbox

Posted on 2003-10-26
12
496 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
[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
  • 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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Suggested Solutions

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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