tengu77
asked on
Common keydown event handler for multiple forms
I have 3 or more forms in my windows application where each of the form has button panel- contains one or more buttons and button names are listed like
for form1
1. a
2. b
3. c
for form2
1. x
2. y
3. z
4. m
for form3
and so on
Basically a,b,c,x,y,z,m are urls(or form names- or functin names)
when user inside form1 and if they press key -1 then it should transfer them to url (or form) 'a'.
I would like to write a common function which would work for all the forms -buttons. button count will vary
I am new to c# and have no idea how to implement this.
thank you
for form1
1. a
2. b
3. c
for form2
1. x
2. y
3. z
4. m
for form3
and so on
Basically a,b,c,x,y,z,m are urls(or form names- or functin names)
when user inside form1 and if they press key -1 then it should transfer them to url (or form) 'a'.
I would like to write a common function which would work for all the forms -buttons. button count will vary
I am new to c# and have no idea how to implement this.
thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for quick response...
would get back to you after working on it..
would get back to you after working on it..
ASKER
My form is already derived from another form.. so I cannot derive it either..
I would like to write a common function for all the form..
I would like to write a common function for all the form..
I think the best choice in this situation would be to create a usercontrol which, when dropped onto the form, would add the desired functionality. But, since you don't know a lot of C#, I think adding the code I gave you to each form would be efficacious without being overly onerous.
David
David
ASKER
Yep, but I was trying to write a code.. can you give me some hint how could I write it?
ASKER
I like to something like this
form1_keypress(...)
{
keydown.general_keypress(f orm, btn);
}
public class keydown
{
public static general_keypress(form, btn)
{
form.text = btn.text;
form.close();
}
}
form1_keypress(...)
{
keydown.general_keypress(f
}
public class keydown
{
public static general_keypress(form, btn)
{
form.text = btn.text;
form.close();
}
}
What form are your forms derived from?
ASKER
func keydown
{
foreach(Control ctl in cform.Controls)
{
// Debug.Write(ctl.GetType(),
if (ctl.GetType().Name == ("Button"))
{
//if((ctl).Text.StartsWith
if (prefix == "1.")
{
}
}
}
}
Sorry, I'm at a loss here. Can anyone else help?
ASKER
Basically I have a form which has panel and then button inside the panel..
When I say
foreach(Control ctl in cform.Controls)
It gets me panel but not the button inside the panel..
How will I get the buttons inside a panel listed?
Thank you
When I say
foreach(Control ctl in cform.Controls)
It gets me panel but not the button inside the panel..
How will I get the buttons inside a panel listed?
Thank you
OK, a couple of minor changes...
using System;
using System.Collections.Generic ;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CommonKeydown
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.KeyPress += new KeyPressEventHandler(Form1 _KeyPress) ;
this.button1.Click += new System.EventHandler(this.b utton1_Cli ck);
this.button2.Click += new System.EventHandler(this.b utton2_Cli ck);
this.KeyPreview = true;
}
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((int)e.KeyChar < (int)Keys.D0 || (int)e.KeyChar > (int)Keys.D9)
return;
string prefix = e.KeyChar.ToString() + ".";
foreach (Control ctl in EnumerateControls(this))
{
if (ctl.Text.StartsWith(prefi x) && ctl.GetType().Name == "Button")
{
Button btn = (Button)ctl;
btn.PerformClick();
e.Handled = true;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Pressed Button 1");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Pressed Button 2");
}
private static IEnumerable<Control> EnumerateControls(Control Parent)
{
foreach (Control child in Parent.Controls)
{
foreach (Control grandChild in EnumerateControls(child))
{
yield return grandChild;
}
}
yield return Parent;
}
}
}
using System;
using System.Collections.Generic
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CommonKeydown
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.KeyPress += new KeyPressEventHandler(Form1
this.button1.Click += new System.EventHandler(this.b
this.button2.Click += new System.EventHandler(this.b
this.KeyPreview = true;
}
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((int)e.KeyChar < (int)Keys.D0 || (int)e.KeyChar > (int)Keys.D9)
return;
string prefix = e.KeyChar.ToString() + ".";
foreach (Control ctl in EnumerateControls(this))
{
if (ctl.Text.StartsWith(prefi
{
Button btn = (Button)ctl;
btn.PerformClick();
e.Handled = true;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Pressed Button 1");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Pressed Button 2");
}
private static IEnumerable<Control> EnumerateControls(Control Parent)
{
foreach (Control child in Parent.Controls)
{
foreach (Control grandChild in EnumerateControls(child))
{
yield return grandChild;
}
}
yield return Parent;
}
}
}
ASKER
That's cool logic,
Thank you very much.
Thank you very much.
David