winforms 按钮在运行时变大,就像在设计器中一样

bf1o4zei  于 2023-04-07  发布在  其他
关注(0)|答案(2)|浏览(183)

我有一个CustomButton-class,在Designer中看起来很好。当我编译我的项目并在运行时查看我的窗体时,按钮比在Designer中更大。
在Designer中,它看起来像这样:

在运行时,它看起来像这样:

下面是我的CustomButton-class:(TPButton.cs)

public class TPButton : Button {
    public TPButton() {
        InitializeComponent();
    }

    private void InitializeComponent() {
        this.SuspendLayout();
        // 
        // TPButton
        // 
        this.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
        this.Size = new System.Drawing.Size(75, 26);
        this.ResumeLayout(false);

    }
}

下面是我的designercode从视图:

partial class EinAusschlussV {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing) {
        if (disposing && (components != null)) {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #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.tpComboBox1 = new Infrastructure.CustomControls.TPComboBox();
        this.lblIln = new Infrastructure.CustomControls.TPLabel();
        this.tpButton1 = new Infrastructure.CustomControls.TPButton();
        this.tpButton2 = new Infrastructure.CustomControls.TPButton();
        this.SuspendLayout();
        // 
        // tpComboBox1
        // 
        this.tpComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        this.tpComboBox1.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.tpComboBox1.FormattingEnabled = true;
        this.tpComboBox1.Location = new System.Drawing.Point(45, 11);
        this.tpComboBox1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
        this.tpComboBox1.Name = "tpComboBox1";
        this.tpComboBox1.Size = new System.Drawing.Size(121, 25);
        this.tpComboBox1.TabIndex = 0;
        // 
        // lblIln
        // 
        this.lblIln.AutoSize = true;
        this.lblIln.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.lblIln.Location = new System.Drawing.Point(12, 14);
        this.lblIln.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
        this.lblIln.Name = "lblIln";
        this.lblIln.Size = new System.Drawing.Size(27, 17);
        this.lblIln.TabIndex = 1;
        this.lblIln.Text = "ILN";
        // 
        // tpButton1
        // 
        this.tpButton1.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.tpButton1.Location = new System.Drawing.Point(172, 10);
        this.tpButton1.Name = "tpButton1";
        this.tpButton1.Size = new System.Drawing.Size(75, 25);
        this.tpButton1.TabIndex = 2;
        this.tpButton1.Text = "tpButton1";
        this.tpButton1.UseVisualStyleBackColor = true;
        // 
        // tpButton2
        // 
        this.tpButton2.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.tpButton2.Location = new System.Drawing.Point(172, 241);
        this.tpButton2.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
        this.tpButton2.Name = "tpButton2";
        this.tpButton2.Size = new System.Drawing.Size(75, 26);
        this.tpButton2.TabIndex = 3;
        this.tpButton2.Text = "tpButton2";
        this.tpButton2.UseVisualStyleBackColor = true;
        // 
        // EinAusschlussV
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(660, 497);
        this.Controls.Add(this.tpButton2);
        this.Controls.Add(this.tpButton1);
        this.Controls.Add(this.lblIln);
        this.Controls.Add(this.tpComboBox1);
        this.Margin = new System.Windows.Forms.Padding(4);
        this.Name = "EinAusschlussV";
        this.Text = "EinAusschlussV";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private TPComboBox tpComboBox1;
    private TPLabel lblIln;
    private TPButton tpButton1;
    private TPButton tpButton2;
}

我不知道为什么会这样。
谁能帮帮我?
先谢谢你了。

bvpmtnay

bvpmtnay1#

您可以在运行时在InitializeComponent中更改Button-Size,从而覆盖设计器代码中指定的值。
在不更改属性的情况下尝试InitializeComponent:

private void InitializeComponent() {
    this.SuspendLayout();
    // 
    // TPButton
    // 
    this.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    //this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
    //this.Size = new System.Drawing.Size(75, 26);
    this.ResumeLayout(false);

}
ibps3vxo

ibps3vxo2#

为了使事情更容易,你应该使用TableLayoutPanelControl.Dock

相关问题