Link to home
Start Free TrialLog in
Avatar of tengu77
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

ASKER CERTIFIED SOLUTION
Avatar of anyoneis
anyoneis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want all of your forms to exhibit the same behavior, derive them all from the above form.

David
Avatar of tengu77
tengu77

ASKER

Thank you for quick response...
would get back to you after working on it..
Avatar of tengu77

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 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
Avatar of tengu77

ASKER

Yep, but I was trying to write a code.. can you give me some hint how could I write it?
Avatar of tengu77

ASKER

I like to something like this


form1_keypress(...)
{
 keydown.general_keypress(form, btn);

}


public class keydown
{
public static general_keypress(form, btn)
{
form.text = btn.text;
form.close();

}


}
What form are your forms derived from?
Avatar of tengu77

ASKER



func keydown
{

foreach(Control ctl in cform.Controls)
{
// Debug.Write(ctl.GetType(), "Keydown1");
                        
if (ctl.GetType().Name == ("Button"))
{
                                    
//if((ctl).Text.StartsWith(prefix))  // Here it gives me an error Since My form uses buttonPanel- buttons

if (prefix == "1.")
{
                                                                        

}
                                    
}
                        
                  }




}
Sorry, I'm at a loss here. Can anyone else help?
Avatar of tengu77

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
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.button1_Click);
            this.button2.Click += new System.EventHandler(this.button2_Click);

            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(prefix) && 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;
        }

    }
}
Avatar of tengu77

ASKER

That's cool logic,
Thank you very much.