I have a bunch of usercontrols which i'm mixing an matching on forms, but I'd like to match a shortcut key with button click, so when you enter data in one UserControl and hit CTRL-F it will kick off a button click on another UserControl that's on the same form. How can i set this up?
I have button click method
private
void butSearch_Click(object sender, EventArgs e){...}
Satish KathiPosted Aug 6, 2008, 6:04 PM
Expose the KeyUp event of user control and a public method to perform button click of user controls.
In the form where you are using these user controls handle the keyDown event and call the method to perform click.
Here is Sample Code
UserControl1
namespace DB
{
public delegate void KeyUphandler (object sender, KeyEventArgs e);
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public event KeyUphandler UCkeyUp;
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (UCkeyUp != null)
{
UCkeyUp(this, e);
}
}
}
}
UserControl2
namespace DB
{
public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
}
public void UCBtnPress()
{
button1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}
}
}
Form
namespace DB
{
public partial class UsingUserControl : Form
{
public UsingUserControl()
{
InitializeComponent();
}
private void userControl11_UCkeyUp(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.F)
userControl21.UCBtnPress();
}
}
}
Hope this helps