简体   繁体   中英

MouseDown event for multiple buttons

Basically, I've named my mousedown event to be LBTNDOWN, and I've linked the event together with 3 other buttons. I want to make a switch case for each button when it's pressed down, it does something. And I'll also be making a separate mouseup event that does something when the mouse is released, but I'm already stuck at mousedown.

I've tried almost everything and researched so many solutions however it doesn't work! I'm desperate as I have to submit this project tomorrow omg!

    private void LBTNDOWN(object sender, MouseEventArgs e)
    {

        ///Code
        switch (e.Button)
        {
            case btnCFL:  
                txtbox1.text = '1';
                break;
            case btnCFR:
                txtbox1.text = '2';
                break;
        }
    }

I expected the output to be for example when button CFL is pressed down, textbox1 will change to 1, then when button CFR is pressed down, textbox1 will change to 2.

I don't think "switch (e.Button)" is well supported.

Please try below code:

    private void LBTNDOWN(object sender, MouseEventArgs e){

    ///Code
        switch ((sender as Button).Text){
            case "CFL":  
                txtbox1.text = '1';
                break;
            case "CFR":
                txtbox1.text = '2';
                break;
            default:
                Console.WriteLine("Default case should be included as a good habit");
                break;
        }
    }
  • If text cannot distinguish these buttons, you may use tag property of button instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM