简体   繁体   中英

Deny Switching to Tabpage in a tabcontrol

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 ?

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

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/

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

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

您可以连接到TabControl上的TabIndexChanged并具有一个变量,该变量指示是否允许更改它们,如果不允许,则仅更改回原始选项卡。

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
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM