I have a .NET UserControl (FFX 3.5). This control contains several child Controls - a Panel, a couple Labels, a couple TextBoxes, and yet another custom Control. I want to handle a right click anywhere on the base Control - so a right click on any child control (or child of a child in the case of the Panel). I'd like to do it so that it's maintainable if someone makes changes to the Control without having to wire in handlers for new Controls for example.
First I tried overriding the WndProc, but as I suspected, I only get messages for clicks on the Form directly, not any of its children. As a semi-hack, I added the following after InitializeComponent:
foreach (Control c in this.Controls)
{
c.MouseClick += new MouseEventHandler(
delegate(object sender, MouseEventArgs e)
{
// handle the click here
});
}
This now gets clicks for controls that support the event, but Labels, for example, still don't get anything. Is there a simple way to do this that I'm overlooking?
Add the control by drawingSelect the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.
Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client-side, Windows-based applications. Not only does Windows Forms provide many ready-to-use controls, it also provides the infrastructure for developing your own controls.
A Form is a Control so we can cast any form in your project as a simple Control object. Example; Control c = TheForm("Form1"); Once we have this, we can gain access to ALL the child controls including the children in other container controls on the form.
Click the tab you wish to add to. Right-click anywhere on the toolbox and select "Choose Items… " to open the Choose Toolbox Items dialog box. Scroll through the list, and select the check boxes corresponding to the Ultimate UI for Windows Forms controls and components that you want to add to the toolbox.
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);
To handle a MouseClick event for right click on all the controls on a custom UserControl:
public class MyClass : UserControl
{
public MyClass()
{
InitializeComponent();
MouseClick += ControlOnMouseClick;
if (HasChildren)
AddOnMouseClickHandlerRecursive(Controls);
}
private void AddOnMouseClickHandlerRecursive(IEnumerable controls)
{
foreach (Control control in controls)
{
control.MouseClick += ControlOnMouseClick;
if (control.HasChildren)
AddOnMouseClickHandlerRecursive(control.Controls);
}
}
private void ControlOnMouseClick(object sender, MouseEventArgs args)
{
if (args.Button != MouseButtons.Right)
return;
var contextMenu = new ContextMenu(new[] { new MenuItem("Copy", OnCopyClick) });
contextMenu.Show((Control)sender, new Point(args.X, args.Y));
}
private void OnCopyClick(object sender, EventArgs eventArgs)
{
MessageBox.Show("Copy menu item was clicked.");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With