Deny Switching to Tabpage in a tabcontrol

痞子三分冷 提交于 2020-01-06 06:57:20

问题


I have a form(C#) with a tab control and its has around five tab pages.

each of the tab have a few textboxes.

1) if a User is in say Tab A and edits certain fields i need to validate the text enetered if found invalid then i should not allow any tab switch ? is that possible?

2) Another case could be ... user edits some values and clicks on another tab, on doing so i need to check if the values that were enetered for Tab A is correct or not ? can i do this?

I am a novice to C#... so may be these questions sound very basic any help will be appreciated.

also i want to know what are these events of a tab page

Leave, validated or validating ?


回答1:


I had a similar problem, but thankfully I came across this MSDN page. Just set up a tab selecting event and add your logic to cancel/continue there.

http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.selecting.aspx




回答2:


You can disable a tab page. Is not the best/simplest way but is working. Here is how to do it: http://social.msdn.microsoft.com/forums/en-US/winforms/thread/985b41c3-a1de-4744-8875-63262d4c2718/




回答3:


You could hook up to the TabIndexChanged on the TabControl and have a variable that says if they are allowed to change or not and just change back to the orginal tab if they are not allowed.




回答4:


in your form Designer, you can add any tab that you want and limit users.

if(your_condition)
    this.tab1.Controls.Add(this.tabPage2);



回答5:


You can use the Selecting event of the TabControl. It is of type: TabControlCancelEventHandler and it have a parameter of type TabControlCancelEventArgs with the attribute Cancel.

    private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
    {
        if (e.TabPageIndex > 0  /* && some condition still not reached */)
        {
            e.Cancel = true;
        }

       //Avoiding a tabchange from Index Zero if some condition is not accomplished yet
       //e.TabPageIndex: is the new TabIndex
       //e.Cancel == true: makes the TabControl stay in the previous tab index
    } 


来源:https://stackoverflow.com/questions/2092273/deny-switching-to-tabpage-in-a-tabcontrol

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