Shared Textbox control not visible on form

caeanis

Freshman
Joined
Sep 6, 2006
Messages
40
I have a custom textbox class that is set public scope. I created it because I needed to override the WndProc sub to handle paste messages sent from this control. The problem I'm having is that when the form is compiled, I get no error messages but the textbox doesn't show on the form. I've posted my code below, and would appreciate any observations on what I missed. I'm sure it's something simple...

Code:
Public Class optyTextBox
        Inherits System.Windows.Forms.TextBox
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            Const WM_PASTE As Int32 = &H302
            Select Case m.Msg
                Case WM_PASTE ' The clipboard contents were pasted... 
                        'Code to handle paste event.
            End Select
        End Sub
End Class

    Shared WithEvents txtZip As optyTextBox
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtZip = New optyTextBox
        txtZip.Name = "txtZip"
        txtZip.Font = New System.Drawing.Font("Tahoma", 6.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        txtZip.Location = New System.Drawing.Point(142, 160)
        txtZip.Margin = New System.Windows.Forms.Padding(2, 3, 2, 3)
        txtZip.Size = New System.Drawing.Size(59, 18)
        txtZip.TabIndex = 28
        txtZip.Visible = True  ' Shouldn't this work?
        Me.Controls.Add(txtZip)
        txtZip.Show()  ' I tried using this line to see if it forced the control to
        'be visible but no dice...
    End Sub
 
Last edited:
This code has one major problem:
Code:
Public Class optyTextBox
        Inherits System.Windows.Forms.TextBox
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            Const WM_PASTE As Int32 = &H302
            Select Case m.Msg
                Case WM_PASTE ' The clipboard contents were pasted... 
                        'Code to handle paste event.
            End Select
        End Sub
 
[COLOR="Red"]        MyBase.Wndproc(m)[/COLOR]
End Class
When you override a method like this it means that the base method wont be called. Yours will be called instead. If you need the base method to be called, you must do this explicitly in your overriding method.

In the case of WndProc, when you don't call the base method, the TextBox will not recieve any messages.
 
Try changing the class definition to
Visual Basic:
Public Class optyTextBox
    Inherits System.Windows.Forms.TextBox

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Const WM_PASTE As Int32 = &H302
        Select Case m.Msg
            Case WM_PASTE ' The clipboard contents were pasted... 
                'Code to handle paste event.
            Case Else
                MyBase.WndProc(m)
        End Select
    End Sub
End Class
If you are not interested in a particular message you need to pass it on to the default WndProc so it still gets processed.
 
Back
Top