Question

I've been trying to play with Visual Inheritance so that I can reuse a set of buttons in multiple forms.

Basically what I'm trying to achieve is, I want the buttons to have same visual behavior across the different forms, however, they should perform different actions depending on which form is inheriting it.

Suppose I have the following buttons in FormButtonBar

New | Edit | Search | Cancel | Close

They should be disabled or have their text and icons changed according to the current situation going on (I do that using .Tag), these are the alternative options displayed

Save | Save | Remove | Cancel | Close

Even though I got that working as supposed on forms that inherit this, I obviously want these buttons to deal with different content depending on which form I have them.

What I thought of was a way to call methods like saveNewItem() saveChangedItem() removeItem() that every form inheriting FormButtonBar should have.

But how do I call them from FormButtonBar?

For example:

    private void buttonSearchRemove_Click(object sender, EventArgs e)
    {

        if (buttonSearchRemove.Tag == "search")
        {

            //call the search form here and wait for something to be returned

            //the following 3 lines are the ones that switch text, icons and enabled/disabled on the buttons

            utils.hablitarBotoes(panelBotoes, "abc");
            utils.alternarBotoes(panelBotoes, "b");
            buttonBuscarExcluir.Text = "Excluir";

        }
        else if (buttonSearchRemove.Tag == "remove")
        {
            DialogResult reply = MessageBox.Show("Are you sure you want to remove it?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (reply == DialogResult.Yes)
            {

                  //CALL REMOVE METHOD THAT SHOULD BE IN THE FORM INHERITING THIS ONE, BUT HOW?

                  removeItem();

              }

                utils.hablitarBotoes(panelBotoes, "nbf");
                utils.alternarBotoes(panelBotoes, "");

                buttonNovoSalvar.Text = "New";
                buttonBuscarExcluir.Text = "Search";
                buttonAlterarSalvar.Text = "Edit";
            }
        }
Était-ce utile?

La solution

You should declare these methods in base class as virtual methods and then override them in child classes.

public class MyBaseClass
{
    public virtual void RemoveItems()
    {
    }
}

public class MyDerivedClass : MyBaseClass 
{
    public override void RemoveItems()
    {
       //your specific implementation for this child class
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top