Change accept button with tabs

假如想象 提交于 2019-12-30 07:32:16

问题


I have a windows form application written in C# and it has three tabs and I would like the accept button change with the active tab. Like When I am in tab 1 I want button _1 to be the accept button but when I am in tab 3 I want button_3 to be my accept button. I cannot figure out how to do this and maybe I'm not using the correct terms in my searches but I cannot find any good resources online showing me how to do this.


回答1:


Best guess would be to hook in to the SelectedIndexChanged event on the tab control and change the AcceptButton depending on which tab is selected. Pseudo-code:

Form_OnLoad(...)
{
    this.tabControl.SelectedIndexChanged += (s,e) => {
        TabControl tab = s as TabControl;
        switch (tab.SelectedIndex){
            case 3:
                this.AcceptButton = this.button_3;
                break;
            case 2:
                this.AcceptButton = this.button_2;
                break;
            case 1:
            default:
                this.AcceptButton = this.button_1;
                break;
        }
    };
}

Or something of the sort.




回答2:


You can do different things based on the tab that is currently selected by using the following code in the AcceptButton_Click event handler:

if (tabControl1.SelectedTab == tabPage1)
{
    //Do something
}
else if (tabControl1.SelectedTab == tabPage2)
{
    //Do something different
}

If you prefer to work with strings, each tab page has a unique name:

switch (tabControl1.SelectedTab.Name)
{
    case "Tab1Name":
        //Do something
        break;
    case "Tab2Name":
        //Do something different
        break;
}

If this answered your question, please mark it as the answer to your question.




回答3:


TabControl have SelectedIndexChanged event.

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (tabControl1.SelectedTab == tabControl1.TabPages["RenewalTab"])
        {
            this.AcceptButton = btnRenewal;
        }
        else if (tabControl1.SelectedTab == tabControl1.TabPages["SellerTab"])
        {
            this.AcceptButton = btnSeller;
        }
    }


来源:https://stackoverflow.com/questions/14045825/change-accept-button-with-tabs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!