How to make visual form inheritance in Windows Forms?

Asked

Viewed 1,689 times

3

I have a form base that will be the form 'Dad, with three buttons on it. Add, Delete and Change.

I’m gonna use the formbase to standardize my registration screens, so each registration screen when you click 'Record', will perform a different recording routine.

So I have this:

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 alter function, I have to see what I’m going to perform inside the if because it depends on the form that I call the alters function will make it different. But this if that I did does not work. So I wanted to know if using inheritance has how to know form using the visual and event heritage of the formbase.

3 answers

4


In fact, what you’re doing makes no sense. It kills every advantage of inheritance.

If I understand, you want to do polymorphism. Then you turn this method into virtual and reimplementation in the daughter 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
    }
}

I put in the Github for future reference.

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

Eventually this base class should be abstract, then the virtual method could even be without implementation and always leave for the child classes to implement.

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

  • Thanks for the example will really help me, but in the case of the 'On click' event of the button that c# provides, when I am in the form that is inheriting it ignores the event that I implement there, So from what I understand I have to make it virtual in the base form and then override the frmCadastroFuncinario ? I am in the service(I can not test at the moment), but I thank you from now on the answer!!

  • I cannot say why this is not covered in your question. Open a new question by putting these details. C# provides nothing. Your code provides something. You should not ignore it if it is correct. But you would need to analyze the actual code. The virtual only serves to be able to exchange the implementation. Inheritance is automatic, unless the method is privado. This may be the case.

  • OK ! Thank you very much !

  • Coming home I got the expected result ! The event 'Onclick' comes private so just put it as virtual public and implement it in the child form with override! Thanks again and that’s what I always say, sharing knowledge and good manners of OO is how we learn! A great hug.

0

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

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

In the 'child' form the events appear:

inserir a descrição da imagem aqui

In the code you implement the event:

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

In the base form calls the Event:

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

0

And if you override this Altera method, where each child implements its own Altera(), I didn’t get to testing 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
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.