Problem with enter key in a multiline textbox

I have had a problem using an enterr key to go from ctrl to ctrl on a form and also using the enter key to go from line to line on a multiline text box.  I could not do both of these together.

However, a solution was provided to me from this web site that enabled me to do both of these things with the ener-key .  But this code only worked if I removed code that prevented the 'beep' when the enter key was pressed.  I had used this code to prevent the warning beeb because the enter key was now being used to go from ctrl to ctrl and there would be a beep everytime the user pressed enter.  However, the presense of this code blocked the other code that fixed the enter key/multilne text box problem.  So I created some code that would silence the beep except when the user entered the multiline text box.  This enabled the enter key to work properly, but now when entering the multiline text box, there is a beep (because I have removed the code that would prevent it)

Now how do I prevent the 'beep' when the enter key is pressed but also keep the use of the enter key for both moving from ctrl to ctrl and going from line to line in the multiline text box?

Here is the code I used  to silence the beep
 Private Sub DialogOrderInfo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = ControlChars.Cr Then ' Keeps from beeping when enter key is handled in KeyDown
            e.Handled = True
            Exit Sub
        End If

here is the code provided for my solution to the enter key problem

 Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            Dim ctl As Control = Me.ActiveControl
            If TypeOf ctl Is TextBox Then
                Dim tb As TextBox = DirectCast(ctl, TextBox)
                If Not tb.Multiline Then
                    Me.ProcessTabKey(True)
                End If
            Else
                Me.ProcessTabKey(True)
            End If
            e.Handled = True
        End If
        MyBase.OnKeyDown(e)
    End Sub

For some reason the first snipped prevents the second snippet from running properly.

I modified the first snippet and now there is only one beep when the user presses the enter key and enters the multiline text box.  You see in the code below that the beep will not occur exept when the textbox is multiline.  Why do I do that, you ask?  It's because that allows the enterkey to work to move from ctrl to ctrl and to be used in the multiline text box to go from line to line.  Without any code for these beeps, it would beep all the time the enter key is pressed.  If I prevented all the beeps, then the enterkey code would not work.

Private Sub DialogOrderInfo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = ControlChars.Cr Then ' Keeps from beeping when enter key is handled in KeyDown
            Dim ctl As Control = Me.ActiveControl
            If TypeOf ctl Is TextBox Then
                Dim tb As TextBox = DirectCast(ctl, TextBox)
                If Not tb.Multiline Then
                    e.Handled = True
                End If
            Else
                e.Handled = True
            End If
        End If

What in these form keypress events are blocking the code in the  OnKeyDown event to not work?
GeorgeChamichianAsked:
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.

GeorgeChamichianAuthor Commented:
Here is a partial solution.  Now there are no beeps and the solution to the enter key works but I get ONE beep when I enter the multiline text box.

Here is the code I used

 Private Sub frmOrderEntry_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

      If e.KeyChar = ControlChars.Cr Then ' Keeps from beeping when enter key is handled in KeyDown
            Dim ctl As Control = Me.ActiveControl
            If TypeOf ctl Is TextBox Then
                Dim tb As TextBox = DirectCast(ctl, TextBox)
                If Not tb.Multiline Then
                    e.Handled = True
                End If
            Else
                e.Handled = True
            End If
        End If
    End Sub
0
Jeffrey CoachmanMIS LiasonCommented:
GeorgeChamichian,

Can you state what platform you are developing this in?

The "Normal" way to move from control to control is by pressing the tab key.
So can you state why using the most accepted method is not good for you?

As you know, Enter will wrap the text in a multiline textbox.
In your case, your system causes these to events to "Fight" each other.

I have had nothing but grief anytime I try to over-ride the default functionality.

Why not leave Tab as the default to move from control to control?
Then the enter key should work as intended in the tetbox.
Thus avoiding the issue altogether.

JeffCoachman
0
GeorgeChamichianAuthor Commented:
This is vb 2008 and the user wants the enter key.  A solution was presented that I put in my earlier post that allows me to use the enter key instead of the tab key and also to use th enter key on the multiline text box.  The textbox properties allows me to toggle off the wraparound.

The problem is that I have extra code to get rid of the beep that occurs when the enter key is pressed but when I use that code, the code to handle the enter key does not work.  Something in the nobeep code undoes the code in the enter key code.

0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

CodeCruiserCommented:
What happens if you remove this check
                If Not tb.Multiline Then
                    e.Handled = True
                End If
and change it to
                    e.Handled = True
0
GeorgeChamichianAuthor Commented:
If I do that, then using enter to go from line to line in the multiline textbox fails and does nothing and I want the enter key to funtion as a tab key going from  ctrl to ctrl and also to move from line to line in the multiline text box.


If I don't use  this frmOrderEntry_KeyPress event  code at all , then the beep occurs every time I go from ctrl to ctrl on the form using the enter key.  This code prevents a beep EXCEPT for when tb.Multiline is true, because then this code does not apply (which is why the if clause is there).  The problem is that while this code prevents beeps from ctrl to ctrl, it does not prevent the beep when the enter key is used to go to the multiline text box (because this code does not operate when Multiline is true).
0
CodeCruiserCommented:
That is the trouble. You have a condition where either one of the two is true and hence when its false then the beep.
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
Jeffrey CoachmanMIS LiasonCommented:
CodeCruiser is correct.

That is what I was trying to say in a different way.
Again, anytime you try to bypass the standard functionality (Tab to move through controls and Enter for Data Entry)
You are asking for headaches like this.

<the user wants the enter key.>
Again, this is odd, because, again, ....this is not the default behavior in the majority of professionally designed applications.
So I am wondering why they are asking you to do something that is non-standard and counter-intuitive.
Is this requirement a deal breaker?
Is this "User" paying you to do this, or is this just a request?

If you open any dialog box (aka: Form) in the majority of commercially available applications and press Tab, it will cycle the controls. Using the enter key will not.
Even on Webpages, I have never see the enter key used to fill in a webform.

Can this be done?
I am sure that it can.
But typically these "workarounds" have there own "issues" that you should be aware of.
Most of the time when this is done (ex.: MS Access), the default control cycle key is Tab.
Enter is only offered as an "option".
Bust as I stated, this causes issues in Access with multiline texboxes as well.

CodeCruiser is a top Expert here, I have no issue with him helping you accomplish what you are after, but from where I sit, it just does not seem like it is worth the trouble.

;-)

JeffCoachman
0
GeorgeChamichianAuthor Commented:
If I understand what you are saying:  When I disable the code to use the enter key as the tab key, there will be a beep.  Since I disable use of the enter key as tab key when the multiline text box is entered, there will be a beep.  There is no way to turn off this  beep when the multiline text box is entered.

 There are people who use "heads down" programming for data entry that come out of a DOS tradition.  And I do find programs on the web that accept the enter key as a tab key by the way. Ergonomically, I find the tab key to make it more difficult to do fast entry on the computer which is what these people do.  

 In addition to the beep, however, there is another drawback which clearly cannot be solved:  to get out of the multiline text box, they have to use the tab key  to go to the next control (then they can continue to use the enter key).  

 If the users voice concerns about these things, I will point out that they can either live with it, or go back to the tab instead of the enter key.  But before doing that, I needed to know whether there was a way around it: namely if one could turn the beep off when the enter key is pressed.
0
GeorgeChamichianAuthor Commented:
I guess the solution to this is that there is no solution to avoiding the beep.   So I am splitting the points between the two people who helped me accept  this.
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.