Click to See Complete Forum and Search --> : Tab Control Question


bhushan1980
February 12th, 2010, 01:19 AM
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...

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

HanneSThEGreaT
February 12th, 2010, 01:54 AM
Try this :

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;
}

BigEd781
February 12th, 2010, 02:30 AM
How about using the Enter event (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.enter.aspx) of the TabPage class? This is what I usually do:


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.
}

bhushan1980
February 12th, 2010, 04:15 AM
@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

memeloo
February 12th, 2010, 05:14 AM
@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.
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:

public delegate void EventHandler(Object sender, EventArgs e)

angedelamort
February 12th, 2010, 08:44 AM
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.

HanneSThEGreaT
February 12th, 2010, 03:19 PM
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! :thumb: That is indeed well said! :thumb: