开发者

Is there a way to get the bounds of a TabPage in a Winforms TabControl?

I have a Form with only a single TabControl, with many tabs, where each tab has only square buttons side by side. I am trying to make it so that when the user c开发者_如何学运维licks on a tab, the form resizes itself to a size where you can see all the buttons in a particular tab or a size where you can see all the tabs, whichever is greater.

I am just curious if there is a way to query where the last control in a tab page is? So I can just do something like:

tabForm.Width = currentTabPage.UsedContentBorder + 10;

Or do I have to do this by adding all the controls and the sizes between them, etc?


You want to find out the maximum coordinates of all controls in a specific tab? Easy with LINQ:

int right = tab.Controls.Cast<Control>().Max(c => c.Right);
int bottom = tab.Controls.Cast<Control>().Max(c => c.Bottom);

Now, to properly choose the size of the form, I imagine you just have to figure out how much larger the Form is than its TabPages... I would guess something like this:

int extraWidth = form.Width - tabControl.SelectedTab.Width;
int extraHeight = form.Height - tabControl.SelectedTab.Height;

Then you just do

form.Size = new Size(right + extraWidth, bottom + extraHeight);

(the TabControl will resize automatically if its Anchor property is set to all four sides.) It occurs to me that this may malfunction if the user resizes the form very small... you may be able to compensate by calculating extraWidth and extraHeight in the Form.Load event and then saving those values for when you need them later.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜