简体   繁体   中英

Resizing tabs individually and programmatically in C# with Winforms

I have a form with a tab control.

I want each tab to have its own interface with its own size, so that I can have button layouts as necessary.

I am, in this example, only altering height.

Currently I have the default form height and default tab height set (set by tab index 0).

I need a programmatic way to set each tab's height individually, and on event selectedIndexChanged , I am able to resize the form as needed relative to the currently selected tab, but I don't know how to change each tab's height individually.

How can I achieve this?

It sounds like you are talking more about the height of the form based on the selected tab than the height of each individual tab item.

Assuming the TabControl is Dock-Filled on a parent form, you can try this code to resize the form's height based on the content of the TabPage:

void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
  var controls = tabControl1.SelectedTab.Controls.Cast<Control>();
  if (controls.Any()) {
    this.Height = controls.Max(x => x.Bottom) + 72;
  }
}

The routine finds the lowest based control on the TabPage and then adds a fudge number of 72 to account for the height of the form's non-client area and other miscellaneous spacing issues.

But note, constantly changing the height of the form based on a tab selection can be a bit jarring to the end user, and is probably not considered a popular UX implementation.

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