Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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?
If it works... don't worry, I'll fix it.
  • *Experts*
Posted

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 want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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

Posted

... 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

 

I second that...

If it works... don't worry, I'll fix it.
  • *Gurus*
Posted

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

 

   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

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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

Posted
I have the EXACT same problem with the EXACT same keys. I will keep looking into it. Let me know if anyone finds anything out, I will do the same.
If it works... don't worry, I'll fix it.
Posted

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:

 

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.

If it works... don't worry, I'll fix it.
Posted

Got it!

 

Substitute 'Keys.Separator' in the above WrongKey function for 'Keys.OemQuestion'. Now it works perfectly.

If it works... don't worry, I'll fix it.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...