簡體   English   中英

從基本表單派生表單的調用方法

[英]Call method on derived form from the base form

我一直在嘗試使用視覺繼承,以便可以以多種形式重用一組按鈕。

基本上,我想要實現的是,我希望按鈕在不同的表單上具有相同的視覺行為,但是,它們應該根據繼承它的表單執行不同的操作。

假設我在FormButtonBar具有以下按鈕

新增| 編輯 搜索| 取消|

應該禁用它們,或者根據當前情況更改其文本和圖標(我使用.Tag進行此操作),這些是顯示的替代選項

保存 保存 刪除| 取消|

即使我在繼承該表單的表單上按預期工作,但顯然我希望這些按鈕根據我擁有的表單來處理不同的內容。

我想到的是一種方法,該方法可以調用每個繼承FormButtonBar表單FormButtonBar應該具有的諸如saveNewItem() saveChangedItem() removeItem()

但是,如何從FormButtonBar調用它們呢?

例如:

    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";
            }
        }

您應該在基類中將這些方法聲明為虛擬方法,然后在子類中覆蓋它們。

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

public class MyDerivedClass : MyBaseClass 
{
    public override void RemoveItems()
    {
       //your specific implementation for this child class
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM