Functions

  • Thread starter Thread starter Claudio
  • Start date Start date
C

Claudio

I have a windows project that there is Form, and inside the form there are
three buttons (Button1, Button2, Button3). The text of the buttons are 'F2',
'F3' and 'F4'. When the user press F2 function, the button1 will be clicked.
When press F3 function the button2 will be clicked...and so on. I want to
associate the F1, F2 and F3 key press with the click of the buttons. Does
anyone know how to do it?
 
Not sure what you are trying to do but to capture a click event you use
this

this.button1.Click += new System.EventHandler(this.button1_Click);

Or if you mean when a user hits one of the Function keys (F1-F12) you can
capture it with this

this.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.MyProgram_KeyDown);

and in MyProgram_KeyDown you do

private void MyProgram_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.F1:
doButton1();
break;
case Keys.F2:
doButton2();
...
}
}

private void button1_Click(object sender, System.EventArgs e)
{
doButton1();
}

private void doButton1()
{
// do stuff that happens when the users clicks button 1 or hits F1
}
 
If what you want is that your button luck like pressed when the user press the (F1-F3) key, then you just simply to send a button onClick event and it'll be captured for the button event manager.

I hope this help.
 
I have other controls on the form (DBgrid, TextBox,....). If the focus is on
one of this controls the "this.KeyDown" event is not fired when I press the
F2 key. I want the key function works even if the focus is on another
control on the form.
"Morten Wennevik" <[email protected]> escreveu na mensagem
Not sure what you are trying to do but to capture a click event you use
this

this.button1.Click += new System.EventHandler(this.button1_Click);

Or if you mean when a user hits one of the Function keys (F1-F12) you can
capture it with this

this.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.MyProgram_KeyDown);

and in MyProgram_KeyDown you do

private void MyProgram_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.F1:
doButton1();
break;
case Keys.F2:
doButton2();
....
}
}

private void button1_Click(object sender, System.EventArgs e)
{
doButton1();
}

private void doButton1()
{
// do stuff that happens when the users clicks button 1 or hits F1
}
 
Back
Top