Generating or simulating key events sent to control on form

  • Thread starter Thread starter Bill J
  • Start date Start date
B

Bill J

Hi Everyone,

Is there a way of generating a keyboard character event and sending it to a
control on a form? For example, if I have a menu item called "Delete", I
would like to send a DEL character event to the currently active control on
the form.

Thanks.
 
Sorry, I forgot to mention that this is for VB.NET 2003.

- Ivan

Hi Everyone,

Is there a way of generating a keyboard character event and sending it to a
control on a form? For example, if I have a menu item called "Delete", I
would like to send a DEL character event to the currently active control on
the form.

Thanks.
 
Hi Ivan,
Is there a way of generating a keyboard character event
and sending it to a control on a form?

Have you tried the System.Windows.Forms.SendKeys class?

I suggest you can set the focus to the target control first, then call the
System.Windows.Forms.SendKeys.Send method to send the keystroke, such as:

Control1.Focus()
System.Windows.Forms.SendKeys.Send("{DEL}")

Please refer to the following MSDN documentation for the details:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformssendkeysclasstopic.asp


Thanks!

Best regards,

Gary Chang
Microsoft Community Support
======================================================
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD will be updated at 9:00
AM PST, February 14, 2006. Please complete a re-registration process by
entering the secure code mmpng06 when prompted. Once you have entered the
secure code mmpng06, you will be able to update your profile and access the
partner newsgroups.
======================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================
 
Hi Gary,

Thank you very much. It works like a charm. Cut, copy, paste also work
based on the control key sequences (eg "^C" for copy). However, I am not
able to get Select All to work. The "^A" sequence does not have any effect.
Is there a special command for this in a simple edit control?


Hi Ivan,
Is there a way of generating a keyboard character event
and sending it to a control on a form?

Have you tried the System.Windows.Forms.SendKeys class?

I suggest you can set the focus to the target control first, then call the
System.Windows.Forms.SendKeys.Send method to send the keystroke, such as:

Control1.Focus()
System.Windows.Forms.SendKeys.Send("{DEL}")

Please refer to the following MSDN documentation for the details:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformssendkeysclasstopic.asp


Thanks!

Best regards,

Gary Chang
Microsoft Community Support
======================================================
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD will be updated at 9:00
AM PST, February 14, 2006. Please complete a re-registration process by
entering the secure code mmpng06 when prompted. Once you have entered the
secure code mmpng06, you will be able to update your profile and access the
partner newsgroups.
======================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================
 
Hi Bill,

I think this is because the TextBox control doesn't have a shortcut for the
Select All operation It doesn't work when you press Ctrl+A on a textbox
control directly neither.

I suggest you can use the SelectAll() method of the textbox control instead:

TextBox1.SelectAll()


Thanks!

Best regards,

Gary Chang
Microsoft Community Support
======================================================
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD will be updated at 9:00
AM PST, February 14, 2006. Please complete a re-registration process by
entering the secure code mmpng06 when prompted. Once you have entered the
secure code mmpng06, you will be able to update your profile and access the
partner newsgroups.
======================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================
 
Back
Top