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

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

Summary

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

Subclass a Control

  1. To create a new Visual Basic .NET or Visual Basic 2005 Windows Application that is named SubclassingDemo, follow these steps:
    1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
    2. On the File menu, point to New, and then click Projects.
    3. 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.
    4. In the Name box, type SubclassingDemo.
  2. To add a new Class module to the project, click Add Class on the Project menu. In the Name box, type SCTextBox.vb.
  3. 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
  4. Drag a TextBox control from the toolbox to Form1.vb.
  5. Edit Form1.vb code as follows:
    1. Expand the region that is marked as follows:
      #Region " Windows Form Designer generated code "
    2. 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.

      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()

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

Subclass a Form

  1. To create a new Visual Basic .NET or Visual Basic 2005 Windows Application that is named SubclassingDemo, follow these steps:
    1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
    2. On the File menu, point to New, and then click Projects.
    3. 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.
    4. In the Name box, type SubclassingDemo.
  2. Edit the code for Form1.vb as follows:
    1. Expand the region that is marked as follows:
      #Region " Windows Form Designer generated code"
    2. 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

      #Region " Windows Form Designer generated code "

      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

Subclass Any HWND

  1. 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.
  2. 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
  3. 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

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.

References

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
320583 HOW TO: Trap Keystrokes in .NET Controls by Using Visual Basic .NET
Propriedades

ID do Artigo: 311317 - Última Revisão: 6 de dez de 2006 - Revisão: 1

Comentários