problem with enter key in a multiline textbox in vb.net 2008

I have a form where i am using a override on the forms key press event to allow moving from text box to text box when the user presses enter key (they prefer enter to the tab key).

However, I have a multiline textbox and of course, now, when the user presses enterkey for a new line in the textbox, they go to the next form control rather than the next line on the textbox.  What can I do to allow using the enter key to go to the next line in the multiline text box but keep the ability to go from control to control on the form using the enter key?

Here is what I have tried so far.

First here is the code that does work in causing the jump from control to control when pressing enter key:

Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
        If e.KeyCode = Keys.Enter AndAlso Not MessageTextBox.Focused Then
            Me.ProcessTabKey(True)
            e.Handled = True
        End If
        MyBase.OnKeyDown(e)
    End Sub

Here is what I tried with the multiline text box but does not work:

  Private Sub MessageTextBox_KeyDown(ByVal sender As Object, ByVal e As  System.Windows.Forms.KeyEventArgs) Handles MessageTextBox.KeyDown
        If e.KeyCode = keys.enter Then
            Me.ProcessTabKey(True)
            e.Handled = True
        End If
    End Sub

I have also tried  setting "acceptstab" and "acceptsreturn" on the textbox properties to true, but that does not work either.

thanks for any help on this.
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.

käµfm³d 👽Commented:
You could have a common handler for all of your text boxes as demonstrated below.
Private Sub TextBox_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown, TextBox4.KeyDown, TextBox3.KeyDown, TextBox2.KeyDown, TextBox1.KeyDown
	If e.KeyCode = Keys.Enter Then
		If Not CType(sender, TextBox).Multiline Then
			Me.ProcessTabKey(True)
			e.Handled = True
		End If
	End If
End Sub

Open in new window

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
I would keep the OnKeyDown() approach but check to see if the ActiveControl() is a TextBox and if so, further check for the MultiLine:
    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

Open in new window

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
GeorgeChamichianAuthor Commented:
For some reason, the  solution of modifiying the onkeydown event does not work.  The enter key still moves from ctrl to ctrl but inside the multiline textbox nothing happens at all now.  It does not move to the next line in the textbox nor does it move to the next control.

I traced the code and it passes thru the new code correctly.  It just does nothing with the enter key.

I don't like the idea of putting code on every ctrl, but I will try the other solution to see if that works.

In all cases, however, I guess there is now no way to move to the ctrl after the multiline textbox since  there is no way to know if the user wants to leave the box or just make a new line.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

GeorgeChamichianAuthor Commented:
Thanks for your help on this
0
GeorgeChamichianAuthor Commented:
I figured out what I did to get this solution to work, but not sure why.

I had this code to prevent the 'beep' with the enter key and when I removed it, the enter key worked properly in the multiline text box.  Why is that?

    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
end sub
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure...I didn't need that extra bit of code in my test app.  =\
0
GeorgeChamichianAuthor Commented:
You can see the  related problem I just posted.

I also found a partial solution so maybe you can help on that
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
Visual Basic Classic

From novice to tech pro — start learning today.