Solved

VB.net MyBase not valid within module

Posted on 2013-06-04
3
526 Views
Last Modified: 2013-06-17
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
Comment
Question by:mossmis
  • 2
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39219473
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
 

Author Comment

by:mossmis
ID: 39219675
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39219808
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Set form below another form 3 35
Get String split 5 48
Visual Studio editor? 1 38
LINQ - C# to VB convertion 12 48
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now