按钮在运行时会像在设计时一样变大。

huangapple go评论57阅读模式
英文:

Button gets bigger at runtime as in designer

问题

我有一个CustomButton类,在设计师中看起来很好。当我编译我的项目并在运行时观察我的窗体时,按钮比在设计师中要大。

在设计师中它看起来像这样:

按钮在运行时会像在设计时一样变大。

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

按钮在运行时会像在设计时一样变大。

这是我的CustomButton类:(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);

    }
}

这是视图的设计代码:

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;
}

为什么会发生这种情况?

英文:

I have a CustomButton-class and in Designer it looks fine. When I compile my project and watch my form at runtime the buttons are bigger then in designer.

In Designer it looks like this:

按钮在运行时会像在设计时一样变大。

At Runtime it looks like this:

按钮在运行时会像在设计时一样变大。

Here my CustomButton-class: (TPButton.cs)

public class TPButton : Button {
public TPButton() {
InitializeComponent();
}
private void InitializeComponent() {
this.SuspendLayout();
// 
// TPButton
// 
this.Font = new System.Drawing.Font(&quot;Segoe UI&quot;, 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);
}
}

And here is my designercode from the view:

partial class EinAusschlussV {
/// &lt;summary&gt;
/// Required designer variable.
/// &lt;/summary&gt;
private System.ComponentModel.IContainer components = null;
/// &lt;summary&gt;
/// Clean up any resources being used.
/// &lt;/summary&gt;
/// &lt;param name=&quot;disposing&quot;&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;
protected override void Dispose(bool disposing) {
if (disposing &amp;&amp; (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// &lt;summary&gt;
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// &lt;/summary&gt;
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(&quot;Segoe UI&quot;, 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 = &quot;tpComboBox1&quot;;
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(&quot;Segoe UI&quot;, 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 = &quot;lblIln&quot;;
this.lblIln.Size = new System.Drawing.Size(27, 17);
this.lblIln.TabIndex = 1;
this.lblIln.Text = &quot;ILN&quot;;
// 
// tpButton1
// 
this.tpButton1.Font = new System.Drawing.Font(&quot;Segoe UI&quot;, 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.tpButton1.Location = new System.Drawing.Point(172, 10);
this.tpButton1.Name = &quot;tpButton1&quot;;
this.tpButton1.Size = new System.Drawing.Size(75, 25);
this.tpButton1.TabIndex = 2;
this.tpButton1.Text = &quot;tpButton1&quot;;
this.tpButton1.UseVisualStyleBackColor = true;
// 
// tpButton2
// 
this.tpButton2.Font = new System.Drawing.Font(&quot;Segoe UI&quot;, 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 = &quot;tpButton2&quot;;
this.tpButton2.Size = new System.Drawing.Size(75, 26);
this.tpButton2.TabIndex = 3;
this.tpButton2.Text = &quot;tpButton2&quot;;
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 = &quot;EinAusschlussV&quot;;
this.Text = &quot;EinAusschlussV&quot;;
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private TPComboBox tpComboBox1;
private TPLabel lblIln;
private TPButton tpButton1;
private TPButton tpButton2;
}

Why does this happen?

答案1

得分: 2

你在InitializeComponent中运行时更改了按钮大小,因此覆盖了设计器代码中指定的值。

尝试在不更改属性的情况下使用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);
}
英文:

You change the Button-Size in runtime in the InitializeComponent, thus overriding the values specified in the designercode.

Try the InitializeComponent without changeing the properties:

private void InitializeComponent() {
this.SuspendLayout();
// 
// TPButton
// 
this.Font = new System.Drawing.Font(&quot;Segoe UI&quot;, 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);
}

答案2

得分: 0

你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。以下是要翻译的内容:

To make things easier you should use TableLayoutPanel and Control.Dock.

英文:

To make things easier you should use TableLayoutPanel and Control.Dock.

huangapple
  • 本文由 发表于 2023年4月4日 17:11:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定