How to make visual form inheritance in Windows Forms?

3

I have a form base that will be form 'Father', with 3 buttons in it. Add, Delete, and Change.

I'm going to use formbase to standardize my registration screens, so each registration screen when I click 'Record' will execute a different recording routine.

Then I have the following:

class frmCadastroFuncionario: Views.formulariobase
{

//herdando os botoes e o evento 'onclick'

}


Class formulariobase
 {

    public void Altera(string nome)
    {
       if (this.Form.Name.ToString() == "FuncionarioCadastro")
            //faça isso

    }

 }

So when I call the change function, I have to see what I'm going to execute inside the if because it depends on the form that I call the change function will make it be different. But this% w% I did not work. So I wanted to know if using inheritance has to know that if is using the visual and event inheritance of form .

    
asked by anonymous 05.11.2015 / 16:28

3 answers

4

In fact what you are doing does not make sense. It kills every advantage of inheritance.

If I understand you, you want to polymorphism . Then you transform this method into virtual and reimplement it in child classes.

class frmCadastroFuncionario: Views.formulariobase {
//herdando os botoes e o evento 'onclick'
    public override void Altera(string nome) {
        //faça alguma coisa específica aqui
    }

}

Class formulariobase {
    public virtual void Altera(string nome) {
        //faça alguma coisa
    }
}

In this way it will call the method of each class as it is instantiated. If, by chance, the daughter class needs to call what the parent class does with this method (I doubt it is the case in this example) you can call base.Altera(nome) , inside the daughter. This will call the top method.

Eventually this base class should be abstract , then the virtual method could even be de-implemented and always leave the child classes to implement.

It would be interesting to study more about object orientation before you start using it. There's a lot of good stuff right here on the site. It can be a powerful tool if well used. And terrible if abused.

    
05.11.2015 / 16:37
0

And if you give an override in this method Altera, where each child implements its own Altera() , I did not get to test but I believe it works.

//classe pai
public virtual void Altera() {}

//classe filho1
public override void Altera() 
{
    //implementação do form1
}

//classe filho2
public override void Altera()
{
    //implementação do form2
}
    
05.11.2015 / 16:37
0

In the case of form I find much more elegant to use event's. Within the click events to perform an action in the base form you call the event that are implemented in each inherited form:

[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
public event ActionFormEventHandler FormIncluirClick;

In the form 'child' the events appear:

In the code you implement the event:

private bool FrmCadastroCartorio_ActionFormIncluirClick()
{
        // implementar aqui..
}

In base form call event:

private void BtnSalvarClick(object sender, EventArgs e)
{
    if (ActionFormIncluirClick()){
    // cadastrou!
    }
}
    
08.07.2016 / 22:51