Windows窗体应用程序基础:在更改为全屏时调整控件的大小和重定位

时间:2014-08-06 18:35:30

标签: .net vb.net windows forms

这是一个非常简单的问题,所以希望我不要在这里解释。我想让我的表单应用程序有控件(按钮,DataViewGrids等),在将窗口更改为全屏时自动重定位。 (虽然我在下面发布了VB.net代码,但也欢迎任何C#答案。)

我尝试将我的控件固定到正确的位置,以及搞乱AutoSize的所有各种选项,但令人惊讶的是,这并没有奏效。

我通过创建一个主窗体,然后在该窗体上创建一个主面板,然后在UserControls中封装应用程序的不同部分的所有功能来设置我的应用程序。我在应用程序导航过程中根据需要动态显示/隐藏Panel上的UserControls。

例如,如果用户点击"将数据移动到Excel!"按钮,我们通过在Panel上隐藏先前显示的UserControl来导航,并在其中显示DataToExcel.vb。这是DataToExcel.vb:

Public Class DataToExcel
Inherits System.Windows.Forms.UserControl
Dim db As theDataContext = New theDataContext()

Private Sub ToExcel_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load
    ' Location on screen stuff:  This is probably what I need to change!
    MainForm.WindowState = FormWindowState.Maximized
    MainForm.MainPanel.AutoSize = True
    MainForm.MainPanel.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowOnly
End Sub

Private Sub BackButton_Click(sender As System.Object, e As System.EventArgs) Handles BackButton.Click
    Dim IntroductionForm = New Introduction
    MainForm.MainPanel.Controls.Clear()
    MainForm.MainPanel.Controls.Add(IntroductionForm)
    MainForm.Size = New System.Drawing.Size(837, 588)
    MainForm.WindowState = FormWindowState.Normal '<-- Go back to non-fullscreen.
    IntroductionForm.Show()
End Sub

' ...Other functions and subs related to the functionality of DataToExcel...

End Class

例如,我有一个&#34; Back&#34;按钮,它锚定在此UserControl的左下角。但是,在切换到全屏时,此按钮保持在以前的位置,并且不会将其位置更改为应用程序的左下角。这导致丑陋,非动态的外观。

1 个答案:

答案 0 :(得分:1)

锚定应该适用于此,但您必须记住控件在调整大小时要重定位到的位置。

考虑一个包含以下代码的表单:

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(12, 227);
            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;
            // 
            // button2
            // 
            this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button2.Location = new System.Drawing.Point(197, 227);
            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;
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;

当我左下角拖动时,button2保持相对于屏幕右下角的位置(这包括最大化表格)。但是,button1似乎不动。但如果我拖动左上角则反之亦然。

这不适合你吗?