简体   繁体   中英

c# .NET - Tabpage margin

I have a tabcontrol with 3 tabpages. I need to add a left margin to the first tabpage ( so move all tabpages move right of 200px ). How can I do it??

Using Visual Studio 2008 / c#

EDIT Reading again I think you're more looking for the controls on each page to be on the right of the tabs rather than moving the buttons.

As Hans suggests a panel would be the easiest way. But it's not pretty.

    private void Form1_Load(object sender, EventArgs e)
    {
        // Create spacer tab with a name long enough to reach the 200px mark
        TabPage spacer = new TabPage("..............................................................");
        tabControl1.TabPages.Insert(0, spacer);

        // Create a panel at the same location of the tab control.
        Panel spacerBlock = new Panel();
        spacerBlock.Name = "spacer";
        spacerBlock.Location = tabControl1.Location;
        spacerBlock.Width = 198;
        spacerBlock.Height = 20;

        this.Controls.Add(spacerBlock);

        spacerBlock.BringToFront();
    }

    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Ensure the user can't use the keyboard to somehow select the spacer tab.
        if (tabControl1.SelectedIndex == 0)
            tabControl1.SelectedIndex = 1;

        // Check if the second (first I guess) tab is selected and adjust the panel to keep the look consistant.
        if (tabControl1.SelectedIndex == 1)
            this.Controls["spacer"].Width = 198;
        else
            this.Controls["spacer"].Width = 200;
    }

You'll want to make sure the tab isn't selectable by the user via keyboard shortcuts thus the index change check.

Also note the panel will have to have its width adjusted if the second (first in your case) tab is selected due to the 3d GUI effect.

Honestly the hassle of taking into account the appearance settings of the end user to ensure the spacer tab's text and the panel width are correct length doesn't really make up for fancy look IMHO.

Only other option I could think of would be a tab panel with a 16px height. Again this would have to be adjusted depending on the end users appearance settings, not to mention the excess overhead in getting it all working.

If it's the AjaxControlToolkit tab control, add this CSS class:

.TabContainer .ajax__tab_header
{
    padding-left: 200px;
}

you would need a work-around for that because tab pages can't be moved. You might wanna place a groupbox inside the tabpage and then you can add all the controls inside the groupbox as you desire...

// tabPage 1
this.tabPage1.Controls.Add(this.groupBox1);

// groupBox1
this.groupBox1.Location = new System.Drawing.Point(200,6);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.AnyControls); //etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM