开发者

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 d开发者_StackOverflow社区oing 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 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
    } 


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/


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.


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜