关于 TabControl 中事件特殊性的问题

发布于 2024-07-07 00:51:05 字数 3537 浏览 6 评论 0原文

下面我对一个特殊问题进行了一些演示。

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public class TestForm : Form
    {
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.TextBox textBox1;

        public TestForm()
        {
            //Controls
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.textBox1 = new System.Windows.Forms.TextBox();

            // tabControl1
            this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Location = new System.Drawing.Point(12, 12);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(260, 240);
            this.tabControl1.TabIndex = 0;
            this.tabControl1.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl1_Selected);

            // tabPage1
            this.tabPage1.Controls.Add(this.textBox1);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Size = new System.Drawing.Size(252, 214);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "tabPage1";

            // tabPage2
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Size = new System.Drawing.Size(192, 74);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "tabPage2";

            // textBox1
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.Location = new System.Drawing.Point(6, 38);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(240, 20);
            this.textBox1.TabIndex = 0;

            // TestForm
            this.ClientSize = new System.Drawing.Size(284, 264);
            this.Controls.Add(this.tabControl1);
            this.Name = "Form1";
            this.Text = "Form1";
        }

        //Tab Selected
        private void tabControl1_Selected(object sender, EventArgs e)
        {
            this.Text = "TextBox Width: " + this.textBox1.Width.ToString();
        }
    }

    //Main
    static class Program
    {
        static void Main()
        {
            Application.Run(new TestForm());
        }
    }

}

如果运行上面的 C# 代码,您将得到一个包含选项卡控件的小窗体。 在选项卡控件中,第一个选项卡上有一个文本框。 如果您按照以下步骤操作,您将看到问题:

  1. 选择 tabPage2(在表单标题中报告 textBox1 的宽度)
  2. 调整表单大小
  3. 选择 tabPage1(报告错误的 textBox1 宽度)

有什么想法吗? 文本框显然比所报告的要大。 如果您再次单击 tabPage2,则会更新正确的尺寸。 显然有一个事件更新了textBox1的宽度。 选择 tabPage1 时可以触发此操作吗?

I have a little demonstration below of a peculiar problem.

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public class TestForm : Form
    {
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.TextBox textBox1;

        public TestForm()
        {
            //Controls
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.textBox1 = new System.Windows.Forms.TextBox();

            // tabControl1
            this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Location = new System.Drawing.Point(12, 12);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(260, 240);
            this.tabControl1.TabIndex = 0;
            this.tabControl1.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl1_Selected);

            // tabPage1
            this.tabPage1.Controls.Add(this.textBox1);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Size = new System.Drawing.Size(252, 214);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "tabPage1";

            // tabPage2
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Size = new System.Drawing.Size(192, 74);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "tabPage2";

            // textBox1
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.Location = new System.Drawing.Point(6, 38);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(240, 20);
            this.textBox1.TabIndex = 0;

            // TestForm
            this.ClientSize = new System.Drawing.Size(284, 264);
            this.Controls.Add(this.tabControl1);
            this.Name = "Form1";
            this.Text = "Form1";
        }

        //Tab Selected
        private void tabControl1_Selected(object sender, EventArgs e)
        {
            this.Text = "TextBox Width: " + this.textBox1.Width.ToString();
        }
    }

    //Main
    static class Program
    {
        static void Main()
        {
            Application.Run(new TestForm());
        }
    }

}

If you run the above C# code you will have a small form containing a tabcontrol. Within the tabcontrol is a texbox on the first tab. If you follow these steps you will see the problem:

  1. Select tabPage2 (textBox1's width is reported in the form title)
  2. Resize the form
  3. Select tabPage1 (The wrong textBox1 width is reported)

Any ideas what is going on here? The textbox is obviously bigger than what is being reported. If you click again on tabPage2 the correct size is then updated. Obviously there is an event updating the width of textBox1. Can i trigger this when tabPage1 is selected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

意中人 2024-07-14 00:51:05

如果您通过向 textbox1.Resize 事件添加事件处理程序来稍微修改代码,您将看到会发生什么。
tabPage1.Selected 事件在调整选项卡页中的控件大小之前发生,因此当您检查文本框的宽度时,您是在调整大小之前检查它。

通常这不会成为问题,因为调整大小之后会正确完成,但我猜您会使用文本框的大小来做什么?

您应该能够编写自己的 TabControl 来解决此问题,但您必须进行试验才能了解此处的效果。

If you modify your code a little by adding an event handler to the textbox1.Resize event you will see what happens.
The tabPage1.Selected event occurs before the controls in the tab page is resized so when you check the width of the textbox you are checking it before it is resized.

Normally this wouldn't be a problem, for the resizing is done properly afterwards, but I guess that you will be using the size of the textbox for something?

You should be able to write your own TabControl that fixes this problem, but you will have to experiment to see what works here.

2024-07-14 00:51:05

不确定我是否理解这个问题。
但是,您可以使用文本框的调整大小事件来捕获宽度更改或表单的调整大小。

在您的示例中,执行步骤 3 时是否会触发 tabPage1 的 select 事件?

Not sure if I understand the problem.
But, you might use textbox's resize event to capture the width change OR form's resize.

In your example, does the select event of tabPage1 fire when you do step 3?

冷情 2024-07-14 00:51:05

首先,感谢完整的程序 - 它让我们更容易弄清楚发生了什么!

虽然文本框不可见,但其大小并未调整。 当您选择 tabPage1 时,在控件变得可见并且文本框再次布局之前将触发 Selected 事件。

现在,这就是它发生的原因 - 但你的真实情况是什么? 如果您确实想捕获控件大小的变化,请订阅它们的 Resize 事件。 如果没有,您能否详细解释一下您想要实现的目标?

Firstly, thanks for the complete program - it made it much easier to work out what was going on!

While the textbox isn't visible, it isn't resized. When you select tabPage1, the Selected event fires before the controls become visible and the textbox gets laid out again.

Now, that's why it's happening - but what's your real situation? If you actually want to capture the size of controls changing, subscribe to their Resize events. If not, could you explain more about what you're trying to achieve?

快乐很简单 2024-07-14 00:51:05

我非常确定正在发生的事情是在选项卡页变得可见之前稍微引发了 Selected 事件。 在选项卡页变得可见之前,文本框不会调整大小,因此您最终需要在实际调整大小之前检查文本框大小的值。 当您再次更改选项卡时,文本框已经调整大小,因此您可以获得正确的值。

将示例表单的最后几行更改为如下所示,它就会变得显而易见:

            this.textBox1.SizeChanged += TextboxSizeChanged;
        }

        //Tab Selected
        private void tabControl1_Selected(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("tab selected");
            this.Text = "TextBox Width: " + this.textBox1.Width.ToString();
        }

        private void TextboxSizeChanged(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Textbox resized");
        }

I'm pretty sure that what's happening is the Selected event is raised slightly before the tab page becomes visible. The text box is not resized until the tab page becomes visible, so you end up checking the value of the text box's size before it is actually resized. When you change tabs again, the text box is already resized, so you get the correct value.

Change the last few lines of your example form to look like this and it will become apparent:

            this.textBox1.SizeChanged += TextboxSizeChanged;
        }

        //Tab Selected
        private void tabControl1_Selected(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("tab selected");
            this.Text = "TextBox Width: " + this.textBox1.Width.ToString();
        }

        private void TextboxSizeChanged(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Textbox resized");
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文