Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 10th, 2006, 11:45 AM
Registered User
 
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# Control.KeyDown Not Working

Control.KeyDown, C#.NET

I have a user control with several controls on it. I had the same code in all the controls to call the Winhelp or DO SOME STUFF. The code basically captured the F1 Key when the control had the focus. All worked fine, but I had up to 20 controls on a form and the same snippet of code below had to basically be repeated 20 times

1. Create an event for Control in IntitializeComponent()
this.MyTextBox.KeydDown += new System.Windows.Fomrs.KeyEventHandler(this.MyTextBo x_EventName);

2. Create the Event Handler for For the Above Control.KeyDown event

private void MyTextBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
 if (e.KeyCode == Keys.F1)
 {
  DO SOME STUFF HERE;
 }
}

I then made the code generic see the snippet below and used one routine for all the controls. Now the Event Handler is never called, nothing happens. What is missing or what else is needed for a simple solution?

Below is the Scenario

1.Create a generic event for all controls on the form in IntializeComponents()
this.KeydDown += new System.Windows.Forms.KeyEventHandler(this.MyGeneri c_EventName_KeyDown);

2 .Create a generic event handler for all The Controls that we are interested in.

private void MyGeneric_EventName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
 foreach (Control c in this.Controls)
 {
  if (c is TextBox || c is ComboBox || c is CheckBox)
  {
   if (e.KeyCode == Keys.F1)
   {
    DO SOME STUFF HERE;
   }
  }
 }
}
 
Old September 11th, 2006, 09:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Quote:
quote:1.Create a generic event for all controls on the form in IntializeComponents()
this.KeydDown += new System.Windows.Forms.KeyEventHandler(this.MyGeneri c_EventName_KeyDown);

2 .Create a generic event handler for all The Controls that we are interested in.

private void MyGeneric_EventName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
 foreach (Control c in this.Controls)
 {
  if (c is TextBox || c is ComboBox || c is CheckBox)
  {
   if (e.KeyCode == Keys.F1)
   {
    DO SOME STUFF HERE;
   }
  }
 }
}
Set the KeyPreview property to True for form 'this'.

Regards
Ankur
 
Old September 21st, 2006, 02:40 AM
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to yudi010
Default

my english is very badily
but the code is see







Similar Threads
Thread Thread Starter Forum Replies Last Post
checkpassword control not working aruni_cbe ASP.NET 1.0 and 1.1 Professional 0 October 7th, 2006 05:56 AM
keydown/press r.parolari BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 March 17th, 2006 09:34 AM
How to use KeyDown event in Form zhangxujun1981 C# 5 July 6th, 2004 02:07 AM
Working with MSHFlexGrid control predal VB How-To 0 March 3rd, 2004 07:06 AM
User Control Id not working duncanthescott Pro VB.NET 2002/2003 4 September 23rd, 2003 11:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.