none
How to create an extended form base class?

    Question

  • I want to create a new form base with additional properties, methods and events. How do i do this? I tried creating the following (VB.Net code) but i cannot inherit it using the add inherited form wizard.

    Public MustInherit Class BaseForm
       Inherits System.Windows.Forms.Form
       ...
        Public MustOverride Property X()
    End Class

    And if i try to inherit it manualy it gives me an error message if i try to open it in the designer

    Public Class NewForm
       
    Inherits BaseForm
    End Class

    --error--
     The designer must create an instance of type 'WindowsApplication1.BaseForm' but it cannot because the type is declared as abstract.


    Thursday, September 29, 2005 3:58 AM

Answers

  • So how do i override the events?
    I need to create a base class that has some added methods, properties and events. But how do i create an overridable event, or something like it.

    You can't overwrite events or even raise events when you inherrit.
    To solve this you could do something like this

    Public Class formEx : Inherits Form
    Public Event MyEvent(ByVal sender As Object, ByVal e As EventArgs)

    'other code ommited

    Public
    Overridable Sub RaiseMyEvent(ByVal e As EventArgs)
       RaiseEvent MyEvent(Me, e)
    End
    Sub
    end
    class


     


    Now you can cal in your childform (inherrits from FormEx)
    RaiseMyevent or override RaiseMyEvent

    Make Sense ??

    but if would be better if i can use mustoverride so the inheritting classes will know that they must.

    You can raise a exception in the basefunction , so if the basefunction is not overriden it raise a exception

    Remco

    Tuesday, October 04, 2005 10:00 AM

All replies

  • Hi,

    You basically cannot inherit from an abstract class. Thus you can't inherit BaseForm but in your situation you could inherit the Form class (This is what the VS is doing when your creating a new form) and override its properties...

     

     

    cheers,

    Paul June A. Domag

    Thursday, September 29, 2005 5:08 AM
  • That is actually part of my problem.

    I want to create a type that "IS" a Form and "Has" some more properties, methods and events. And i want this type to be inheritable. Any ideas how to do this?
    Thursday, September 29, 2005 6:21 AM
  • Hi,

    Well you could remove the MustInherit in your class definition. This definition makes your class as an abstract. You can declare it as an interface or simply a class. Then just inherit it on your other class...

     

     

     

    cheers,

    Paul June A. Domag

    Thursday, September 29, 2005 9:17 AM
  • I cannot make it at interface since i need to inherit the form class but I could remove the mustinherit and just make the methods overridable. But what will i do with events? I tried shadowing but it doesn't seem to work.

    This is how i used it or how i want to use it. The property and method works but the event does not fire :-(

    '--- Calling Form
    Dim WithEvents f As BaseForm

    f = New MyForm 'this will be replaced by reflection assembly creatinstance
    f.Show()

    Private Sub f_MyEvent() Handles f.MyEvent
       MsgBox("myevent")
    End Sub

    '---Inherited form
    Inherits Base.BaseForm
    Public Shadows Event MyEvent()

    Public Overrides Sub MySub()
       '...something
    End Sub

    Public Overrides Property MyProperty() As Object
     
    Get
     End Get
     
     Set(ByVal Value As Object)
     
    End Set
    End Property

    Public Sub SomeWork()
       '...
       
    RaiseEvent MyEvent()
    End Sub

    '--Base Form
    Inherits System.Windows.Forms.Form
    Public Event MyEvent()

    Public
    Overridable Sub MySub()
    End Sub

    Public Overridable Property MyProperty()
       
    Get
       
    End Get
       
       
    Set(ByVal Value)
       
    End Set
    End Property


    Friday, September 30, 2005 6:16 AM
  • Hi,

    I can't seem to find where your calling your SomeWork()... As I understand this is where you raise your event...

     

     

    cheers,

    Paul June A. Domag

    Friday, September 30, 2005 8:11 AM
  • i tried calling it with a button in the inherited form, it does not work. I tried calling it from the form load, i didn't either.

    Maybe there's another way to do it? I just need something inheritable that the forms can use to notify the calling form that something happend.
    Friday, September 30, 2005 12:30 PM
  • Remove this line: Public Shadows Event MyEvent()

    and try it. What shadow does is create a new instance, it does not override.
    So when you handle BaseForm.MyEvent, it does not handle InheritedForm.MyEvent

    Friday, September 30, 2005 2:52 PM
  • Look, its real easy to do what you are asking, You seem to be doing a lot of extra stuff. Just create a class that inherits Form and add your properties....

    public class MyForm : Form
    {
           private void _NewProperty;
           public void NewProperty { get { return _NewProperty; } set { _NewProperty = value; } }
    }

    Then inherit your new class...

    public class MySecondForm : MyForm
    {
    }

    And thats all you have to do, no overwrites or anything is needed
    Friday, September 30, 2005 2:56 PM
  • So how do i override the events?

    I need to create a base class that has some added methods, properties and events. I ok with just declaring methods and properties (but if would be better if i can use mustoverride so the inheritting classes will know that they must). But how do i create an overridable event, or something like it.

    I do need to override in the inheritted form. That's the whole point. So i can declare the base form, just like a template or interface, have the code implemented latter.

    Thanks guys but i'm beginning to wonder if it possible.
    Tuesday, October 04, 2005 2:02 AM
  • So how do i override the events?
    I need to create a base class that has some added methods, properties and events. But how do i create an overridable event, or something like it.

    You can't overwrite events or even raise events when you inherrit.
    To solve this you could do something like this

    Public Class formEx : Inherits Form
    Public Event MyEvent(ByVal sender As Object, ByVal e As EventArgs)

    'other code ommited

    Public
    Overridable Sub RaiseMyEvent(ByVal e As EventArgs)
       RaiseEvent MyEvent(Me, e)
    End
    Sub
    end
    class


     


    Now you can cal in your childform (inherrits from FormEx)
    RaiseMyevent or override RaiseMyEvent

    Make Sense ??

    but if would be better if i can use mustoverride so the inheritting classes will know that they must.

    You can raise a exception in the basefunction , so if the basefunction is not overriden it raise a exception

    Remco

    Tuesday, October 04, 2005 10:00 AM