How to subclass Windows in Windows Forms by using Visual C# .NET or Visual C# 2005

Article translations Article translations
Article ID: 815775 - View products that this article applies to.
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article describes several subclassing techniques that you can use in Visual C# .NET or Visual C# 2005.

Subclass a Control

  1. To create a new Visual C# .NET or Visual C# 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 C# Projects under Project Types, and then click Windows Application under Templates.

      Note In Visual C# 2005, click Visual C# under Project Types.
    4. In the Name text box, type SubclassingDemo.
  2. To add a new Class module to the project, click Add Class on the Project menu.
  3. In the Name text box, type SCTextBox.cs, and then click Open.
  4. Add the following statement to the top of the code in SCTextBox.cs:
    using System.Windows.Forms;
  5. Override the inherited WndProc. To do this, replace the existing SCTextBox class code with the following code:
    public class SCTextBox : TextBox
    {
        private const int  WM_CHAR = 0x102;
        protected  override void WndProc(ref Message m)
         {	
             //See if the CTRL key is being pressed.
             if ( SCTextBox.ModifierKeys.CompareTo(Keys.Control) == 0)
             {																																
    				         switch(m.Msg)
                 {
                     case WM_CHAR:
                          // Disable CTRL+X.
                          switch(m.WParam.ToInt32())
                          {
                               case 24 ://X = 24th letter of alphabet
                                    break;
                                    // Do nothing here to disable the default message handling.
                               default:
                                    //Make sure that you pass unhandled messages back to the default message handler.
                                    base.WndProc( ref m);
                                    break;
                            }
                            break;
                      default:
                            //Make sure that you pass unhandled messages back to the default message handler.
                            base.WndProc(ref m);
                            break;
                     }
                  }
               else
                  //Make sure that you pass unhandled messages back to the default message handler.
                  base.WndProc(ref m);
        }
    }
  6. Drag a TextBox control from the Toolbox to Form1.cs.
  7. Edit the Form1.cs code as follows:
    1. Expand the region that is marked in the code as follows:
      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:
      //private System.Windows.Forms.TextBox textBox1;
      //is now
      //private SCTextBox textBox1;
      
      //this.textBox1 = new System.Windows.Forms.TextBox();
      //is now
      //this.textBox1= new SCTextBox();
  8. Save the project. On the Debug menu, click Start.
  9. Type some sample text in the text box.
  10. Select the text, and then press CTRL+X. Notice that this key combination does not delete the text.
  11. 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 C# .NET or Visual C# 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 C# Projects under Project Types, and then click Windows Application under Templates.
      Note In Visual Studio 2005, click Visual C# under Project Types.
    4. In the Name text box, type SubclassingDemo.
  2. Edit the code for Form1.cs as follows:
    1. Add the following statement to the top of the Form1 code:
      using System.Windows.Forms;
    2. Expand the region that is marked as follows:
       Windows Form Designer generated code
      
    3. Override the inherited WndProc. To do this, add the override void WndProc procedure to the Form1 class as follows:
      public class Form1 : System.Windows.Forms.Form
      {
         #region Windows Form Designer generated code
         protected override void WndProc(ref System.Windows.Forms.Message m)
         {
             // Perform whatever custom processing you must have for this message.
             System.Diagnostics.Debug.WriteLine(m.ToString());
             //Forward message to base WndProc.
             base.WndProc(ref m);
         }	
        #endregion
      }

Subclass Any HWND

  1. Add a new Class module that is named SubclassHWND.cs to the Visual C# .NET or Visual C# 2005 application. To do this, click Add Class on the Project menu.
  2. In the Name text box, type SubclassHWND.cs, and then click Open.
  3. Replace the SubclassHWND class code with the following code:
    public class SubclassHWND : NativeWindow
      {
       protected override void WndProc(ref Message m)
        {
          // Perform whatever custom processing you must have for this message
          System.Diagnostics.Debug.WriteLine(m.ToString());
         // forward message to base WndProc
         base.WndProc(ref m);
        }
    }
  4. To demonstrate its use, add the following code to the Load event of Form1:
    SubclassHWND s = new SubclassHWND();
    s.AssignHandle(this.Handle);
    //Now s should be listening to the messages of the form.

Troubleshoot

The global hook must add itself in multiple processes These processes 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 following article number to view the article in the Microsoft Knowledge Base:
320584 HOW TO: Trap Keystrokes in .NET Controls by Using Visual C# .NET

Properties

Article ID: 815775 - Last Review: December 11, 2006 - Revision: 2.4
APPLIES TO
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
Keywords: 
kbctrl kbcontrol kbwindowsforms kbwndwproc kbforms kbhowtomaster KB815775

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com