Adding a Windows Controls Library

 <  Day Day Up  >  

The whole idea of object-oriented programming is that you code things once, and then reuse them throughout your application. That applies to everything ”even the controls on each of your forms.

For example, I like to make it easy for users to see which control has the focus. So I either change the background color of the active control or enhance its border. It's a small thing, but it wouldn't be a small thing if I had to code each individual control. Believe it or not, kids , back when I was walking six miles to school in a foot of snow, that's what we had to do. We wrote macros to speed up the process, but what a pain!

Now it's much, much easier. Simply right-click on your solution and select Add Class Library, giving it the name MyControls. A new project will be added to your solution, with an empty code window. Name it MyControls.vb . Add the code shown in Listing 4.1. Recompile the project.

Listing 4.1. The MyControls Class
 Imports System.Windows.Forms Imports System.Drawing Public Class MyControls     Public Class MyText         Inherits TextBox         Public Sub New()             MyBase.new()             Text = ""             Width = 200             Enabled = False             BackColor = System.Drawing.SystemColors.ControlLight         End Sub         Public Sub EnterHandler( _          ByVal Sender As Object, ByVal e As EventArgs) _          Handles MyBase.Enter             ForeColor = ForeColor.White             BackColor = BackColor.Blue         End Sub         Public Sub LeaveHandler( _          ByVal Sender As Object, ByVal e As EventArgs) _          Handles MyBase.Leave             ForeColor = ForeColor.Black             BackColor = BackColor.White         End Sub         Public Sub DisableHandler( _          ByVal Sender As Object, ByVal e As EventArgs) _          Handles MyBase.EnabledChanged             Sender.BackColor = _             IIf(Sender.enabled, BackColor.White, _                 System.Drawing.SystemColors.ControlLight)         End Sub     End Class     Public Class MyEdit         Inherits TextBox         Public Sub New()             MyBase.new()             Text = ""             Width = 200             Multiline = True             Enabled = False             BackColor = System.Drawing.SystemColors.ControlLight         End Sub         Public Sub EnterHandler( _          ByVal Sender As Object, ByVal e As EventArgs) _          Handles MyBase.Enter             ForeColor = ForeColor.White             BackColor = BackColor.Blue         End Sub         Public Sub LeaveHandler( _          ByVal Sender As Object, ByVal e As EventArgs) _          Handles MyBase.Leave             ForeColor = ForeColor.Black             BackColor = BackColor.White         End Sub         Public Sub DisableHandler( _          ByVal Sender As Object, ByVal e As EventArgs) _          Handles MyBase.EnabledChanged             Sender.BackColor = _              IIf(Sender.enabled, BackColor.White, _                  System.Drawing.SystemColors.ControlLight)         End Sub     End Class     Public Class MyCombo         Inherits ComboBox         Public Sub New()             MyBase.new()             Text = ""             Width = 200             Enabled = False             BackColor = System.Drawing.SystemColors.ControlLight         End Sub         Public Sub DisableHandler( _          ByVal Sender As Object, ByVal e As EventArgs) _          Handles MyBase.EnabledChanged             Sender.BackColor = _             IIf(Sender.enabled, BackColor.White,                  System.Drawing.SystemColors.ControlLight)         End Sub     End Class     Public Class MyCheck         Inherits CheckBox         Public Sub New()             MyBase.new()             Text = ""             Width = 200             Enabled = False         End Sub     End Class     Public Class MyRadio         Inherits RadioButton         Public Sub New()             MyBase.new()             Text = ""             Width = 200             Enabled = False         End Sub     End Class     Public Class MyLabel         Inherits Label         Public Sub New()             MyBase.new()             Text = ""             TextAlign = ContentAlignment.MiddleRight             Height = 12         End Sub     End Class End Class 

Next, open the Toolbox using Ctrl+Alt+X, right-click, and add a new tab called My User Controls. Open the tab, right-click anywhere under it, and select Add/Remove Items. When the Customize Toolbox dialog appears, click on the Browse button, and add the new MyControls.dll component from the MyControls\bin directory.

Now when you open the Toolbox and select the MyControls tab, you'll see the MyText , MyCombo , MyCheck , MyRadio , MyLabel , and MyEdit components .

From now on you'll use these controls on your forms, rather than the VB standard controls. And if you change MyLabel 's font to Tahoma Bold and its color to purple, it will change on every single one of your forms. That's a lot of benefit for 15 seconds of work.

The same holds true for forms. In FoxPro, it's common to build a form to add, edit, and delete records in a single table. Most applications have several such tables, and the logic is the same for all of them. In FoxPro, they're called form templates, but they're just form classes. In Visual Basic .NET, they're called inheritable forms. So let's build one and see if the benefits that accrue are as considerable as they are in FoxPro.

 <  Day Day Up  >  


Visual Fox Pro to Visual Basic.NET
Visual FoxPro to Visual Basic .NET
ISBN: 0672326493
EAN: 2147483647
Year: 2004
Pages: 130
Authors: Les Pinter

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