KeyPress event with controls in Windows
I created a Form and put some controls in it like: button, textBox. I tried to connect all the controls to KeyPressEvent, but when it's run the count of the collection is 0. I don't understand why? my code: public Form1() { InitializeComponent(); ControlCollection C = new ControlCollection(this); for (int i = 0; i < C.Count; i++) { C[i].KeyPress += new KeyPressEventHandler(funcionName); } }
Kirtan PatelPosted Dec 6, 2009, 10:08 PM
Here is Code that you need .
if my answer helps you then check "do you like this answer" check box :)
you dont have to loop through all controls for that .
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A || e.KeyCode == Keys.Tab)
{
SendKeys.Send("{TAB}");
}
}
batel hodefiPosted Dec 16, 2009, 3:12 AM
Kirtan PatelPosted Dec 10, 2009, 10:03 PM
you need to set tab order to move to next control . then my code will move focus to next control automatically
Just Set TabIndex properties of every control in form as starting from 0 to ...n
theLizardPosted Dec 10, 2009, 9:53 PM
You need to know which control you came from and which control you need to go to in the order you want to go in, I have written a control (lizardControlAgent) that does this for you but for this control to work I also created controls inherited from standard controls with additional properties to handle these types of situations.
this is the code I write when I need to shift focus to the next control in specified order.
private void KeyDowNHandlerForAllControls(object sender, KeyPressEventArgs e)
{
//an example only.
if(e.KeyCode == Keys.A && lizardControlAgent.MoveNextAllowed(object sender, KeyPressEventArgs e)
{
lizardControlAgent.FocusNext();
}
}
Don't be so quick to accept answers...
batel hodefiPosted Dec 10, 2009, 6:03 AM
Kirtan PatelPosted Dec 7, 2009, 2:03 AM
No Code is Needed to Advance the control as When you Press A it will take A as Tab Button Of KeyBorad and Cursor will move to Next Control in Form
batel hodefiPosted Dec 7, 2009, 1:50 AM
batel hodefiPosted Dec 6, 2009, 5:30 PM
batel hodefiPosted Dec 6, 2009, 5:24 PM
Kirtan PatelPosted Dec 6, 2009, 4:42 PM
Can you tell me What you exactly trying to do by this code i will provide code for yout question .
just tell me the task you want to perform :)
batel hodefiPosted Dec 6, 2009, 4:37 PM
Hiren SoniPosted Dec 5, 2009, 9:21 PM
Hiren SoniPosted Dec 5, 2009, 9:19 PM
Hiren SoniPosted Dec 5, 2009, 9:16 PM
theLizardPosted Dec 5, 2009, 8:53 PM
1. go to the events on the properties pane
2. give the keypress event a a name like groupNameKeyPress, it will then generate private void groupNameKeyPress(object sender, KeyPressEventArgs e)
then for each control that has a keypress event link it to the groupNameKeyPress by selecting it from the drop down list of the key press event.
You can then do things like this.
private void groupNameKeyPress(object sender, KeyPressEventArgs e)
{
if(sender.GetType().Name == "TextBox")
{
TextBox t = (TextBox)sender; //cast sender to a text box assigning it to t, then access it's properties, methods and events.
t.Text = "Hello";
}
}