我可以强制 TabControl 的 TabPages 在*它们被选中之前*调整大小吗? [英] Can I force a TabControl's TabPages to resize *before* they're selected?

查看:25
本文介绍了我可以强制 TabControl 的 TabPages 在*它们被选中之前*调整大小吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现 TabPages 不会使用 TabControl 调整大小,直到之后它们被选中.这对我来说是一个大问题,因为我有一个面板,它锚定在所有四个边缘上并共享 - 通过设置它的 Parent 属性 - 在 TabControl 上的几个页面中的两个页面之间共享形式.因此,如果我选择一个从不包含面板的选项卡,调整表单的大小,然后选择一个包含它但目前不包含的选项卡,则面板不会调整大小.

I've just discovered that TabPages don't resize with a TabControl until after they're selected. This is a big problem for me because I have a panel that's anchored on all four edges and shared – by setting its Parent property – between two of several pages on a TabControl that is fill-docked to a resizable form. So, if I select a tab that never contains the panel, resize the form, and then select a tab that will contain it but doesn't presently, the panel doesn't get resized.

我发现填充对接面板而不是锚定它可以正常工作,但如果可能的话,我宁愿避免这种情况,因为共享面板不会延伸到页面的边缘,并且它的大小可以各不相同,所以我必须弄乱每个页面的填充大小,而不是简单地改变面板的大小.

I've found that fill-docking the panel rather than anchoring it works okay, but I'd rather avoid that if at all possible, as the shared panel doesn't extend to the edges of the pages and its size can vary, so I'd have to mess around with the padding size of each page rather than simply altering the panel's size.

我已经在 VB 和 C# 中对此进行了测试,它们的行为方式相同.我认为针对这个已知错误给出的解决方案可能会奏效,但不幸的是它没有.

I've tested this in both VB and C# and they both behave the same way. I thought the solution given for this known bug might do the trick, but unfortunately it doesn't.

我还能做些什么来解决这个问题,例如强制调整页面大小?

Is there anything else I can do, such as force the pages to resize, to get around this problem?

编辑 – 在 VS 2010 中重现此内容的步骤:

EDIT – Steps to reproduce this in VS 2010:

  • 将 TabControl 添加到可调整大小的表单,填充它并添加第三个选项卡.

  • Add a TabControl to a resizable form, fill-dock it and add a third tab.

将面板添加到第三个选项卡,在两侧留出足够的空间,将其锚定到所有四个边缘并将其背景色设置为与 tabPage 不同的颜色,以便您可以在运行时看到它.

Add a Panel to the 3rd tab, leaving plenty of space at the sides, anchor it to all four edges and set its BackColor to something different than the tabPage's so you can see it at runtime.

将这行代码添加到 SelectedIndexChanged 事件中:

Add this line of code to the SelectedIndexChanged event:

VB

If TabControl1.SelectedIndex > 0 Then Panel1.Parent = TabControl1.SelectedTab  

C#

if (tabControl1.SelectedIndex > 0) panel1.Parent = tabControl1.SelectedTab;

  • 在选择第一个选项卡时运行/调试表单并调整其大小.

    • Run/debug the form and resize it whilst the 1st tab is selected.

      选择第二个选项卡 - 您将看到面板没有调整大小.

      Select the 2nd tab – you will see the panel hasn't resized.

      推荐答案

      作为一种解决方法,Hans Passant 建议我将面板放在第一个 TabPage(隐藏)上,然后在事件处理程序中设置它的 Visible 属性.正如我所解释的,这不会完全奏效——因为它不仅仅是启动时的一次性"问题;它发生每当在选择非面板显示选项卡时调整表单大小,然后选择当前不包含面板的面板显示选项卡 - 但它确实为我指明了正确的方向为了答案.即根据选择了哪个标签来设置面板的 Visible 属性,并且始终将面板移动到选定的标签,而不管面板是否显示在其上——EG:

      As a workaround Hans Passant suggested I put the panel on the first TabPage (hidden) then set its Visible property in the event handler. As I explained, that wouldn't quite work – as it isn't just a "once-only" problem at start up; it occurs whenever the form is resized while a non-panel-displaying tab is selected, then selecting the panel-displaying tab that doesn't currently contain the panel – but it did point me in the right direction for the answer. Which is to set the panel's Visible property according to which tab is selected and always move the panel to the selected tab, regardless of whether the panel is displayed on it – EG:

      Private Sub TabControl1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
          Panel1.Visible = (TabControl1.SelectedTab Is TabPage2) OrElse (TabControl1.SelectedTab Is TabPage3)
          Panel1.Parent = TabControl1.SelectedTab
      End Sub
      

      这篇关于我可以强制 TabControl 的 TabPages 在*它们被选中之前*调整大小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆