Sunday, October 20, 2019
How to Create User Control Components in VB.NET
How to Create User Control Components in VB.NET A user control is just like the Visual Basic supplied controls, such as TextBox or Button, but you can make your own control do whatever you like with your own code. Think of them like bundles of standard controls with custom methods and properties. Whenever you have a group of controls that youre likely to use in more than one place, consider a user control. Note that you can also create web user controls but theyre not the same as web custom controls; this article only covers the creation of user controls for Windows. In more detail, a user control is a VB.NET class. The class Inherits from the Framework UserControl class. The UserControl class gives your control the base functions it needs so it can be treated like the built-in controls. A user control also has a visual interface, much like a VB.NET form that you design in VB.NET. Four Function Calculator Control To demonstrate a user control, were going to create our own four function calculator control (this is what it looks like) that you can drag and drop right onto a form in your project. If you have a financial application where it would be handy to have a custom calculator available, you can add your own code to this one and use it just like a Toolbox control in your projects. With your own calculator control, you could add keys that automatically input a company standard such as a required rate of return, or add the corporate logo to the calculator. Creating a User Control The first step in creating a user control is to program a standard Windows application that does what you need. Although there are some extra steps, its still often easier to program your control first as a standard Windows application than as a user control, since its easier to debug. Once you have your application working, you can copy the code to a user control class and build the user control as a DLL file. These basic steps are the same in all versions since the underlying technology is the same, but the exact procedure is a little different between VB.NET versions. Using Different VB.NET Versions You will have a small problem if you have the VB.NET 1.X Standard Edition. User controls have to be created as DLLs to be used in other projects and this version wont create DLL libraries out of the box. Its a lot more trouble, but you can use the techniques described in this article to learn how to get around this problem. With the more advanced versions, create a new Windows Control Library. Follow this link to see the VB.NET 1.X dialog. From the VB main menu, click Project, then Add User Control. This gives you a form design environment almost identical to the one you use for building standard Windows applications. Add the components and code for your control and customize the properties you need. You can copy and paste from your debugged standard Windows app. In fact, the code for the CalcPad control (more on this below) was copied with no changes.Build your solution to get the DLL file for your control. Remember to change the Configuration to Release before the Build for production use.To move the control to the Toolbox, right-click the Toolbox and select Add/Remove Items...Using the .NET Framework Components tab, browse to the DLL for your component (probably in the bin folder of the Windows Control Library solution). Click Open when the DLL file is selected to move the control to the Toolbox, then choose OK. See this screenshot of CalcPad in the VB.NET 1.1 Toolbox. To check out your work, you can close the Windows Control Library solution and open a standard Windows Application solution. Drag and drop your new CalcPad control and run the project. This illustration shows that it behaves just like the Windows calculator, but its a control in your project. This isnt everything you need to do to move the control into production for other people, but thats another subject! The procedure for building a user control in VB.NET 2005 is almost identical to 1.X. The biggest difference is that instead of right-clicking on the Toolbox and selecting Add/Remove Items, the control is added by selecting Choose Toolbox Items from the Tools menu; the rest of the process is the same. Heres the same component (actually, converted directly from VB.NET 1.1 using the Visual Studio conversion wizard) running in a form in VB.NET 2005. Again, moving this control into production can be an involved process. Usually, that means installing it in the GAC, or Global Assembly Cache.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment