英文:
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("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);
}
}
And here is my designercode from the view:
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;
}
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("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);
}
答案2
得分: 0
你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。以下是要翻译的内容:
To make things easier you should use TableLayoutPanel and Control.Dock.
英文:
To make things easier you should use TableLayoutPanel and Control.Dock.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论