14
Answers

C# Button Click, Hold and Release

Asterix Obelix

Asterix Obelix

13y
16.4k
1
Hi,

I need to do a button which has same functionality as PTT (Press to Talk).
So, you press and keep the button pressed - it does some function.
On release of the button, do something else.

while (button.pressed)
do something

on release 
do something


ps. I found this code in a Forum, but it is not working. Nothing happens on pressing the button.

isLooping = false;

//on mouse down
private void myControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
    isLooping
= true;
    runLoop
();
}

//on mouse up event
private void myControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
    isLooping
= false;
}

//This is the main loop you care about.  Put this in your application
//This should go in its own thread
void runLoop() {
   
while (isLooping) {
       
//do stuff
   
}
}


Answers (14)