Forms Inheritance, Incomplete?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KK

    #1

    Forms Inheritance, Incomplete?

    ** Posting it here cause after couple of days no body responded.**

    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.EventArg s e)

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

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

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

    //this.btnClose.C lick += new System.EventHan dler(this.btnNe w_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?

    Regards
    KK


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Forms Inheritance, Incomplete?

    KK,
    ** reposting my answer from a couple of days ago! **

    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
    NotImplementedE xception. This way I find out real quick if I forgot to
    override the method ;-)
    [color=blue]
    > 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.[/color]
    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:uE%23OXTvS DHA.632@tk2msft ngp13.phx.gbl.. .[color=blue]
    > ** Posting it here cause after couple of days no body responded.**
    >
    > 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.EventArg s e)
    >
    > and then edit the close button event in the
    > child form to;
    >
    > protected override void btnClose_Click( object sender, System.EventArg s e)
    >
    > And then, in the child forms' event registration for
    > CLOSE button should be removed/commented(the line below).
    >
    > //this.btnClose.C lick += new System.EventHan dler(this.btnNe w_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?
    >
    > Regards
    > KK
    >
    >[/color]


    Comment

    • KK

      #3
      Re: Forms Inheritance, Incomplete?

      Hi Jay/Martin

      Thanks for the explanation. I got ur point Jay :o)
      I didnt checked the patterns!!! And If I manage to
      find a problem in that i'll ask a question again :ppp

      Thanks again!
      KK[color=blue]
      >[color=green]
      > > 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.[/color]
      > My question would be, why automatic this when the Template Pattern seems[/color]
      to[color=blue]
      > be a cleaner more OOP solution? :-)
      >
      > Hope this helps
      > Jay
      >[/color]


      Comment

      Working...