views:

514

answers:

2

I'm building a small tabbed c# Form and I'd like each tab page to have some common features, notably, an OK button and an error message and to have a space for the specific form fields.

Has anyone else done something similar and how did you approach it?

A: 

Several ideas:

  • Keep the common controls outside the tabpanel;
  • Extend the TabPage/TabControl
  • Create a base UserControl with the common buttons and make usercontrols that inherit from it. Then place one inherited usercontrol per TabPage.
Vilx-
+3  A: 

This is easy to do without extending either TabControl/TabPage.

Define one UserControl, and put the common elements on it you want on every TabPage.

On the Form: go ahead and design the TabPage specific controls you want for each TabPage : make sure they are not going to visually overlap with the common controls once the UserControl has been added.

In the Form Load Event of your main Form do something like this :

    // form scoped variable to hold a referece to the current UserControl
    private UserControl1 currentUserControl;

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach(TabPage theTabPage in tabControl1.TabPages)
        {
            currentUserControl = new UserControl1();

            theTabPage.Margin = new Padding(0);
            theTabPage.Padding = new Padding(0);

            theTabPage.Controls.Add(currentUserControl);

            currentUserControl.Location = new Point(0,0);

            currentUserControl.Dock = DockStyle.Fill;

            currentUserControl.SendToBack();
        }
    }

Even though the 'SendToBack isn't really required here it is "insurance" that your UserControl with the 'Okay button and TextBox for an error message are placed behind the individual controls you have assigned to each TabPage.

BillW
Those of you who down-voted: understanding why don't like this solution would be very appreciated. thanks,
BillW
@BillW Actually, some ass marked this as spam. Now, I do not know if this is a good answer but I do know it is not spam. So, +1 to help make up for the rep loss and neutralize your answer's rank.
Sinan Ünür
Hi BillW,I like this solution. I know it means I lose designer support in terms of "seeing" the control, but I think it does the job nicely, thanks.
Tim Almond
@Tim Actually, you don't have to "lose seeing" : just drag and drop the UserControl all TabPages share from the ToolBox at Design-Time onto each TabPage : make sure it's "docked fill" and sent to back. You can then overlay your other controls on it. I added the UserControls in a loop in the Form Load Event, just out of habit. Another alternative, which I have not explored, would be to go ahead and create a derived TabPage with the UserControl embedded in it, and appropriate access to its whatever exposed through public methods or properties.
BillW
@Sinan Thanks for this act of consideration and kindness !
BillW
@BillW I do not do this for downvoted answers, esp when I have no idea whether the answer is correct, but clearly the answer was not spam.
Sinan Ünür