c# - 形状比应有的要大

标签 c# .net forms visual-studio

我只是想创建一个带有“X”按钮的非常小的表单。

当我执行整个事情时,它比预期的要大: 在 Visual Studio 设计编辑器上:

enter image description here

但是运行时,会出现这样的情况:

enter image description here

设计器代码:

        private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.BackColor = System.Drawing.Color.Black;
        this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.button1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.button1.ForeColor = System.Drawing.Color.Gray;
        this.button1.Location = new System.Drawing.Point(6, 7);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(27, 26);
        this.button1.TabIndex = 0;
        this.button1.Text = "X";
        this.button1.UseVisualStyleBackColor = false;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // Form3
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
        this.ClientSize = new System.Drawing.Size(40, 40);
        this.Controls.Add(this.button1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "Form3";
        this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
        this.ResumeLayout(false);

    }

我做错了什么?

最佳答案

您可以看到下面的设计器代码,因为您缺少设置按钮和表单的一些属性。

private void InitializeComponent()
{
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.BackColor = System.Drawing.Color.Black;
    this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    this.button1.Font = new System.Drawing.Font("Arial", 12F, 
    System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 
    ((byte)(0)));
    this.button1.ForeColor = System.Drawing.Color.Gray;
    this.button1.Location = new System.Drawing.Point(0, 0);
    this.button1.Name = "button1";
    this.button1.AutoSize = true;  //you were missing this for the button
    this.button1.Size = new System.Drawing.Size(27, 26);
    this.button1.TabIndex = 0;
    this.button1.Text = "X";
    this.button1.UseVisualStyleBackColor = false;
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.AutoSize = true;  //you were missing this for the form
    this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;//also this 
    this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
    this.ClientSize = new System.Drawing.Size(40, 40);
    this.Controls.Add(this.button1);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
    this.Name = "Form1";
    this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
    this.ResumeLayout(false);
}

请检查上面的设计器代码,它可以工作。

关于c# - 形状比应有的要大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53648359/

相关文章:

c# - 如何将鼠标悬停在一个分区上的鼠标指针更改为手?

c# - 查找未使用的代码

c# - Xamarin.Forms 绑定(bind)不起作用

c# - 为什么我的断点在 Visual Studio 中重复?

c# - 为什么 .NET 中的排序集合没有匹配的接口(interface)?

javascript - 如何清除 dijit 表单中的验证消息

c# - 对 System.data.entity.design.dll 的引用不起作用

C# 在循环中为字符串赋值的最佳方式

forms - 在 Symfony 2 表单中自定义数据原型(prototype)属性

python - 覆盖 Django 中表单模板的样式