Sviluppo e Sviluppi
sabato, marzo 05, 2005
 

Using inherited forms: specify different event response in child forms
Delphi has this great features.
This is a logical (and graphical) conseguence of Object Oriented Inheritance. IDE offers a simple way to work with inherited forms: create master form and childs form that will include all master's controls.
It's really useful in a lot of cases:

  1. define a template form that include all control and graphical settings common to all program
  2. using same logic for all common control: for example include an about dialog clicking the company's image
  3. make a correspondance between hierarchic OO library and user interation

Now you can use the same feature using VS and all his languages (J#, C#, VB) but there are some small differences related to the Event handlers inherited by child form (Delphi hide some implementation details and allows to specify different events on the master control).This is an example by using C#:

  1. create a window form (Form1)
  2. include a button (button1)
  3. insert an event handler on mouse click:
    private void button1_Click(object sender, System.EventArgs e)
    {
    MessageBox.Show("Click on Form 1!");
    }

    Modify private modifier in public virtual modifier so that you can override this method in child forms
  4. build the project
  5. create an inherited form (Form2) (Menu Project --> Add Inherited Form)
  6. button1 will be present also on Form2
  7. create a method to override button1_Click:
    public override void button1_Click(object sender, System.EventArgs e)
    {
    MessageBox.Show("Click on Form 2!");
    }

  8. Form2 specify a different response to Button1 click. If you want to call inherited method you can use:
    base.button1_Click(sender,e);

Delphi hides the creation of method overriding and use keyword inherited to call parent method


Comments: Posta un commento

<< Home

Powered by Blogger