• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 641
  • Last Modified:

VB.net MyBase not valid within module

I have some code I want to reuse in several of my apps written in VB.net. I would like to put them in a module/class so I can reference them from other apps. One sub routine doesn't like being in a class\module and I was wondering how it coud be rewritten:

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        '
        'Have the base class handle all messages.
        MyBase.WndProc(m)
        '
        'Check if a new window has been created.
        If m.Result.ToInt32 = WM_CREATE Then
            '
            'Check the new window to see if it has the charateristics that we are looking for. Namely that is has the Title that we want to find.
            windowHandle = FindWindow(vbNullString, inputTitle)
            '
            'Make sure that the WindowHandle is not 0 which should mean it have a handle to the Inputbox window that we are wanting.
        ElseIf windowHandle > 0 Then
            '
            'Call the code to set the textbox to a password textbox. 
            setPassword()
            '
            'Reset the handle back to 0 since the window handle will be different each time you display Inputbox window.
            windowHandle = 0
        End If
    End Sub

Open in new window


VB doesn't let me put a Protected Overrrides in a module and it also doesn't like MyBase in a class or module. Any suggestions?
0
mossmis
Asked:
mossmis
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
WndProc() is specifically used with windows that have a message pump...so it can't be in a module since a module isn't a window.  You could encapsulate a NativeWindow() instance inside a class if you need to trap windows messages.
0
 
mossmisAuthor Commented:
I'm not that saavy in .net. I'm coming from vb6 and never really got that complex for me. I guess I could just paste this code in all my apps as a work around, but it wouldn't be as organized as I'd like. If you can elaborate more on how to use it, I'd like to give it a try.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Well...normally WndProc() would be in a form (or some kind of derived control) right?
Public Class Form1

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

        End Select
        MyBase.WndProc(m)
    End Sub

End Class

Open in new window


You could instead create a Class to act as a receiver for windows messages like this:
Public Class MyNativeWindow
    Inherits NativeWindow

    Private Const HWND_MESSAGE As Integer = -3

    Public Sub New()
        Dim cp As New CreateParams()
        cp.Parent = HWND_MESSAGE
        Me.CreateHandle(cp)
    End Sub

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

        End Select
        MyBase.WndProc(m)
    End Sub

End Class

Open in new window


Obviously you'd need to add more code so that it is registered for the kind of messages you want.  You can then simply add a Reference to this class project in new projects so you could you use it.  You'd still have to create an instance of it though.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now