I have a control that should do something when clicked on, to achieve this I connected its mouseclick event to a function on creation but the problem is that the click event Is triggered even tho my mouse is not clicking it(My mouse is not even on the form???)
Here is the code behind it=>
//called on creation(I create my control through code so doing it manually wont work)
seriesPanel.MouseClick += Mouseclicked(Data.C_Title);
//function
public MouseEventHandler Mouseclicked(string Title)
{
SeriePage page = new SeriePage();
page.Title = Title;
page.Show();
return null;
}
NEWBIE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
7
Instead of adding Mouseclicked
as an event handler, you’re calling Mouseclicked
and adding what it returns, in this case null
as a handler. Removing the (Data.C_Title)
is what you want, though it will expose a different problem in this case.
Almost all (if not all) WinForms event handlers are expected to have arguments of (object sender, System.EventArgs e)
where EventArgs
might be replaced with another class inheriting from EventArgs
. So MouseClicked
needs to be changed to have those parameters.
1
-
Store
Data.C_Title
in a field somewhere, and reference that field inside yourMouseclicked
event-handler.- And
Mouseclicked
needs to conform toMouseEventHandler
, i.e.private void Mouseclicked(Object? sender, EventArgs e)
.
- And
-
Wire-up
seriesPanel.MouseClick
correctly:- If your event-handlers will be added only once, inside the ctor (after
InitialzieComponent
), then you can useseriesPanel.MouseClick += this.Mouseclicked
(this creates a delegate variable behind-the-scenes and passes that to theMouseClick
multicast delegate). - If your event-handlers are dynamic (added and removed at runtime) then you need to use
+=
and-=
with aMouseEventHandler
-typed variable rather than the method-name itself.
- If your event-handlers will be added only once, inside the ctor (after
-
Also, are you sure you want
MouseClick
instead ofClick
? Consider usingClick
instead ofMouseClick
where-possible for accessibility reasons.
Like so:
(I’ve renamed the Mouseclicked
method to OnSeriesPanelClicked
for clarity)
void Somewhere( ... )
{
this.title = Data.C._Title;
// called on creation (I create my control through code so doing it manually wont work)
seriesPanel.MouseClick += this.OnSeriesPanelClicked;
}
private String? title;
public void OnSeriesPanelClicked(Object? sender, EventArgs e)
{
SeriePage page = new SeriePage();
page.Title = this.title;
page.Show();
}
For added/removed event-handlers (and assuming you’ll be using different handler methods than just OnSeriesPanelClicked
):
(For clarity, this code is more verbose than it needs to be)
void Somewhere( ... )
{
if( this.lastMouseClick != null )
{
seriesPanel.MouseClick -= this.lastMouseClickHandler;
this.lastMouseClickHandler = null;
}
this.title = Data.C._Title;
MouseEventHandler newHandler = new MouseEventHandler( this.OnSeriesPanelClicked );
seriesPanel.MouseClick += newHandler;
this.lastMouseClickHandler = newHandler;
}
private MouseEventHandler? lastMouseClickHandler;
private String? title;
public void OnSeriesPanelClicked(Object? sender, EventArgs e)
{
SeriePage page = new SeriePage();
page.Title = this.title;
page.Show();
}
2