By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,023 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,023 IT Pros & Developers. It's quick & easy.

Windows Forms Inheritance, Incomplete?

P: n/a
KK
Windows Forms Inheritance, Incomplete?

I was playing around with Windows Forms
and found out this Forms Inheritance feature.

The moment I saw that, I felt this can be
used effectively if the application
contains couople of forms which have
a consistant look and also shares
SOME similar functionality between the forms.

Lets say for example, I have 2 forms and both
forms have buttons as (New, Edit, Save, Close)

Both the forms before showing, should do a
security check(say in forms load event)

In the old vb scenerio, we would create 2 similar
forms seperately and do code cut and paste
for each form which have the similar functions.

But here, with forms inheritance this should
be automaticaly done. But what happense when
u inherit a form at the moment is, it only
sets the controls MODIFIER poperty to
say Protected, Proteted Internal etc..

But If I want to override the parent forms
functionality , say overriding CLOSE buttons
click method, I have to manually edit
the parents click event as;

protected virtual void btnClose_Click(object sender, System.EventArgs e)

and then edit the close button event in the
child form to;

protected override void btnClose_Click(object sender, System.EventArgs e)

And then, in the child forms' event registration for
CLOSE button should be removed/commented(the line below).

//this.btnClose.Click += new System.EventHandler(this.btnNew_Click);

Otherwise, when the child instance get created
it will register 2 events(cause it goes up the
inheritance hierachy and creates parent form instance
first and then come back to child form)
so if u click CLOSE button once,
it will call the close method twise!!!!!

My question is why they didn't automate this couple
of steps ? If a forms controls have been set to
Protected / Protected Internal, they could have
generate the code so that it will really
work.

I want to know what you guys think. May be
I have taken the whole thing from the wrong
end.

May be forms inheritance is there ONLY to get the
UI duplicated? is it?

Rgds
KK
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
KK,
Why go though all that work?

I would use the Template Pattern to introduce a virtual OnClose method to
the base form, that the derived form could override and alter the flow.

The base form's btnClose_Click event handler would call this OnClose method.
The base form's OnClose would have the default behavior (none?) for OnClose,
while the derived form's OnClose would do what was needed.

In Release builds I would consider making OnClose abstract, while in Debug
builds I would make it virtual, as the Forms Designer does not like abstract
base forms. In Debug builds, if the method is meant to be overridden, I
normally make the base.OnClose method simply throw a
NotImplementedException. This way I find out real quick if I forgot to
override the method ;-)
My question is why they didn't automate this couple
of steps ? If a forms controls have been set to
Protected / Protected Internal, they could have
generate the code so that it will really
work. My question would be, why automatic this when the Template Pattern seems to
be a cleaner more OOP solution? :-)

Hope this helps
Jay

"KK" <kk@kk.com> wrote in message
news:el*************@tk2msftngp13.phx.gbl... Windows Forms Inheritance, Incomplete?

I was playing around with Windows Forms
and found out this Forms Inheritance feature.

The moment I saw that, I felt this can be
used effectively if the application
contains couople of forms which have
a consistant look and also shares
SOME similar functionality between the forms.

Lets say for example, I have 2 forms and both
forms have buttons as (New, Edit, Save, Close)

Both the forms before showing, should do a
security check(say in forms load event)

In the old vb scenerio, we would create 2 similar
forms seperately and do code cut and paste
for each form which have the similar functions.

But here, with forms inheritance this should
be automaticaly done. But what happense when
u inherit a form at the moment is, it only
sets the controls MODIFIER poperty to
say Protected, Proteted Internal etc..

But If I want to override the parent forms
functionality , say overriding CLOSE buttons
click method, I have to manually edit
the parents click event as;

protected virtual void btnClose_Click(object sender, System.EventArgs e)

and then edit the close button event in the
child form to;

protected override void btnClose_Click(object sender, System.EventArgs e)

And then, in the child forms' event registration for
CLOSE button should be removed/commented(the line below).

//this.btnClose.Click += new System.EventHandler(this.btnNew_Click);

Otherwise, when the child instance get created
it will register 2 events(cause it goes up the
inheritance hierachy and creates parent form instance
first and then come back to child form)
so if u click CLOSE button once,
it will call the close method twise!!!!!

My question is why they didn't automate this couple
of steps ? If a forms controls have been set to
Protected / Protected Internal, they could have
generate the code so that it will really
work.

I want to know what you guys think. May be
I have taken the whole thing from the wrong
end.

May be forms inheritance is there ONLY to get the
UI duplicated? is it?

Rgds
KK

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.