user1914530
user1914530

Reputation:

TabControl, TabPage avoid switch statement

I'm using an MVP supervising presenter pattern in a WinForms application. I have a view with a TabControl and a number of TabPages. I need to lazy load the contents of each tab page on selected as there is a noticeable lag when trying to populate the entire view.

I was hoping to hook into the TabPage.GotFocus event and provide a handler for when a given tab is selected but that does not appear to work. So I had to use the TabControl.SelectedIndexChanged event and a switch statement to get this to work. Having to put the switch statement in is a real pain. Is there a way to avoid the switch statement?

public partial class Form1 : Form
{
    public event EventHandler TabPage1Selected;

    public event EventHandler TabPage2Selected;

    public event EventHandler TabPage3Selected;


    public Form1()
    {
        InitializeComponent();
        this.tabControl1.SelectedIndexChanged += tabControl1_SelectedIndexChanged;

        this.tabPage1.GotFocus += tabPage1_GotFocus; //Doesn't work!
    }

    private void tabPage1_GotFocus(object sender, EventArgs e)
    {
        Debug.WriteLine("event fired for tabpage1");
    }

    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Debug.WriteLine("Raising tab selected event");
        EventHandler h = null;
        switch (this.tabControl1.SelectedIndex)
        {
            case 0:
                h = this.TabPage1Selected;
                break;
            case 1:
                h = this.TabPage2Selected;
                break;
            case 2:
                h = this.TabPage3Selected;
                break;
            default:
                break;
        }
        if (h != null) h(this, new EventArgs());
    }    
}

Upvotes: 0

Views: 2567

Answers (2)

jross
jross

Reputation: 1119

You could use the Enter Event. Select the TabPage in the Properties | Events list in the designer you can set the Enter event / or you can set the event programmatically. The enter fires each time the TabPage gets the focus…

public Form1()
{
    InitializeComponent();

    // I set tabPage1 & 2 event w/ the designer
    tabPage3.Enter += tabPage3_Enter;
}

private void tabPage1_Enter(object sender, EventArgs e)
{
    Debug.WriteLine("tabPage1_Enter");
}

private void tabPage2_Enter(object sender, EventArgs e)
{
    Debug.WriteLine("tabPage2_Enter");
}

private void tabPage3_Enter(object sender, EventArgs e)
{
    Debug.WriteLine("tabPage3_Enter");
}

Upvotes: 0

TheEvilPenguin
TheEvilPenguin

Reputation: 5682

You could subclass TabItem for each TabItem you want to add and implement a .Load() method on each one. Apart from subclassing or some overly complex dictionary/delegate solution, I don't think you'll avoid some form of flow control. Switch is the most appropriate form of flow control here.

I would look at finding something better than index to switch on - some sort of tab id or name. You don't want to have to rewrite this code if you rearrange your tabs later on.

Upvotes: 1

Related Questions