I am trying to have a C# program running in the background on Windows that will print "Hello!" after seeing that the user has clicked his or her mouse 10 times. But not just in the console window, anywhere on the screen.
The following event handler for click-tracking is from msdn.microsoft.com:
private void OnMouseDownClickCount(object sender, MouseButtonEventArgs e) {
// Checks the number of clicks.
if (e.ClickCount == 1) {
// Single Click occurred.
lblClickCount.Content = "Single Click";
}
if (e.ClickCount == 2) {
// Double Click occurred.
lblClickCount.Content = "Double Click";
}
if (e.ClickCount >= 3) {
// Triple Click occurred.
lblClickCount.Content = "Triple Click";
}
}
But, I'm not sure how to actually use this. When I add this function anywhere, the MouseButtonEventArgs type is undefined.
What "using" statements do I need? How do I actually get this code to run properly -- do I call it once from main? What do I do to call it?
EDIT: Here is a picture showing Visual Studio not understanding MouseButtonEventArgs:
Initially You have to select the form and go to properties, Here you have to go events area and there is MouseClick event. Click that Mouse click. Go to Code behind window. there is the click event generated automatically. In that Form_MouseClick event you can count the number of clicks.
Initially declare a variable
int count = 0;
In method
Private void Form_MouseClick(object sender, MouseEventArgs e)
{
count++;
//add lable which will displays the count value
label.Text=count.ToString();
}
I think which will helps to count the clicks in the form.
I'm not entirely sure what you're trying to accomplish but..
To track user clicks I hooked up the "MouseDown" event on a form in a Windows Forms applications.
From there I check click counts in the event handler.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplicationTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent( );
this.MouseDown += Form1_MouseDown;
}
private void Form1_MouseDown( object sender, MouseEventArgs e )
{
// Count clicks
}
}
}
Related
Visual Studio C #
I made a calculator, and now I have to make a calculator memory (event).
There are 4 components other than the calculator: one Textbox for the answer of the calculator, two Buttons for "M" and "M+", and one Lable to display the answer again.
When the user clicks the “M” button, the contents of the Answer TextBox should be copied to a memory variable. Also make it so that when the user moves the mouse over the label, the value in the memory variable will appear in this label, and then disappear, when the mouse moves away from the label. Also add one more button, an “M+” button. When the user clicks this button, the contents of the Results box will be added to Memory. You will need to use a Global Variable to store this data.
My problem is that the label doesn't appear when the mouse over the label, and also it doens't disappear when the mouse leave the label. How can I fix it?
And also, is this way the right way to use the Global variable?
Below is my code (I just put the code for "M" and "M+" buttons, not the code for the calculator).
private String ans;
private Double answer;
private Double answerPlus;
private void btnM_Click(object sender, EventArgs e)
{
ans = txtDisplay.Text;
answer = double.Parse(ans);
lblblank.Text = answer.ToString();
}
private void lblblank_MouseEnter(object sender, EventArgs e)
{
lblblank.Show();
lblblank.Text = answer.ToString();
}
private void lblblank_MouseLeave(object sender, EventArgs e)
{
lblblank.Hide();
}
private void btnMplus_Click(object sender, EventArgs e)
{
answerPlus = answer + double.Parse(ans);
lblblank.Text = answerPlus.ToString();
}
Storing variables
The way you store your values is fine.
Events
Once you call .Hide() the next MouseEnter/MouseLeave-event will not be triggered anymore. What you could do is to take a panel, or any layout element as a wrapper/parent-element for the label and then adjust your event-callbacks to something like that:
private void panel_MouseEnter(object sender, EventArgs e)
{
lblblank.Show();
lblblank.Text = answer.ToString();
}
private void panel_MouseLeave(object sender, EventArgs e)
{
lblblank.Hide();
}
Edit
~~~
What does it mean that any layout element as a parent-element for the
label? Could you explain more?
What I meant was to just create a new panel (or layout-element) and put the label into it as a child. See the picture below:
If you set that up correctly, the code snippet I posted above will work just fine. This solution does not prevent the MouseLeave event from triggering when your mouse enters the label. Therefore you could use an alternative solution using the MouseMove event.
Alternative
using System;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
// Subscribe to the MouseMove event
this.panel.MouseMove += this.panel_MouseMove;
}
private void panel_MouseMove(object sender, MouseEventArgs e)
{
// Checks if current mouse position is within the panel
if (this.panel.Bounds.Contains(new Point(e.X, e.Y)))
{
// Current mouse position within the panel
this.label.Show();
return;
}
// Current mouse position outside the panel
this.label.Hide();
}
}
}
I currently have a NotifyIcon as part of a Windows Form application. I would like to have the form show/hide on a double click of the icon and show a balloon tip when single clicked. I have the two functionalities working separately, but I can't find a way to have the app distinguish between a single click and double click. Right now, it treats a double click as two clicks.
Is there a way to block the single click event if there is a second click detected?
Unfortunately the suggested handling of MouseClick event doesn't work for NotifyIcon class - in my tests e.MouseClicks is always 0, which also can be seen from the reference source.
The relatively simple way I see is to delay the processing of the Click event by using a form level flag, async handler and Task.Delay :
bool clicked;
private async void OnNotifyIconClick(object sender, EventArgs e)
{
if (clicked) return;
clicked = true;
await Task.Delay(SystemInformation.DoubleClickTime);
if (!clicked) return;
clicked = false;
// Process Click...
}
private void OnNotifyIconDoubleClick(object sender, EventArgs e)
{
clicked = false;
// Process Double Click...
}
The only drawback is that in my environment the processing of the Click is delayed by half second (DoubleClickTime is 500 ms).
There are 2 different kinds of events.
Click/DoubleClick
MouseClick / MouseDoubleClick
The first 2 only pass in EventArgs whereas the second pass in a MouseEventArgs which will likely allow you additional information to determine whether or not the event is a double click.
so you could do something like;
obj.MouseClick+= MouseClick;
obj.MouseDoubleClick += MouseClick;
// some stuff
private void MouseClick(object sender, MouseEventArgs e)
{
if(e.Clicks == 2) { // handle double click }
}
It is enough to register a Click event and then handle single and double clicks from it.
int clickCount;
async void NotifyIcon_Click( object sender, EventArgs e ) {
if( clickCount > 0 ) {
clickCount = 2;
return;
}
clickCount = 1;
await Task.Delay( SystemInformation.DoubleClickTime );
if( clickCount == 1 ) {
// Process single click ...
} else if( clickCount == 2 ) {
// Process double click ...
}
clickCount = 0;
}
I need to differentiate between a single click and a double click but I need to do this during the click event. I need this because if there's a single click I want to call Function A and if there's a double click I want to call Function B. But Function A must be called only in case of a single click.
How may I achieve this using standard Winforms in C# (or VB)?
click and double click can be handled seperately
click -> http://msdn.microsoft.com/en-US/library/system.windows.forms.control.click(v=vs.80).aspx
double click -> http://msdn.microsoft.com/en-US/library/system.windows.forms.button.doubleclick(v=vs.80).aspx
You will need to use a timer wait "some amount of time" after the click event to determine if the click was a single click. You can use the SystemInformation.DoubleClickTime property to determine the maximum interval between clicks that the system will consider sequential clicks a double click. You would probably want to add a little padding to this.
In your click handler, increment a click counter and start the timer. In the double click handler, set a flag to denote a double click occurred. In the timer handler, check to see if the double click event was raised. If not, it was a single click.
Something like this:
private bool _doubleClicked = false;
private Timer _clickTrackingTimer =
new Timer(SystemInformation.DoubleClickTimer + 100);
private void ClickHandler(object sender, EventArgs e)
{
_clickTrackingTimer.Start();
}
private void DoubleClickHandler(object sender, EventArgs e)
{
_doubleClicked = true;
}
private void TimerTickHandler(object sender, EventArgs e)
{
_clickTrackingTimer.Stop();
if (!_doubleClicked)
{
// single click!
}
_doubleClicked = false;
}
Another options is to create a custom control which derives from Button, and then call the SetStyles() method, which is a protected method) in the constructor and set the ControlStyles flag
class DoubleClickButton : Button
{
public DoubleClickButton() : base()
{
SetStyle(ControlStyles.StandardDoubleClick, true);
}
}
I am using a windows form and within the form i have a user control with two labels, one that has a message ENTER AMOUNT and the other where I am putting the values typed by the user (like when you go to an ATM) it starts showing the number .. it works fine if i dont have any other controls on the user control.. but the moment i add a button it does not work, it wont start showing the numbers as I use my numeric key pad.. but if i remove whatever button i added it works again... Here is my user control code.
public partial class OperationAmount : UserControl
{
public OperationAmount()
{
InitializeComponent();
}
private int _inputNumber = 0;
private void OperationAmount_Load(object sender, EventArgs e)
{
}
private void Form_KeyAmountPressed(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar))
{
return;
}
else if (lblOperationAmount.Text.Length > 9)
{
return;
}
else
{
_inputNumber = 10 * _inputNumber + Int32.Parse(e.KeyChar.ToString());
ReformatOutput();
}
}
private void ReformatOutput()
{
lblOperationAmount.Text = String.Format("{0:0.00}", (double)_inputNumber / 100.0);
}
}
Probably the new control steals the keypresses from your Form_KeyAmountPressed method because now it has the focus and receive the event KeyPress.
A simple workaround would be to add the method Form_KeyAmountPressed also at the KeyPress event of the button. Try also to set the TabStop property of the button to false. (not sure if this has any effect when the button is the only control that can get focus on your user control).
This question is related to this my question. Now I have form in my class and when user click on button I show (or hide) form. That´s ok. But I want to hide form when I move with origin form or when I click somewhere in origin form. The new form is behind that origin form. I was trying events like lostfocus and others but It didn´t help. So I think I need some trick that check from my control if there was click in parrent form (origin form) or some other hack. I know the best would be that I put code but I have many lines so I think that best way will be if you help me in general way and then I try to applicate to my app.
You can do it with a global mouse and keyboard hook. In fact, its been wrapped up into well documented, well structured .NET API over at CodePlex
Go over there and download it. Then, set up a global mouse hook:
_mouseListener = new MouseHookListener(new GlobalHooker());
_mouseListener.MouseMove += HandleGlobalHookMouseMove;
_mouseListener.Start();
The key here is that you will receive the MouseMove event ANY time the mouse moves ANYWHERE on the desktop, not just within the bounds of your window.
private void HandleAppHookMouseMove(object sender, MouseEventArgs e)
{
if (this.Bounds.Contains(e.Location))
{
HandleEnter();
}
else
{
HandleLeave();
}
}
You can also setup one for MouseClick. The combination of the two will enable you to determine any time the mouse moves over your origin form, or the mouse is clicked when its over it. Unlike the LostFocus and other events you tried, focus is irrelevant.
Does below help?
public partial class Form1 : Form
{
Form f2 = new Form2();
public Form1()
{
InitializeComponent();
f2.Show();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.ClientRectangle.Contains(e.Location) && f2.Visible) { f2.Hide(); }
}
private void button1_Click(object sender, EventArgs e)
{
f2.Visible = !f2.Visible ? true : false;
}
}