Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Questions/Problems with subclassing controls and forms
Message
From
04/08/2010 21:08:44
 
General information
Forum:
ASP.NET
Category:
Object Oriented Programming
Miscellaneous
Thread ID:
01475116
Message ID:
01475232
Views:
45
Not sure yet what the problem is ... does form2 also inherit from baseform? Sorry, that's not quite clear from your description (or maybe it's just me <g>).

BTW, I would use an interface here if I were you. Something like this, off the top of my head (I apologize in advance if I've gotten the VB syntax wrong, I mostly know C#):
Public Interface IDoExit
    Sub DoExit()
End Interface
Then, your baseform has to implement the interface:
Public Class baseform
    Inherits Form
    Implements IDoExit
....
Then, your button only needs to know if its parent implements that interface:
  Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
        If TypeOf(Me.Parent) Is IDoExit Then
            CType(Me.Parent, IDoExit).DoExit()
            'MsgBox("Methode da")
        Else
            'MsgBox("Methode fehlt")
        End If
        MyBase.OnClick(e)
    End Sub
It's cleaner to use interfaces, and it's quicker because it avoids using reflection to get method names and invoke them.

I know this doesn't answer your question, but I think I need some clarification about form2 first because I think what you're trying to do should work.

~~Bonnie




>Hi there,
>
>moving an app from VFP to VB.Net I started building some classes but I am getting to the point where
>I am encountering some problems and need some advice. What I do is this:
>
>1) Build a Button class which serves as my base class:
>
>...
>Public Class cmdbase
>    Inherits System.Windows.Forms.Button
>...
>
>
>This class sets just some properties which all buttons have in common like size, color etc.
>
>2) I Build buttons for special purposes from that class:
>
>
>Public Class cmdexit
>    Inherits cmdbase
>
>
>This button gets a special text, an image and some special behaviour:
>
>
>    Public Sub New()
>        MyBase.New()
>        InitializeComponent()
>        'Me.Text = "&Ende"
>        Me.ImageAlign = System.Drawing.ContentAlignment.TopCenter
>    End Sub
>    Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
>        MyBase.OnPaint(pevent)
>        Me.Text = "&Ende"
>    End Sub
>  Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
>        Dim meth As MethodInfo = Me.Parent.GetType().GetMethod("DoExit")
>        If meth = Nothing Then
>            'MsgBox("Methode fehlt")
>        Else
>            'MsgBox("Methode da")
>            meth.Invoke(Me.Parent, Nothing)
>        End If
>        MyBase.OnClick(e)
>    End Sub
>
>
>There is a baseform which has a DoExit-Method which just executes the me.close() command.
>
>
>    Public Overridable Sub DoExit()
>        If Not Me.lIsRunning Then    ' A property to check if the form is busy
>            MsgBox("Base doexit")
>            Me.Close()
>        Else
>            MsgBox("Isrunning")
>        End If
>    End Sub
>
>
>This button shall look if his parent (a Form) has a Method doexit and if, shall execute it.
>When I put this button on a form inherited from my baseform (My startform) , everything works fine.
>But when I load another form from my startform with
>
>
> dim frm as new form2
>frm.showdialog()
>
>
> the method in the baseform is executed (I have same messageboxes implemented for testing reasons) but the
>me.close-command does not have any impact, the form does not close. If I put another button for testing purposes
>on the form which issues a close()-command, it works, if I change the calling of form2 to non-modal, it works too.
>Could anybody tell me, what I am doing wrong.
>
>Thanks in advance
>
>Thomas
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform