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).
Related
Using my notify icon my function for mouse click and double click event are entirely different
Mouse click event - will open a form
Doouble click event - will open a folder
Mouse click event do its job but when fire the double click event, it still trigger the mouse click event by which my form still show on double click.
Actual result on double click: opens the folder and opens the form
Expected result on double click: opens the folder
I already close the form on click but still show for a split second before hiding since the mouse click event is fired first before double click event.
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
//opens folder function
//close widget on double click
if (widgetForm != null)
widgetForm.Close();
}
This code I wrote for wpf also works with a slight change in winforms.
DispatcherTimer timer;
int clickCount = 0;
public MainWindow()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Tick += Timer_Tick;
timer.Interval = TimeSpan.FromMilliseconds(200);
}
private void MyButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
clickCount++;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
timer.Stop();
if (clickCount >= 2)
{
MessageBox.Show("doubleclick");
}
else
{
MessageBox.Show("click");
}
clickCount = 0;
}
for winforms:
System.Timers.Timer timer;
int clickCount = 0;
public Form1()
{
InitializeComponent();
timer = new System.Timers.Timer();
timer.Elapsed += Timer_Elapsed;
//SystemInformation.DoubleClickTime default is 500 Milliseconds
timer.Interval = SystemInformation.DoubleClickTime;
//or
timer.Interval = 200;
}
private void myButton_MouseDown(object sender, MouseEventArgs e)
{
clickCount++;
timer.Start();
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer.Stop();
if (clickCount >= 2)
{
MessageBox.Show("doubleclick");
}
else
{
MessageBox.Show("click");
}
clickCount = 0;
}
With the code I mentioned in the wpf example, you can change SystemInformation.DoubleClickTime to your desired time
i figured out on how to handle this is to add task delay on my mouse click event to wait if it triggers the double click event at the same time.
thank you for the answers!
private bool isSingleClicked;
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
isSingleClicked = false;
//double click process
}
private async void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
//delay click event to check if it trigger double click event
isSingleClicked = true;
await Task.Delay(100);
//check if it trigger double click event, will not proceed to the next line
if (!isSingleClicked) return;
//process of mouse click
isSingleClicked = false;
}
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:
int sn = 0;
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Konfigürasyon Yükleniyor.";
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (sn == 3)
{
label1.Text = "Ayarlar Alınıyor";
}
if (sn == 5)
{
label1.Text = "Program Başlatılıyor";
}
sn++;
timer1.Stop();
}
When I open the form I want to change the label when I select the text range.
I assume that event handler is attached in designer to this timer1.
As far as I can understand, this label is never set, because you stop Timer after it hits first time.
In this case variable sn = 0 so non of if condition from your event handler is met.
I think to solve problem you sould remove this timer1.Stop() from event handler.
You probably want
private void timer1_Tick(object sender, EventArgs e) {
if (sn == 3)
label1.Text = "Ayarlar Alınıyor";
else if (sn == 5) {
label1.Text = "Program Başlatılıyor";
timer1.Stop(); // <- stop timer here on the 5th second, not on the 1st one
}
sn++;
}
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;
}