This question already has answers here:
Long pressed button
(5 answers)
Closed 2 years ago.
I have a timer developed with c# and WinForms controlled by an external button
when I press the button, the time starts, this is the main action! I would like to know
if is it possible to recognize a long press in this button so I can program the action to stop
I am very new to C#, by programming language side is it possible to recognize this long press?
my solution:
create a public datetime variable and add button_mousedown and button_mouseup event to your form.
put the following code in there:
private void button1_MouseDown(object sender, MouseEventArgs e)
{
dt = DateTime.Now;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
var diff= ( DateTime.Now-dt).TotalMilliseconds;
if(diff > x) // x is the minimum time you want the mouse to be down on the button
{
// do stuff
}
}
when you do a click on the button, the exact time is stored in the "dt" variable.
when the click is over, the mouseup event is triggered and there you can compare the time, when you pressed the button with the currenttime and you can get a difference in milliseconds with my code
You can use the button MouseDown and MouseUp events.
Here is one way to create a counter with textbox and button with "Long Press" event:
private bool IsTargetButtonClicked = false;
private Timer HoldButtonTimer;
private void TargerButton_MouseUp(object sender, MouseEventArgs e)
{
IsTargetButtonClicked = false;
}
private void TargerButton_MouseDown(object sender, MouseEventArgs e)
{
IsTargetButtonClicked = true;
InitHoldButtonTimer();
}
private void InitHoldButtonTimer()
{
if (HoldButtonTimer == null)
{
HoldButtonTimer = new Timer();
HoldButtonTimer.Interval = 100;
HoldButtonTimer.Tick += HoldButtonTimer_Tick;
}
HoldButtonTimer.Start();
}
private int SomeIntValueToAdvance = 0;
private void HoldButtonTimer_Tick(object sender, EventArgs e)
{
if (IsTargetButtonClicked)
{
SomeIntValueToAdvance++;
textBox1.Invoke(new Action(() =>
{
textBox1.Text = SomeIntValueToAdvance.ToString();
}));
}
else
{
KillHoldButtonTimer();
}
}
private void KillHoldButtonTimer()
{
if (HoldButtonTimer == null)
{
HoldButtonTimer.Tick -= HoldButtonTimer_Tick;
HoldButtonTimer.Stop();
HoldButtonTimer.Dispose();
}
}
OUTPUT:
Related
So I have a matrix of panels (maybe will change for Picture Boxes in the future), and what i want is that every time i press one of the panels after pressing the button on the toolbox it will change it's background to a certain picture.
Right now what i have is:
private void EtapaInicial_Click(object sender, EventArgs e)
{
EtapaInicialWasClicked = true;
}
private void panel_Click(object sender, EventArgs e)
{
if (EtapaInicialWasClicked)
{
panel1.BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
What I would like to change was the panel1 to make it work for every panel (otherwise it will only change panel1 independently of the panel i've clicked), is that possible?
Try the following
private void EtapaInicial_Click(object sender, EventArgs e)
=> EtapaInicialWasClicked = true;
private void panel_Click(object sender, EventArgs e)
{
if (EtapaInicialWasClicked)
{
(sender as Panel).BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
Yes it is. You have to loop through each panel
and assign the same event handler but you have to make some changes in the event handler itself
foreach(var p in allPanels)
{
p.Click += panel_Click;
}
Then change your event handler like this
private void panel_Click(object sender, EventArgs e)
{
var p = (Panel)sender;
if (EtapaInicialWasClicked)
{
p .BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
Remember the sender argument contains reference to the actual control that initiated the event but you have to cast it first in order to use it.
However if you want to store more data for the event you've just handled you can use the panel.Tag property. This can be used to store EtapaInicialWasClicked for example
I am new on C# so sorry if it look easy for some of you :)
I have this button click:
private void buttonSwitchCamera_Click(object sender, RoutedEventArgs e)
{
_blManager.SendTrackerCmd(TrackerCmdType.PrimaryAVT_ActiveSensor, (float)IR_Id.IR_1); // Switch to IR1
_blManager.SendTrackerCmd(TrackerCmdType.PrimaryAVT_ActiveSensor, (float)IR_Id.IR_2); // Switch to IR2
}
How can I switch between the two methods when clicking the button?
one click will be for method #1
second click will be for method #2
third click will be for method #1
etc..
You can do something like that:
bool executeMethodOne;
private void buttonSwitchCamera_Click(object sender, RoutedEventArgs e)
{
executeMethodOne = !executeMethodOne;
var IrId = executeMethodOne ? IR_Id.IR_1 : IR_Id.IR_2;
_blManager.SendTrackerCmd(TrackerCmdType.PrimaryAVT_ActiveSensor, (float)IrId);
}
Have a bool called executeMethodOne which you will invert everytime you click on the button. Depending on if it is true or false you can execute the first or the second method
First click is old:
private void OnButtonClickOdd(object sender, RoutedEventArgs e)
{
// Unsubscribe OnButtonClickOdd
button.Click -= OnButtonClickOdd;
// Subcribe to OnButtonClickEven
button.Click += OnButtonClickEven;
// Do your job here
}
private void OnButtonClickEven(object sender, RoutedEventArgs e)
{
// Unsubscribe OnButtonClickEven
button.Click -= OnButtonClickEven;
// Subcribe to OnButtonClickOdd
button.Click += OnButtonClickOdd;
// Do your job here
}
Other way: just use bool flag to know it odd or even click:
private bool odd = true;
private void buttonSwitchCamera_Click(object sender, RoutedEventArgs e)
{
if(old)
{
// Odd click job
}
else
{
// Even click job
}
old = !old;
}
How can I code two buttons for scrolling into a RichTextBox up and down ? What I try :
private void btnScrollTop_Click(object sender, EventArgs e) {
if (rtbDefinitie.SelectionStart >= 30) {
rtbDefinitie.SelectionStart -= 30;
rtbDefinitie.ScrollToCaret();
}
}
private void btnScrollBottom_Click(object sender, EventArgs e) {
if (rtbDefinitie.SelectionStart <= 30) {
rtbDefinitie.SelectionStart += 30;
rtbDefinitie.ScrollToCaret();
}
}
But it's seems to get stuck after I press scroll down button twice. What I need to do ?
The second click seems to be interpreted as DoubleClick, so you could just register this event too and put the same code behind it (or replace the 30 by 60)
EDIT:
If the Application stucks, because it's working and does not have the time to update the GUI, you could try to call Application.DoEvents(); after every raised Clickevent:
private void btnScrollBottom_Click(object sender, EventArgs e) {
if (rtbDefinitie.SelectionStart <= rtbDefinitie.TextLength) {
rtbDefinitie.SelectionStart += 30;
rtbDefinitie.ScrollToCaret();
Application.DoEvents();
}
}
Looking for some help on a problem Im having
sorry if this question has already been asked, I can not find anything similar.
The idea is when a picturebox is clicked changed the image to ON.
If the picture box is held for more than 2 seconds to open a new form and leave the picturebox as OFF.
However if the picturebox is clicked ON and then held for 2 seconds and then returns i need the picturebox state to remain ON.
Here is what I have tried so far.
I believe for this to work correctly I need to stop MouseUp event from occuring.
Is there a way I can stop MouseUp when Tick occurs?
Is there a easier / better way to do this?
Any help would be appreciated.
private void time_HoldDownInternal_Tick(object sender, EventArgs e)
{
time_HoldDownInternal.Enabled = false;
time_HoldDownInternal.Interval = 1000;
form1show.Visible = true;
}
private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = true;
time_HoldDownInternal.Enabled = true;
}
private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = false;
//MessageBox.Show("mouse up");
time_HoldDownInternal.Enabled = false;
time_HoldDownInternal.Interval = 1000;
}
private void pb_pictureBoxTest_Click(object sender, EventArgs e)
{
if (mainMenuVariables.mousedown == true)
{
if (mainMenuVariables.pictureBox == false)
{
mainMenuVariables.pictureBox = true;
pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOn);
return;
}
if (mainMenuVariables.pictureBox == true)
{
mainMenuVariables.pictureBox = false;
pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOff);
return;
}
}
if (mainMenuVariables.mousedown == false)
{
//nothing
}
}
Rather than starting a timer, just record the current time on mouse down. Then in mouse up, check if it has been 2 seconds. e.g:
private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = true;
mainMenuVariables.mousedowntime = DateTime.Now;
}
private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = false;
var clickDuration = DateTime.Now - mainMenuVariables.mousedowntime;
if ( clickDuration > TimeSpan.FromSeconds(2))
{
// Do 'hold' logic (e.g. open dialog, etc)
}
else
{
// Do normal click logic (e.g. toggle 'On'/'Off' image)
}
}
I need to Distinguish Between Clicks and Double-Clicks and I have used this solution with the timer msdn.doubleclick
so I have a Timer function who looks something like this
private void doubleClickTimer_Tick(object sender, EventArgs e)
{
milliseconds += 100;
if (milliseconds >= SystemInformation.DoubleClickTime)
{
doubleClickTimer_.Stop();
if (isDoubleClick)
executeDoubleClick();
else
ExecuteSingleClick();
isFirstClick = true;
isDoubleClick = false;
}
}
and this works ok, but in the ExecuteSingleClick I need the MouseEventArgs e, but all I have is the EventArgs e from the doubleClickTimer function, is there someway to get the MouseEventArgs from the doubleClickTimer so I can write like this:
ExecuteSingleClick(MouseEventArgs e)
{
MouseButton button = e.button;
....
}
Before you start the timer, set its tag property to the mouseeventargs parameter (e). You can then use this in the timer.tick event callback (pass it to your execute(double)click functions).