Can't get my head around this, there are no decent examples on how to setup an event listener for a mouse move event.
I can find references like this and this but that doesn't really help...
How can I do something like this:
public event MouseEventHandler(object sender, MouseEventArgs e)
{
//Manage mouse move event
//Get X, Y position of mouse ect...
}
Edit:
I'm getting two errors:
A static readonly field cannot be assigned to (except in a static
constructor or a variable initializer)
Operator '+=' cannot be applied to operands of type 'RoutedEvent' and
'method group'
These errors are on this line: Mouse.MouseMoveEvent += mouseEventHandler.
public MainWindow()
{
InitializeComponent();
Mouse.MouseMoveEvent += mouseEventHandler;
}
public void mouseEventHandler(object sender, MouseEventArgs e)
{
Point mousePosition = e.GetPosition(this);
}
Simply attach an event handler to the MouseMove event of any UIElement.
For example, you could add it to a Window in the code-behind like this:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.MouseMove += Window_MouseMove;
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
Console.WriteLine("Mouse moved");
}
}
The alternative to attaching the event handler in C# in code-behind is to set it using XAML, e.g. for above window, it could be like this:
<Window …
MouseMove="Window_MouseMove">
Note that you still need to declare the Window_MouseMove method in the code-behind.
EDIT:
If you are using Forms, you probably want to use:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousemove(v=vs.110).aspx
Like:
System.Windows.Forms.MouseMove += MouseEventHandler
Take in mind you need a instance of Forms, and use it directly like:
this.MouseMove += MouseEventHandler
Because of the InitializeComponent() function in your constructor I guess you are using Winforms using the designer in visual studio
Apart from the correct answers others gave I want to point out to you that subscribing to events in visual studio is really easy in the designer.
The designer is the part where you draw the form, add all the buttons, text boxes etc.
To add an event handler for your mouse move (or for any event that any component in your form might raise), do the following:
Select the component that should react on the your move move, for instance a button, or the form you are designing
Go to the properties window of this component (there are a lot of possibilities of how to do this, one of it is via menu view - properties
In the properties window you see a lightning flash sign. If you click on it, you see a list of most events the component can generate
One of these events is the mousemove. Type a function name, or double click on the event to generate a default name.
The event handler is generated with all correct parameters and the code of the event handler is shown
Related
(I am newbie and this is probably a duplicate question.)
To see which control is clicked on form, I have a method like this;
public void IdentifyControl(object sender, System.Windows.Input.MouseEventArgs e)
{
Control ctrl = sender as Control;
if (ctrl != null)
SelectedControl = ctrl;
this.Cursor = new System.Windows.Forms.Cursor(System.Windows.Forms.Cursor.Current.Handle);
MessageBox.Show(""+ctrl.GetChildAtPoint(System.Windows.Forms.Cursor.Position).ToString());
}
and trying to call it from mouse click.
In my first attempt, I added this function to main forms MouseClick event but it only worked for form itself but controls in form. Then I tried to create a general click event and use by Mouse class.
The main point I stuck is that I couldn't create the suitable parameters Mouse.AddMouseUpHandler(DependencyObject element,
MouseButtonEventHandler handler)
Its probably because of I don't know event handling but maybe I am all in a wrong way.
You can create a mouse click event just double clicking on any buttons, labels, etc, and the Visual studio will create a code for you, like this:
private void YOUROBJECT_Click(object sender, EventArgs e)
{
}
Just put the method you want into this click event.
Hope this can help you!
I wanna change the number of the TextBox when The Mouse Scrolls. I have a Scroll TextBox But I Don't wanna use that. Is There any Event related to this?
Should I write an TextBox Event? If yes, How can I write a textBox event that Happens when Mouse Scrolls?
The MouseWheel event is there alright:
public Form1()
{
InitializeComponent();
textBox1.MouseWheel += textBox1_MouseWheel;
}
void textBox1_MouseWheel(object sender, MouseEventArgs e)
{
throw new NotImplementedException();
}
But it is not visible in the event editor. No idea why..
You can find the full list of events through Intellisense like this:
Enter the Control's name and a dot. Now watch the dropdownlist for the event you need. When you have it write += add press Tab twice. This hooks up the event and creates a stub for it.
You need MouseWheel event. Check the documentation.
VS studio is not Intellisense becuase some properties and methods are
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
The child controls of my custom control are obstruction the mouse events in my custom control. I have worked through the accepted answer and the answer at the bottom of this thread...
exposing-events-of-underlying-control
I haven't gotten them to work (the answer at the bottom seemed most straight forward to me). But really I would like to disable the events of them altogether. I have a pictureBox and a label, I don't need to interact with either of the child controls. Is there a way to disable them so they wont interfere with the events of my custom control?
Edit:
I'm using the custom control to gather and process a number of things and make them available as properties. When I click on it, I need to access to the properties. When the event happens at the child control, I don't have access to the propertied of my custom control. The following code is in my form...
public void Form1_MouseDown(object sender, MouseEventArgs e)
{
var myControl = sender as SubstanceViewer;
richTextBox1.Text = myControl.substanceInfo;
}
so I will need to access the properties of the parent control.
If you need the the events that are normally trapped by the child controls to be handled by the custom control itself, then simply wire up those events at run-time in the constructor of the custom control.
For example if you needed the MouseMove() event of the PictureBox and Label to fire the already wired up event of the UserControl:
public partial class SomeUserControl : UserControl
{
public SomeUserControl()
{
InitializeComponent();
this.pictureBox1.MouseMove += SomeUserControl_MouseMove;
this.label1.MouseMove += SomeUserControl_MouseMove;
}
private void SomeUserControl_MouseMove(object sender, MouseEventArgs e)
{
}
}
Be aware, though, that since different controls are firing the same handler you'll need to take that into account. For example, the e.X and e.Y values in the handler above would be relative to the source control.
*You can also wire these events up at design-time using the IDE itself, but I thought code better illustrated the solution.
What I'm trying to do is get my winform to display a debug line when ever I click in my winform. However, when I do, nothing happens. I know how to get a button / other click event to happen. But what I need is to be able to click anywhere within my winform.
I've googled this for the past hour but can't see what I'm doing wrong. As far as I'm aware, this code should be correct in detecting a mouse click. This method is held withing the form1.cs class:
private void mouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
Trace.WriteLine("Mouse clicked");
}
I've tried setting brake points, but these don't get triggered either. What is it I'm doing wrong?
Sorry for the stupidly newbie question, but I am very new to winform programming.
How to add the EventHandler:
public Form1()
{
InitializeComponent();
// This line should you place in the InitializeComponent() method.
this.MouseClick += mouseClick;
}
Using the editor built-in to Visual Studio:
Go to the properties window (if you don't see it, press Alt + Enter).
Select the events icon (looks like lightning).
Double-click on the empty ComboBox to the right of Click.
You'll be taken to an empty method where you can put your code.
The method itself is correct. I think your actual problem is: you haven't added this method to MouseClick events.
In C# – and most other languages too – event is handled by an event handler. Windows forms and controls have events for all the events happening in your controls, such as OnClick or OnResize.
You can append methods to these events, and the methods will automatically get called when the actual event happens. Simply add the following line to your form's constructor, Form_Load-method, InitializeComponent-method, or such:
this.MouseClick += mouseClick;
Now when ever MouseClick event happens, your method mouseClick will be called automatically.
I would recommend reading Events C# Programming Guide. You need to add an event handler like so:
form1.MouseClick += mouseClick;
First off, the mousewheel event is not listed in Visual Studio 2008's events pane which is very annoying.
I found the correct format online though, and wrote this into my code:
private void Form1_MouseWheel(object sender, MouseEventArgs e)
{
Debug.WriteLine("Foo");
}
...from which I'm getting no response when the mousewheel is rotated.
I'm doing this in my code's main class area, and the designer contains only the one form/window/whatever so the mouse isn't losing focus.
namespace BlahBlah
{
public partial class Form1 : Form
{
And by contrast, I have this method right above the mousewheel one and it works fine:
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Debug.WriteLine("Foo");
}
If I had to guess, I'm thinking I'm not correctly linking the code to the form (aka: all the stuff that visual studio would do for me if I added this event through the designer's events panel). But I could be wrong or just be making some silly error.
Can you help me get ANY kind of response when the mouse wheel is rotated? Thanks!
The mousewheel event needs to be set up.
Add this to Form1's constructor (After InitalizeComponents();)
this.MouseWheel+= new MouseEventHandler(Form1_MouseWheel);
I don't have enough reputation to respond with a comment, but the answer to your side question is that the delegates do need to be setup. However when you create one by double clicking it in the properties pane the code is automatically generated and placed in the *.designer.cs file in the InitializeComponent method.
It's best in this case to override the OnMouseWheel member function rather than register to the event. For example:
public class MyFrm : Form
{
protected override void OnMouseWheel(MouseEventArgs e)
{
/*Handle the mouse wheel here*/
base.OnMouseWheel(e);
}
}
I don't know how to subscribe to that particular event but you can override the appropriate method on the form, instead.