Handling a Click for all controls on a Form
If the labels are in a subcontrol then you'd have to do this recursively:
void initControlsRecursive(ControlCollection coll)
{
foreach (Control c in coll)
{
c.MouseClick += (sender, e) => {/* handle the click here */});
initControlsRecursive(c.Controls);
}
}
/* ... */
initControlsRecursive(Form.Controls);
Handling a Click for all controls on a Form & getting mouse location
The delegate for the event handler is defined like so:
public delegate void MouseEventHandler(object sender, MouseEventArgs e);
MouseEventArgs
inherits from EventArgs
, that's why your code is working. If you change the definition of your EventHandler
, you should be able to access the coordinates:
public void myEvent_handler_click(object sender, MouseEventArgs e)
{
Point point = new Point(e.X, e.Y);
}
You can also simply access e.Location
to get point:
Point point = e.Location;
All Controls Click event on form
most likely your problem arises because your form is MDI child.. you'll have to click the menu bar to activate the form.. but if you have a borderless form, clicking on controls wont activate the form.. again, this usually happens on MDI but it shouldnt happen on a regular winform.. unless you have other running events in the background that interferes with the process.
One event handler for all controls on the form
MSDN says you can do with with setting KeyPreview to true.
User Control with multiple controls handle click
Add
_button.ButtonClick += Button_Click;
around the line
_button.Click += Button_Click;
Although you don't even need _button.ButtonClick
. Simply do
private void RunCodeButton_Click(object sender, EventArgs e)
{
Click?.Invoke(this, e);
}
How to handle Click event on a Control when a child control has been clicked?
Actually it's really simple to achieve this, you can iterate through all the controls contained in your UserControl and register the Item_Click
to their EventHandler which will invoke it when the Click event is fired:
public partial class TestControl : UserControl {
public TestControl( ) {
//...
for ( int i = 0; i < Controls.Count; i++ ) {
Controls[ i ].Click += Item_Click;
}
}
//...
}
How to fire form click event even when clicking on user controls in c#
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
// 0x210 is WM_PARENTNOTIFY
// 513 is WM_LBUTTONCLICK
if (m.Msg == 0x210 && m.WParam.ToInt32() == 513)
{
// get the clicked position
var x = (int)(m.LParam.ToInt32() & 0xFFFF);
var y = (int)(m.LParam.ToInt32() >> 16);
// get the clicked control
var childControl = this.GetChildAtPoint(new Point(x, y));
// call onClick (which fires Click event)
OnClick(EventArgs.Empty);
// do something else...
}
base.WndProc(ref m);
}
How do I set a click event for a form?
In the form's ControlAdded event, add a MouseClick handler to the control, with the Address of the form's click event. I haven't tested this, but it might work.
Private Sub Example_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded
AddHandler e.Control.MouseClick, AddressOf Example_MouseClick
End Sub
Private Sub Example_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
MessageBox.Show("Click")
End Sub
Click event for .Net (Windows Forms) user control
Sorry about this - just putting an answer on in case someone googles it...
In case you're interested, this post helped solve it: User Control Click - Windows Forms… Basically, remove HandleClick, and the property and substitute this one instead:
public new event EventHandler Click
{
add
{
base.Click += value;
foreach (Control control in Controls)
{
control.Click += value;
}
}
remove
{
base.Click -= value;
foreach (Control control in Controls)
{
control.Click -= value;
}
}
}
Related Topics
How to Use the Cancellationtoken Property
How to Get Ascii Value of String in C#
Xamarin iOS Memory Leaks Everywhere
How to Add a Custom View to a Xib File Defined View in Monotouch
What Setup Code Should Go in Form Constructors Versus Form Load Event
Webexception How to Get Whole Response with a Body
Class with Single Method -- Best Approach
Unity: Apple Game Center iOS7 Registers the Scores for Only 30 Minutes in the Leaderboard
How to Call Code Behind Server Method from a Client Side JavaScript Function
JavaScript Date to C# via Ajax
What's the Method Representation in Memory
When Not to Use Regex in C# (Or Java, C++, etc.)
How to Convert JavaScript Datetime to C# Datetime
Retrying Httpclient Unsuccessful Requests