Microsoft KB Archive/311317

= How to subclass windows in Windows Forms by using Visual Basic .NET or Visual Basic 2005 =

Article ID: 311317

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic 2005

-



This article was previously published under Q311317





For a Microsoft Visual Basic 6.0 version of this article, see 179398.

IN THIS TASK
SUMMARY
 * Subclass a Control
 * Subclass a Form
 * Subclass Any HWND
 * Troubleshooting

REFERENCES



SUMMARY
This step-by-step article describes several subclassing techniques that are available in Visual Basic .NET or in Visual Basic 2005.

back to the top

Subclass a Control
 To create a new Visual Basic .NET or Visual Basic 2005 Windows Application that is named SubclassingDemo, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Projects. Click Visual Basic Projects under Project Types, and then click Windows Application under Templates.

Note In Visual Studio 2005, click Visual Basic under Project Types, and then click Windows Application under Templates. In the Name box, type SubclassingDemo . </li> To add a new Class module to the project, click Add Class on the Project menu. In the Name box, type SCTextBox.vb .</li>  Override the inherited WndProc. To do this, add the following code to the SCTextBox class: 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 </li> Drag a TextBox control from the toolbox to Form1.vb.</li> Edit Form1.vb code as follows:   Expand the region that is marked as follows: </li>  Replace references to the standard TextBox control with references to the new SCTextBox class. To do this, replace all occurrences of the TextBox class with SCTextBox in the code that is generated.
 * 1) Region &quot; Windows Form Designer generated code &quot;

For example: ' Friend WithEvents TextBox1 As TextBox '   ' is now Friend WithEvents TextBox1 As SCTextBox

' Me.TextBox1 = New System.Windows.Forms.TextBox '   ' is now Me.TextBox1 = New SCTextBox

</li></ol> </li> Save and then compile the project.</li> To test the application, press F5, or click Start on the Debug menu.</li> Type some sample text in the text box.</li> Select the text, and then press CTRL+X. Notice that this key combination does not delete the text.</li> Right-click in the text box, and then click Cut. Notice that the text is deleted.</li></ol>

back to the top

Subclass a Form
 To create a new Visual Basic .NET or Visual Basic 2005 Windows Application that is named SubclassingDemo, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.</li> <li>On the File menu, point to New, and then click Projects.</li> <li>Click Visual Basic Projects under Project Types, and then click Windows Application under Templates.

Note In Visual Studio 2005, click Visual Basic under Project Types, and then click Windows Application under Templates.</li> <li>In the Name box, type SubclassingDemo .</li></ol> </li> <li>Edit the code for Form1.vb as follows: <ol style="list-style-type: lower-alpha;"> <li> Expand the region that is marked as follows: </li> <li> Override the inherited WndProc. To do this, add the Overrides Sub WndProc procedure to the Form1 class as follows: Public Class Form1 Inherits System.Windows.Forms.Form
 * 1) Region &quot; Windows Form Designer generated code&quot;

#Region &quot; Windows Form Designer generated code &quot;

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) ' Do whatever custom processing you need for this message. Debug.WriteLine(m.ToString)

' Forward message to base WndProc. MyBase.WndProc(m) End Sub End Class </li></ol> </li></ol>

back to the top

Subclass Any HWND
<ol> <li>Add a new Class module that is named SubclassHWND.vb to the Visual Basic .NET or Visual Basic 2005 application. To do this, click Add Class on the Project menu. In the Name box, type SubclassHWND.vb .</li> <li> Add the following code: Public Class SubclassHWND Inherits NativeWindow

Protected Overloads Overrides Sub WndProc(ByRef m As Message) ' do whatever custom processing you need for ' this message Debug.WriteLine(m.ToString)

' forward message to base WndProc MyBase.WndProc(m) End Sub End Class </li> <li> To demonstrate its use, add the following code to the Form_Load event: Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim s As SubclassHWND = New SubclassHWND s.AssignHandle(Me.Handle) ' Now s should be listening to the form's messages. End Sub </li></ol>

back to the top

Troubleshooting
The global hook must add itself in multiple processes, which require a valid, consistent function to call into. Managed code has no concept of a consistent value for function pointers because these function pointers are proxies that are built on the fly.

back to the top

<div class="references_section">