Question Accessing Events from Nested User Controls

ByteCarp

Member
Joined
Dec 10, 2008
Messages
9
Programming Experience
5-10
OK I really ought to know what the best approach for this is, but I'd rather get some expert input -

What is the "BEST" way to access the methods and properties of nested user controls?

For example, suppose you create a custom control (FirstControl) that has a label and a text box. You then add this custom control to ANOTHER custom control (SecondControl) which adds a picture box. You then add THIS control to a Windows Form.

If you wish to set the properties on the label and text box, what is the best way to do it?

Dig down through the Controls collection i.e., Form1.Controls(0).Controls(0).Controls("Text1").Text = "mytext"?

Or, set an instance to each control in each parent control and then access this?

And then, how about monitoring an event like the text changing in the original control - do you ballon an event up through each user control until it reaches the Form, or is there a better way?

Any thoughts appreciated...

Thanks.
 
Think about how you would like that control class to be as a consumer. In most cases you would want it act as a single unit with direct property and event access. Take the CheckBox for example, let's say it contains a "Checker" control and a Label control; you are using CheckBox.Text property, not CheckBox.TheLabel.Text, you use the CheckBox.Checked property and CheckBox.CheckedChanged event, not CheckBox.theChecker.Checked property or CheckBox.theChecker.CheckedChanged event. Nor would you want anyone to mess with the UI design by changing for example the locations of theChecker or theLabel within the CheckBox.
 
Yes, that's my point exactly, but I'm lost as to how you actually accomplish it.

Assuming that its the BASE controls in each nested control that I wish to expose basic properties for, HOW exactly would I do this?

So, on my main form, if I have a button that should change FirstContol's text box, how do I get there with the least amount of code (using good code practices)?

I'm sure there is a simple solution to this, I'm just not seeing it...

Thanks for the response.
 
There is no simple solution, just basic work.

To add a property and let VS help you use the snippet shortcut: write "prop" and press Tab key twice, the Property code structure is inserted where you fill in the blanks like name and type of property and modify as see fit. In the Get/Set add code to get/set what you want. Example:
VB.NET:
Public Property RealText() As String
    Get
        Return Me.TextBox1.Text
    End Get
    Set(ByVal value As String)
        Me.TextBox1.Text = value
    End Set
End Property
Now this class has a RealText property that handles get/set for text of internal TextBox.

Similarly with events, type "eve" and press Tab key twice and VS inserts a default event code line, modify as you see fit. Handle the event of the internal control and raise your own class event. The common event pattern advices use of a protected method for each event, this is used by developers that inherits the class to override and allow doing code before and after the event is raised, this method prepends "On" to the event name. Note that the common event pattern requires a sender object and e event arguments. The sender is always the class instance that raised it, e is additional event info that you may for some events have to modify to be relevant in relation to the main control class instead of the internal controls, for example mouse locations. Example:
VB.NET:
Public Event RealTextChanged As EventHandler

Protected Overridable Sub OnRealTextChanged(ByVal e As EventArgs)
    RaiseEvent RealTextChanged(Me, e)
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    OnRealTextChanged(e)
End Sub
You can read more about guidelines here Design Guidelines for Developing Class Libraries or in local help.
 
Back
Top