Tuesday, December 02, 2003

Subclassing a Texbox

The paradigm of subclassing gives you a several advantages when developing code. Of course the most important is the ability to cleanly separate code into a front end and back end. Among other advantages this makes it easier for continued maintenance. Additionally, subclassing allows developers to cleanly encapsulate data within code that provides a more reusable code base. For this example, let’s create a sample project that subclass’ a textbox control in a Windows application that prevents a user from cutting values from a textbox. (CTRL-X)

 

  1. Within a Windows application add a new class to the project called – scTextbox.vb
  2. Within the class enter the following code:

 

Public Class scTextBox

Inherits TextBox

Private Const WM_CHAR As Integer = &H102

 

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

' See if the CTRL key is being pressed.

If MyClass.ModifierKeys And Keys.Control Then

Select Case m.Msg

Case WM_CHAR

' Disable CTRL+X.

Select Case m.WParam.ToInt32

Case 24 'X = 24th letter of alphabet

' Do nothing here to disable the default message handling.

Case Else

'It is important to pass unhandled messages back to the default

‘ message handler.

MyBase.WndProc(m)

End Select

Case Else

'It is important to pass unhandled messages back to the default message

‘ handler.

MyBase.WndProc(m)

End Select

Else

'It is important to pass unhandled messages back to the default message handler.

MyBase.WndProc(m)

End If

End Sub

 

End Class

 

  1. Close and save the Class
  2. Drag a textbox control onto the form

 

  1. Open the Windows Form within the project and within the code window expand the region marked “Windows Form Generated code”
  2. Within this section of code replace all object creation instances that refer to the TextBox1 class with scTextbox as shown here.

#Region " Windows Form Designer generated code "

 

Public Sub New()

MyBase.New()

 

'This call is required by the Windows Form Designer.

InitializeComponent()

 

'Add any initialization after the InitializeComponent() call

 

End Sub

 

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

 

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

 

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents TextBox1 As scTextBox

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.TextBox1 = New SubClassExample.scTextBox

Me.SuspendLayout()

'

'TextBox1

'

Me.TextBox1.BackColor = System.Drawing.Color.Red

Me.TextBox1.Location = New System.Drawing.Point(24, 32)

Me.TextBox1.Name = "TextBox1"

Me.TextBox1.TabIndex = 0

Me.TextBox1.Text = "TextBox1"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(176, 78)

Me.Controls.Add(Me.TextBox1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

 

End Sub

 

#End Region

 

There are many other things that you can do with this basic concept. To download the sample project that I created CLICK HERE.


8:43:32 PM    
comment [] trackback []