Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

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

0
tengu77
Asked:
tengu77
  • 7
  • 6
1 Solution
 
anyoneisCommented:
Here is a sample program. Create a windows forms application, drag two buttons onto the surface and paste in this code:

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.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 this.Controls)
            {
                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");
        }
    }
}
0
 
anyoneisCommented:
If you want all of your forms to exhibit the same behavior, derive them all from the above form.

David
0
 
tengu77Author Commented:
Thank you for quick response...
would get back to you after working on it..
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
tengu77Author Commented:
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..
0
 
anyoneisCommented:
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
0
 
tengu77Author Commented:
Yep, but I was trying to write a code.. can you give me some hint how could I write it?
0
 
tengu77Author Commented:
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();

}


}
0
 
anyoneisCommented:
What form are your forms derived from?
0
 
tengu77Author Commented:


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.")
{
                                                                        

}
                                    
}
                        
                  }




}
0
 
anyoneisCommented:
Sorry, I'm at a loss here. Can anyone else help?
0
 
tengu77Author Commented:
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
0
 
anyoneisCommented:
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;
        }

    }
}
0
 
tengu77Author Commented:
That's cool logic,
Thank you very much.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now