Vivek Natani

I'm trying to inherit one windows form (inheritForm) from another windows from (baseForm). I want to override the the baseForm's some events with the inheritForm events (let's day I want to override the form_click event). Can we really do this. I mean does the design of .net framework 2.0 support this If yes, it'd be gr8 if you can post few lines of code.

Thanks, Vivek



Re: Windows Forms General Inheriting the windows form from another windows form

Andrej Tozon

Quick example:

Base form:

public partial class BaseForm : Form
{
   
public
BaseForm()
    {
        InitializeComponent();
    }
   
private void BaseForm_Click(object sender, EventArgs
e)
    {
       
MessageBox.Show("Hello from base form"
);
    }
}

Inherited form:

public partial class InheritedForm : BaseForm
{
   
public
InheritedForm()
    {
        InitializeComponent();
    }
   
protected override void OnClick(EventArgs
e)
    {
       
MessageBox.Show("Hello from inherited form"
);
       
base
.OnClick(e); //Optional, try with and without calling this
    }
}

Andrej






Re: Windows Forms General Inheriting the windows form from another windows form

Vivek Natani

Sorry that I forgot to mention I'm using VB.Net. Could you post the same in VB if you have it handy. Well thanks I've done it myself.

Could you tell how this could be done for a button click event. This button is present in the Base Form and I want to override this event with the inherited one.

 





Re: Windows Forms General Inheriting the windows form from another windows form

Andrej Tozon

Here's the VB version:

Base form:

Public Class BaseForm
   
Private Sub BaseForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase
.Click
        MessageBox.Show(
"Hello from base form"
)
    
End
Sub
End
Class

Inherited form:

Public Class InheritedForm
   
Protected Overrides Sub OnClick(ByVal e As
System.EventArgs)
        MessageBox.Show(
"Hello from inherited form"
)
       
MyBase.OnClick(e)
'Optional, try with and without calling this
   
End
Sub
End
Class

Andrej

P.S. Inherited form is defined as

Partial Class InheritedForm
   
Inherits BaseForm

[You should add inherited form as: Add New Item | Inherited Form | (select BaseForm)]






Re: Windows Forms General Inheriting the windows form from another windows form

Vivek Natani

Thanks a lot..one more query.. Could you tell how this could be done for a button click event This button is present in the Base Form and I want to override this event with the inherited one.





Re: Windows Forms General Inheriting the windows form from another windows form

Andrej Tozon

I think this would be best using overridable function:

In base form, put:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   
FunctionWhenButtonClicked()
End Sub

Overridable Sub FunctionWhenButtonClicked()
   
MessageBox.Show(
"I'm Button!")
End Sub

In inherited form, put:

Public Overrides Sub FunctionWhenButtonClicked()
   
MessageBox.Show(
"No, I'm Button!!!")
End Sub

How this works is that base form's FunctionWhenButtonClicked is marked as overridable. In base form, it is called when button is clicked. This way, in inherited form, you don't actually override button's click, just the function which is called when button is clicked.

Andrej

 






Re: Windows Forms General Inheriting the windows form from another windows form

Bigul


I followed ur method but i got this error

Warning 1 The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file:

Form2 --- The base class 'Test1.Form1' could not be loaded. Ensure the assembly has been referenced and that all projects have been built. 0 0

pls help me

thanks

Bigul





Re: Windows Forms General Inheriting the windows form from another windows form

sirjis

Try compiling before opening Form2 in the designer. Or just don't open Form2 in the designer, but just use its code.





Re: Windows Forms General Inheriting the windows form from another windows form

Bigul

thank u sirjis.

this is the second time am using this method for a project. There is no runtime error while using this method.
but sometimes am getting this error

Warning 1 The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file:

Form2 --- The base class 'Test1.Form1' could not be loaded. Ensure the assembly has been referenced and that all projects have been built. 0 0

while selecting Form2 in IDE

thanks

Bigul