Functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Claudio

    #1

    Functions

    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?


  • Morten Wennevik

    #2
    Re: Functions

    Not sure what you are trying to do but to capture a click event you use
    this

    this.button1.Cl ick += new System.EventHan dler(this.butto n1_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.KeyEventH andler(this.MyP rogram_KeyDown) ;

    and in MyProgram_KeyDo wn you do

    private void MyProgram_KeyDo wn(object sender,
    System.Windows. Forms.KeyEventA rgs e)
    {
    switch(e.KeyCod e)
    {
    case Keys.F1:
    doButton1();
    break;
    case Keys.F2:
    doButton2();
    ...
    }
    }

    private void button1_Click(o bject sender, System.EventArg s e)
    {
    doButton1();
    }

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


    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

    Comment

    • Hector Martinez

      #3
      Re: Functions



      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.

      Comment

      • Claudio

        #4
        Re: Functions

        I have other controls on the form (DBgrid, TextBox,....). If the focus is on
        one of this controls the "this.KeyDo wn" 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" <MortenWennevik @hotmail.com> escreveu na mensagem
        news:opryvtmk06 hntkfz@localhos t...
        Not sure what you are trying to do but to capture a click event you use
        this

        this.button1.Cl ick += new System.EventHan dler(this.butto n1_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.KeyEventH andler(this.MyP rogram_KeyDown) ;

        and in MyProgram_KeyDo wn you do

        private void MyProgram_KeyDo wn(object sender,
        System.Windows. Forms.KeyEventA rgs e)
        {
        switch(e.KeyCod e)
        {
        case Keys.F1:
        doButton1();
        break;
        case Keys.F2:
        doButton2();
        ....
        }
        }

        private void button1_Click(o bject sender, System.EventArg s e)
        {
        doButton1();
        }

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


        --
        Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


        Comment

        Working...