Hit Enter & Submit Data

Phreak

Regular
Joined
Jun 7, 2002
Messages
62
Location
Iowa, United States
Ok, I know how to set the tab index of my controls, but that is still a little slow and inconvenient. How could I setup a text box and button so that when I was done entering in data into the text field, it would submit the data using the button, by just pushing the ENTER key?
 
Set the Form's AcceptButton property to the button you want to click when "Enter" is pressed.

A form also has a CancelButton property that is auto-magically clicked when they press Escape. :)

Certain controls, such as a multi-line textbox, will NOT click the button even if it's the AcceptButton for the form.

-Nerseus
 
i was gona ask that same question later.

i'm remaking my chat program in vb.net from vb6 and i have a RichTextBox for where you type your message to send. is there a way to where when you press "shift"+"enter" it enters down instead of pressing the button you told it to when you press enter?

thx
 
You could inherit from TextBox and override the ProcessDialogKey method like this:

Visual Basic:
    Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If (keyData And Keys.Return) = Keys.Return Then
            If (keyData And Keys.Shift) = 0 Then
                If TypeOf TopLevelControl Is Form Then
                    If Not DirectCast(TopLevelControl, Form).AcceptButton Is Nothing Then
                        DirectCast(TopLevelControl, Form).AcceptButton.PerformClick()
                    End If
                End If
                Return True
            Else
                Return False
            End If
        Else
            Return MyBase.ProcessDialogKey(keyData)
        End If
    End Function
 
i have a problem with that code.

when i put in that code it takes these keys like i'm pressing enter:
]
o
/
m
-
and these on the number pad:
-
/

it works perfect when i don't have this code in, any suggestions?

thx
 
That's weird... I haven't time to look in to it now but if anyone else finds out why this is happening I'd be interested!
 
I've come up with a solution to the problem (there may be an easier way, but this works... kind of), however, I can't get it to work for the / key. (otherwise known as the ? key). I got the / on the numpad to work, but not the other one. Here is the code:

Visual Basic:
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If (keyData And Keys.Return) = Keys.Return Then
            If (keyData And Keys.Shift) = 0 Then
                If WrongKey(keyData) = False Then
                    If TypeOf TopLevelControl Is Form Then
                        If Not DirectCast(TopLevelControl, Form).AcceptButton Is Nothing Then
                            DirectCast(TopLevelControl, Form).AcceptButton.PerformClick()
                        End If
                    End If
                    Return True
                End If
                Return False
            Else
                Return False
            End If
        Else
            Return MyBase.ProcessDialogKey(keyData)
        End If
    End Function

    Private Function WrongKey(ByVal PKEY As System.Windows.Forms.Keys) As Boolean
        If PKEY <> Nothing Then
            Select Case PKEY
                Case Keys.OemMinus
                    Return True
                Case Keys.Subtract
                    Return True
                Case Keys.O
                    Return True
                Case Keys.OemCloseBrackets
                    Return True
                Case Keys.M
                    Return True
                Case Keys.Divide
                    Return True
                Case Keys.Separator
                    Return True
                Case Else
                    Return False
            End Select
        Else
            Return False
        End If
    End Function

Let me know if anyone knows the key value or "other name" for the forward slash (/) key to implement into the function.
 
Back
Top