Maneesh
Maneesh

Reputation: 101

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 ?

Upvotes: 0

Views: 9565

Answers (5)

David Silva-Barrera
David Silva-Barrera

Reputation: 1146

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
    } 

Upvotes: 2

user1951113
user1951113

Reputation:

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

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

Upvotes: 0

jnich91
jnich91

Reputation: 71

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

Upvotes: 4

Victor Hurdugaci
Victor Hurdugaci

Reputation: 28435

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/

Upvotes: 1

Peter
Peter

Reputation: 38585

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.

Upvotes: 0

Related Questions