User Controls


Although one benefit of custom controls is that they allow you to create a reusable UI, the most popular form of UI reuse for a control is simple containment, as you're accustomed to using when you build custom forms using existing controls. A user control derives from System.Windows.Forms.UserControl and is a way to contain a set of other controls for reuse as a set, producing a kind of "subform." For example, imagine that we want a control that composes our FileTextBox control from earlier with a "..." button to browse the file system.

To create a custom user control, you right-click on your project in Solution Explorer, choose Add | User Control, enter an appropriate name, and press OK.[12] When you do, you get the design surface for your user control, where you can arrange controls, as shown in Figure 10.24.

[12] If you'd like to start a new project to hold user controls, you can use the Windows Controls Library project template in the New Project dialog.

Figure 10.24. A New UserControl


Building a user control that brings the FileTextBox together with a browse button is a matter of dropping each onto the form and arranging to taste. Also, to enable browsing, you'll want to use an instance of the OpenFileDialog component, capturing all that functionality into a single user control for reuse, as shown in Figure 10.25.

Figure 10.25. The FileBrowseTextBox User Control in the Windows Forms Designer


All the control arranging that you're accustomed tosuch as anchoring and dockingworks the same way in a user control as in a custom form. You also use the same techniques for setting properties or handling events. After arranging the existing controls and components on the user control design surface, you simply write a tiny chunk of code to handle the click on the browse button to make it all work:

// FileBrowseTextBox.cs partial class FileBrowseTextBox : UserControl {   ...   void openFileButton_Click(object sender, EventArgs e) {     if( this.openFileDialog.ShowDialog() == DialogResult.OK ) {       fileTextBox.Text = this.openFileDialog.FileName;     }   } }


This code, along with a couple of controls, converts into a single user control form that can be run without any further effort, resulting in Figure 10.26.

Figure 10.26. The FileBrowseTextBox User Control in Action


User controls allow you to build reusable controls using the same tools you use when building forms, but with the added advantage that you can drop a user control onto anything that can contain controls, including container controls, forms, and even other user controls.

Testing User Controls

One issue with testing controls is that you need to create a control library and a control library test client. Furthermore, you also have to fiddle around with referencing between the two projects, which can lead to potential run-time errors; this can be a little awkward.

VS05 helps out by allowing you to test your custom user controls in a generic user control test container, shown in Figure 10.27.

Figure 10.27. VS05's UserControl TestContainer


You select the user control you want to test from the Select User Control drop-down. You can test the various properties exposed by your user control, and you can see how the resulting configuration affects its function. You can also load user controls from other assemblies by clicking the Load button.

To enable this, you create a control library and make sure it is configured to be the start-up project, as shown in Figure 10.28.

Figure 10.28. Configuring a Control Library to Use the UserControl TestContainer





Windows Forms 2.0 Programming
Windows Forms 2.0 Programming (Microsoft .NET Development Series)
ISBN: 0321267966
EAN: 2147483647
Year: 2006
Pages: 216

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net