-2

I need a quick and simple (I hope) solution for my C# Windows Form application.

How can I make a Mouse Down event that occurs every time I click anywhere in my form (not a specific object)?

DzAnej Mrvic
  • 47
  • 1
  • 8
  • 1
    where do you call `initializeComponent();` in your win forms place something like this there after that line `this.MouseClick += mouseClick;` – MethodMan Sep 09 '16 at 18:46
  • related: http://stackoverflow.com/questions/3441762/how-can-i-move-windows-when-mouse-down – rene Sep 09 '16 at 18:47

2 Answers2

1

so if you need to see exactly this would be an example of what you need to do

public Form1()
{
    InitializeComponent();
    this.MouseClick += mouseClick; // create the event/method for mouseClick then it will be called every time you click the mouse
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • I created the event: `private void mouseClick(object sender, MouseEventArgs e)` but for some reason it doesn't get called. And yes I have placed the `this.MouseClick += mouseClick;` under my `InitializeComponent();` – DzAnej Mrvic Sep 09 '16 at 19:19
1

I would recommend you read the documentation for the mouse click and mouse down events for proper handling of the mouse click life cycle plus the events of interest (mousedown) and go over the examples they provide. The problem as to why is not triggering the event you want, might be as simple as you not binding your mousedown event to your form/panel or area of interest.

MMan
  • 78
  • 10
  • Yes, the problem was that I was clicking on other elements on the form, not the form itself.One more thing, is there any way that I can check if the mouse button is pressed (not with an event)? I have a while loop and it would need to check if the mouse is pressed - then execute next code... – DzAnej Mrvic Sep 09 '16 at 20:04
  • I think it is possible, you can take a look at the [Mouse.LeftButton property](https://msdn.microsoft.com/en-us/library/system.windows.input.mouse.leftbutton(v=vs.110).aspx). You can also check this related [question/solution](http://stackoverflow.com/questions/28525564/mouse-leftbutton-mousebuttonstate-pressed-never-returning-true). – MMan Sep 09 '16 at 20:30
  • `if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left)` tried that but not getting called – DzAnej Mrvic Sep 09 '16 at 20:35
  • If that's the case, then I'm not sure how you can capture mouse presses without calling any events. Otherwise, i can only suggest you take advantage of the click events and just use then like a flag toggle. Where, anytime the event is trigger you make a global variable true/false and use that for you while loop. But it really depends on how you are implementing your code/gui/api – MMan Sep 09 '16 at 20:48
  • Yes I've been thinking of that but the problem is that event only gets called when I click the form, but I would need that it gets called on every element in the form... – DzAnej Mrvic Sep 09 '16 at 20:50
  • I see, then you could just bind a single click/button press/dropdown/etc event to multiple elements. Like in this related [question/solution](http://stackoverflow.com/questions/3814234/how-can-i-subscribe-multiple-buttons-to-the-same-event-handler-and-act-according) or you could assignment them [recursively](http://stackoverflow.com/questions/247946/handling-a-click-for-all-controls-on-a-form) – MMan Sep 09 '16 at 20:58
  • That is actually useful. Thanks. – DzAnej Mrvic Sep 09 '16 at 21:08