Controls


After arranging a group of controls just right, you may find that you need that group elsewhere in your application or even in other applications. In that case, you might consider copying and pasting the controls between forms, making sure that all the settings are maintained.

For a more robust form of reuse, however, you can encapsulate the controls into a user control, which is a container for other controls. Although you can add one to a Windows Application project, you get one by default when you create a new Windows Control Library project. To add this project to a solution, right-click the solution in Solution Explorer and choose Add | New Project | Windows Control Library. Also make sure that you're creating the new project in the same location as your existing solution, because VS05 defaults to placing new projects one folder too far up the hierarchy in most cases. Figure 1.11 shows how to add a new project called MyFirstControlLibrary to an existing solution called MySecondApp.

Figure 1.11. Adding a New Project to an Existing Solution


After you've created a Control Library project, you're presented with a user control in a designer that looks very much like the Windows Forms Designer, as shown in Figure 1.12.

Figure 1.12. UserControl Designer


The only real difference is that this designer shows no border or caption, because those features are provided by the form that will host your new control. The code generated by the wizard looks very much like the code generated for a new form except that the base class is UserControl (located in the System.Windows.Forms namespace) instead of Form:

// UserControl1.cs using System.Windows.Forms; namespace MyFirstControlLibrary {   partial class UserControl1 : UserControl {     public UserControl1() {       InitializeComponent();     }   } } // UserControl1.Designer.cs using System.Windows.Forms; namespace MyFirstControlLibrary {   partial class UserControl1 {     ...     void InitializeComponent() {...}   } }


In the UserControl Designer, you can drop and arrange any desired controls on the user control, setting their properties and handling events just as on a form. Figure 1.13 shows a sample user control as seen in the UserControl Designer.

Figure 1.13. A User Control Shown in the UserControl Designer


When you're happy with your user control, build the project and select it from the Toolbox, where you'll find that VS05 has automatically added it to a new tab created especially for your projectin this case, "MyFirstControlLibrary Components." Drag and drop your control onto the forms of your choice, setting properties and handling events via the Properties window just as with any of the built-in components or controls. Figure 1.14 shows the user control from Figure 1.13 hosted on a form.

Figure 1.14. Hosting a User Control


User controls aren't the only kind of custom controls. If you're interested in drawing the contents of your controls yourself, scrolling your controls, or getting more details about user controls, see Chapter 10: Controls. If you need to package reusable code that doesn't have a UI of any kind, see Chapter 9: Components. Either way, you can integrate your custom implementations into the design-time environment of VS05 to take advantage of the Properties window, snap lines, and smart tags, as covered in Chapter 11: Design-Time Integration: The Properties Window, and Chapter 12: Design-Time Integration: Designers and Smart Tags.




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