如何在显示子表单时隐藏控件的移动 [英] how to hide controls' moving when a child form is shown

查看:58
本文介绍了如何在显示子表单时隐藏控件的移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是一个包含FormParent和FormChild的MDI项目。

it is a MDI project containing FormParent and FormChild.

FormParent的密钥代码是加载FormChild,见下文:

  ;  公共部分类FormParent:表格

    {

        public FormParent()

        {

            InitializeComponent();

The key code for FormParent is to load the FormChild, see below:
    public partial class FormParent : Form
    {
        public FormParent()
        {
            InitializeComponent();

            MenuStrip mstrip = new MenuStrip();

            mstrip.Items.Add(" MenuItem1");

            mstrip.Items.Add(" MenuItem2");

            mstrip.Items.Add(" MenuItem3");

            this.Controls.Add(mstrip);

            this.MainMenuStrip = mstrip;

            MenuStrip mstrip = new MenuStrip();
            mstrip.Items.Add("MenuItem1");
            mstrip.Items.Add("MenuItem2");
            mstrip.Items.Add("MenuItem3");
            this.Controls.Add(mstrip);
            this.MainMenuStrip = mstrip;

            this.WindowState = FormWindowState.Maximized;

            this.IsMdiContainer = true;

        }

            this.WindowState = FormWindowState.Maximized;
            this.IsMdiContainer = true;
        }

        private void FormParent_Load(object sender,EventArgs e)

        {

            FormChild child = new FormChild();

            child.ControlBox = false;

            child.WindowState = FormWindowState.Maximized;

            child.MdiParent = this;

            child.Show();                   


        }
    }

        private void FormParent_Load(object sender, EventArgs e)
        {
            FormChild child = new FormChild();
            child.ControlBox = false;
            child.WindowState = FormWindowState.Maximized;
            child.MdiParent = this;
            child.Show();                   
        }
    }

我发现用户体验不好,因为FormChild中的控件在显示时会移动,为了演示它,只有thress控件被添加到FormChild:tableLayoutPannel,以及两个纽扣。以下代码显示,当
显示表单时,button2正在移动:

   公共部分类FormChild:表格

    {

        public FormChild()

        {

            InitializeComponent();

        }

I found that the user expeirence is bad since the controls in the FormChild are moving when it is shown, to demo it, only thress controls are added to the FormChild: tableLayoutPannel, and two buttons. The following code shows that button2 is moving when the form is shown:
    public partial class FormChild : Form
    {
        public FormChild()
        {
            InitializeComponent();
        }

        private void button1_LocationChanged(object sender,EventArgs e)

        {

            MessageBox.Show(" aa");

        }
    }


        private void InitializeComponent()

        {

            this.tableLayoutPanel1 =新System.Windows.Forms.TableLayoutPanel();

            this.button1 = new System.Windows.Forms.Button();

            this.button2 = new System.Windows.Forms.Button();

            this.tableLayoutPanel1.SuspendLayout();

            this.SuspendLayout();

            //

            // tableLayoutPanel1

            //

            this.tableLayoutPanel1.ColumnCount = 1;

            this.tableLayoutPanel1.ColumnStyles.Add(新System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent,50F));

     &NBSP ;      this.tableLayoutPanel1.Controls.Add(this.button1,0,0);

            this.tableLayoutPanel1.Controls.Add(this.button2,0,1);

            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;

            this.tableLayoutPanel1.Location =新System.Drawing.Point(0,0);

            this.tableLayoutPanel1.Name =" tableLayoutPanel1";

            this.tableLayoutPanel1.RowCount = 2;

            this.tableLayoutPanel1.RowStyles.Add(新System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent,50F));

     &NBSP ;      this.tableLayoutPanel1.RowStyles.Add(新System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent,50F));

     &NBSP ;      this.tableLayoutPanel1.Size = new System.Drawing.Size(550,342);

            this.tableLayoutPanel1.TabIndex = 0;

            //

            // button1

            //

            this.button1.Anchor = System.Windows.Forms.AnchorStyles.None;

            this.button1.Location = new System.Drawing.Point(237,74);

            this.button1.Name =" button1";

            this.button1.Size = new System.Drawing.Size(75,23);

            this.button1.TabIndex = 0;

            this.button1.Text =" button1";

            this.button1.UseVisualStyleBackColor = true;

            this.button1.LocationChanged + =新System.EventHandler(this.button1_LocationChanged);

            //

            // button2

            //

            this.button2.Anchor = System.Windows.Forms.AnchorStyles.None;

            this.button2.Location = new System.Drawing.Point(237,245);

            this.button2.Name =" button2";

            this.button2.Size = new System.Drawing.Size(75,23);

            this.button2.TabIndex = 1;

            this.button2.Text =" button2";

            this.button2.UseVisualStyleBackColor = true;

            //

            // FormChild

            //

            this.AutoScaleDimensions =新System.Drawing.SizeF(6F,13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(550,342);

            this.Controls.Add(this.tableLayoutPanel1);

            this.Name =" FormChild";

            this.Text =" FormChild";&
            this.tableLayoutPanel1.ResumeLayout(false);

            this.ResumeLayout(false);

        private void button1_LocationChanged(object sender, EventArgs e)
        {
            MessageBox.Show("aa");
        }
    }

        private void InitializeComponent()
        {
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.tableLayoutPanel1.SuspendLayout();
            this.SuspendLayout();
            //
            // tableLayoutPanel1
            //
            this.tableLayoutPanel1.ColumnCount = 1;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Controls.Add(this.button1, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.button2, 0, 1);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 2;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(550, 342);
            this.tableLayoutPanel1.TabIndex = 0;
            //
            // button1
            //
            this.button1.Anchor = System.Windows.Forms.AnchorStyles.None;
            this.button1.Location = new System.Drawing.Point(237, 74);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.LocationChanged += new System.EventHandler(this.button1_LocationChanged);
            //
            // button2
            //
            this.button2.Anchor = System.Windows.Forms.AnchorStyles.None;
            this.button2.Location = new System.Drawing.Point(237, 245);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            //
            // FormChild
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(550, 342);
            this.Controls.Add(this.tableLayoutPanel1);
            this.Name = "FormChild";
            this.Text = "FormChild";
            this.tableLayoutPanel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }


$
我们可以看到按钮2的位置在调用MessageBox之前和之后发生了变化。为了提供更好的用户体验,我们只需要显示所有控件的最终结果,而不是让最终用户在表单为
show时看到移动控件,谢谢。

        }


We can see the location of button2 changed before and after the MessageBox is called. To provide a better user experience, we need to only show the final result of all the controls, not making end users to see the moving controls when the form is show, thanks.

 

推荐答案

由于您的按钮包含在TableLayoutPanel中,因此表单调整大小会改变面板大小,ant面板中的子控件将在面板调整大小时移动位置。这是面板的默认行为。

Since your buttons are contained in the TableLayoutPanel, and the form resize will change the panel size, ant child controls in the panel will move location while the panel is resizing. This is a default behaviour of the panel.

如果将绝对位置设置为按钮,没有TableLayoutPanel,并且未设置Dock或Athe nchor属性,则按钮将不会移入调整大小事件。

If you set the absolute position to the buttons, without the TableLayoutPanel, and without setting the Dock or Athe nchor properties, the button will not move in the resizing event.

为什么你可以找到Button2移动动作。由于您只是阻止Button1 LocationChanged事件处理程序中的UI线程,并且UI将在button1更改其位置时停止。此时,Button2不会开始更改。因此,如果你想在Button1移动事件中显示
,请不要阻止UI线程,这样你就可以尝试在一秒线程中显示你的消息,或者通过开始调用来显示你的消息。

Why you can find the Button2 moving action. Since you just block the UI thread in the Button1 LocationChanged event handler, and the UI will stop while the button1 changes its location. At this time, the Button2 does not start change. So if you want to show something in the Button1 moving event, please do not block the UI thread, so you could try to show your message in one second thread or show your message by begin invoke.

        private void button1_LocationChanged(object sender, EventArgs e)
        {
            this.BeginInvoke(new Action(() => { MessageBox.Show("aa"); }));            
        }

此致,


这篇关于如何在显示子表单时隐藏控件的移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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