[英]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.