简体   繁体   English

使用Winforms在C#中以编程方式分别调整选项卡的大小

[英]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). 目前,我已经设置了默认的表单高度和默认的标签高度(由标签索引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. 我需要一种编程方式来分别设置每个选项卡的高度,并且在事件selectedIndexChanged ,能够根据需要相对于当前选定的选项卡调整窗体的大小,但是我不知道如何分别更改每个选项卡的高度。

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: 假设TabControl在父窗体上是Dock-Filled,您可以尝试以下代码根据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. 该例程在TabPage上找到最低的基于控件的控件,然后添加fudge数72以说明表单的非客户区域的高度和其他其他间距问题。

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. 但是请注意,根据选项卡的选择不断更改表单的高度可能会给最终用户带来麻烦,并且可能不被认为是流行的UX实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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