CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2006
    Posts
    645

    Tab Control Question

    Hi ,
    I have a tab control with 3 tab pages in it. I have to catch the event when the selected tab is changed. Initially I though it was tabPage_Click event. Later on I came across, tabControl_SelectedIndexChanged event for the tab control itself. And I handled it as under...
    Code:
            private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
            {
                 switch (tabControl1.TabIndex)
                 {
                     case 0:
                         OnTab1();
                         break;
                     case 1:
                         OnTab2();
                         break;
                     case 2:
                         OnTab3();
                         break;
                 }
            }
    Yet even when I change the the tab page from tab page 1 to other...OnTab1() is being executed, no matter what. So please tell me what shall use to switch? I also read some where that the tabpage_Click event handles any click in side the tab page's client area. So that is not what I am looking at. I have also looked at SelectTab() and DeselectTab() methods and more...Yet to find a solution. Please help
    Thanks
    Bhushan

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Tab Control Question

    Try this :

    Code:
            private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
            {
    //Note I'm using the default names for the Tab Pages, obviously if yours are different, you'll have to make the necessary changes to this code...
                switch (tabControl1.SelectedTab.Name)
                {
                    case "tabPage1":
                        OnTab1();
                        break;
                    case "tabPage2":
                        OnTab2();
                        break;
                    case "tabPage3":
                        OnTab3();
                        break;
                }

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Tab Control Question

    How about using the Enter event of the TabPage class? This is what I usually do:

    Code:
    public MyForm( )
    {
        TabPage page = new TabPage( );
        page.Enter += MyDelegate;
        tabControl1.TabPages.Add( page );
    }
    
    private void MyDelegate( object sender, EventArgs e )
    {
        //Now you know which tab page was entered and each has its own method. 
    }

  4. #4
    Join Date
    Jun 2006
    Posts
    645

    Re: Tab Control Question

    @HanneSThEGreaT....u r simply gr8 savior. By answering this query, you have solved many of my other problems. On other note, is this the standard/practical/efficient way of handling navigation within the tab control?

    @BigEd781:
    It seems that u r very much into delegate and stuff. I suppose, u must be using EventHandler, RaiseEvent, Event, delegate, multicast delegate, etc. quite often. I prefer to use and stick with the standard events as far as possible. But when it is not possible, I go for delegation. However, that is due to the fact that I am not very much confident working with them...I know it is easy...but when u have lot of those, it becomes kinda difficult to keep a track of those and readability is reduced...atleast I have seen a few code snippets that over use them. Can you present me any tips on how you decide when to use your own events ahead of the ones provided? How will you implement, my code here using your approach? I would really appreciate if u present me the code...your way....

    Thanks
    Bhushan

  5. #5
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Tab Control Question

    Quote Originally Posted by bhushan1980 View Post
    @BigEd781:
    It seems that u r very much into delegate and stuff. I suppose, u must be using EventHandler, RaiseEvent, Event, delegate, multicast delegate, etc. quite often.
    Quote Originally Posted by bhushan1980 View Post
    I prefer to use and stick with the standard events as far as possible. But when it is not possible, I go for delegation.
    these are the same things. the difference is that in the second case vistual studio helps and creates for you an event handler and attaches it to the actual event. an EventHandler is a delegate that is defined like that:
    Code:
    public delegate void EventHandler(Object sender, EventArgs e)
    Last edited by memeloo; February 12th, 2010 at 06:17 AM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  6. #6
    Join Date
    Mar 2007
    Posts
    59

    Re: Tab Control Question

    The solution of HanneSThEGreaT is really nice and easy, but it's not robust since it's base on string that can easily change in the designer. Probably using the "tabPageObject.Name" property could be a better solution.

    But The enter Even is better in my Opinion since you don't have to create a switch case for it. Like memeloo said, it's exactly the same thing as the SelectedTabChange Event. In order to understand, you should check the code generated by the designer in the "<form>.Designer.cs".

    A delegate is just a glorified pointer to function (with many useful features). From any event, you can add a call to any function you create or exists that match the signature of the delegate (function definition). But what does the designer, it helps you generating 2 things:

    1. The function that will be called (void myFunction() {your code})
    2. The binding to the event (myControl.Event += myFunction)

    When it's not available in the list, you can do it yourself since not all events are available in the designer.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Thumbs up Re: Tab Control Question

    Quote Originally Posted by angedelamort View Post
    The solution of HanneSThEGreaT is really nice and easy, but it's not robust since it's base on string that can easily change in the designer. Probably using the "tabPageObject.Name" property could be a better solution.

    But The enter Even is better in my Opinion since you don't have to create a switch case for it. Like memeloo said, it's exactly the same thing as the SelectedTabChange Event. In order to understand, you should check the code generated by the designer in the "<form>.Designer.cs".

    A delegate is just a glorified pointer to function (with many useful features). From any event, you can add a call to any function you create or exists that match the signature of the delegate (function definition). But what does the designer, it helps you generating 2 things:

    1. The function that will be called (void myFunction() {your code})
    2. The binding to the event (myControl.Event += myFunction)

    When it's not available in the list, you can do it yourself since not all events are available in the designer.
    Good Answer! That is indeed well said!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured