Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I grab events from sub-controls on a user-control in a WinForms App?

Is there any way for the main form to be able to intercept events firing on a subcontrol on a user control?

I've got a custom user-control embedded in the main Form of my application. The control contains various subcontrols that manipulate data, which itself is displayed by other controls on the main form. What I'd like is if the main form could be somehow informed when the user changes subcontrols, so I could update the data and the corresponding display elsewhere.

Right now, I am cheating. I have a delegate hooked up to the focus-leaving event of the subcontrols. This delegate changes a property of the user-control I'm not using elsewhere (in this cause, CausesValidation). I then have a delegate defined on the main form for when the CausesValidation property of the user control changes, which then directs the app to update the data and display.

A problem arises because I also have a delegate set up for when focus leaves the user-control, because I need to validate the fields in the user-control before I can allow the user to do anything else. However, if the user is just switching between subcontrols, I don't want to validate, because they might not be done editing.

Basically, I want the data to update when the user switches subcontrols OR leaves the user control, but not validate. When the user leaves the control, I want to update AND validate. Right now, leaving the user-control causes validation to fire twice.

like image 701
Brock Greman Avatar asked Oct 02 '08 15:10

Brock Greman


People also ask

How do you make the events raised by child controls in a user control available to the host page?

By default, events raised by child controls in a user control are not available to the host page. However, you can define events for your user control and raise them so that the host page is notified of the event. You do this in the same way that you define events for any class.

What is the difference between form and user control?

User controls are a way of making a custom, reusable component. A user control can contain other controls but must be hosted by a form. Windows forms are the container for controls, including user controls. While it contains many similar attributes as a user control, it's primary purpose is to host controls.

What is UserControl in Winforms?

A UserControl is a collection of controls placed together to be used in a certain way. For example you can place a GroupBox that contains Textbox's, Checkboxes, etc. This is useful when you have to place the same group of controls on/in multiple forms or tabs.


2 Answers

The best practice would be to expose events on the UserControl that bubble the events up to the parent form. I have gone ahead and put together an example for you. Here is a description of what this example provides.

  • UserControl1
  • Create a UserControl with TextBox1
  • Register a public event on the UserControl called ControlChanged
  • Within the UserControl register an event handler for the TextBox1 TextChangedEvent
  • Within the TextChangeEvent handler function I call the ControlChanged event to bubble to the parent form
  • Form1
  • Drop an instance of UserControl1 on the designer
  • Register an event handler on UserControl1 for MouseLeave and for ControlChanged

Here is a screenshot illustrating that the ControlChanged event that I defined on the UserControl is available through the UX in Visual Studio on the parent Windows form.

Event Handlers for User Control

like image 194
Eric Schoonover Avatar answered Sep 20 '22 06:09

Eric Schoonover


The best model for this sort of thing will be creating custom events on your user control, and raising them at the appropriate times.

Your scenario is fairly complex, but not unheard of. (I'm actually in a very similar mode on one of my current projects.) The way I approach it is that the user control is responsible for its own validation. I don't use CausesValidation; instead at the appropriate user control point, I perform validation through an override of ValidateChildren(). (This usually happens when the user clicks "Save" or "Next" on the user control, for me.)

Not being familiar with your user control UI, that may not be 100% the right approach for you. However, if you raise custom events (possibly with a custom EventArgs which specifies whether or not to perform validation), you should be able to get where you want to be.

like image 44
John Rudy Avatar answered Sep 20 '22 06:09

John Rudy