当我尝试在 C# 中执行我的 Windows 窗体时,它是空白的?
Posted
技术标签:
【中文标题】当我尝试在 C# 中执行我的 Windows 窗体时,它是空白的?【英文标题】:When I try to execute my Windows Form in C#, It's blank? 【发布时间】:2021-06-11 23:06:10 【问题描述】:当我尝试执行我的 Windows 窗体应用程序时,我看到的只是一个完全白色的空白窗体。当我查看我的 form1.cs[Design] 时,一切看起来都很正常,但当我运行它时却不是?我看不到标签、按钮或任何东西。我以前从未遇到过这个问题,我认为我的 InitializeComponent 应该在哪里?这是我的 Form1.Designer.Cs,还是我的 form1.cs 有问题?
partial class Identity
/// <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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label6 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(278, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(116, 87);
this.label1.TabIndex = 0;
this.label1.Text = "VÄLKOMMEN!";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.Location = new System.Drawing.Point(67, 87);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(222, 23);
this.label2.TabIndex = 1;
this.label2.Text = "Skriv in ditt namn och personnummer nedan.:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(67, 181);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(51, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Förnamn:";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(63, 217);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(58, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Efternamn:";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(30, 269);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(161, 13);
this.label5.TabIndex = 4;
this.label5.Text = "Personummer:(ååååmmdd - xxxx)";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(127, 174);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 5;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(127, 210);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 6;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(197, 266);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(164, 20);
this.textBox3.TabIndex = 7;
//
// button1
//
this.button1.Location = new System.Drawing.Point(307, 329);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(87, 51);
this.button1.TabIndex = 8;
this.button1.Text = "Kör!";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(337, 395);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(35, 13);
this.label6.TabIndex = 9;
this.label6.Text = "label6";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(33, 423);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(492, 296);
this.richTextBox1.TabIndex = 10;
this.richTextBox1.Text = "\"\"";
//
// button2
//
this.button2.Location = new System.Drawing.Point(662, 650);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(116, 69);
this.button2.TabIndex = 11;
this.button2.Text = "Avsluta:";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Identity
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1207, 755);
this.Controls.Add(this.button2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label6);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Identity";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button2;
private EventHandler label1_Click;
我在这里调用我的 Form1.cs 中的 IntializeComponent,自从我开始这个新程序以来,我根本没有移动它。
public partial class Identity : Form
public Identity()
InitializeComponent();
public struct Person
public double idok;
public string fname;
public string lname;
public string id;
public string sex;
public string year;
public string fyear;
【问题讨论】:
你Form1.cs中的Form1是否在其构造函数中调用InitializeComponent? @Steve 我想是的,自从我开始这个新程序以来,我并没有真正移动它,我在谷歌上查看了这个问题并看到了解决方案,但我认为没有什么与众不同,我是初学者,所以也许我错过了一些东西。我可以以某种方式将我的构造函数粘贴到评论中吗?为了展示它的外观,不是 form1.cs 的全部,而只是那一部分,即开头。 在 Form.designer 文件中,在 Fields 声明中,private EventHandler label1_Click;
在做什么?您是否以未在此处显示的任何其他方式篡改此文件?
@Jimi 不,我没有做任何不寻常的事情,即使我删除它,它仍然是空白的。我以前从未遇到过这个问题。
我建议你开始一个新项目,创建一个名为 Identity 的新表单,复制这篇文章中除了最后 3 行之外的每一行代码,并将其粘贴到新 Identity 的每一行顶部.Designer.cs 除了最后 2 行(从而省略 private EventHandler label1_Click;
行,正如 Jimi 指出的那样,这是出乎意料的),然后复制您的结构和两个按钮单击处理程序(双击每个按钮,粘贴代码等) - 你' 可能会通过重新开始这个表格来更快地启动和运行,而不是反复敲打你的头是否有任何问题。
【参考方案1】:
根据评论线索,您创建了一个新解决方案,从有问题的表单的设计器代码中复制了一些选择行,注释掉了设计器代码中缺少的一些事件处理程序,然后能够加载设计器并添加缺少事件处理程序及其主体代码。生成的表单工作正常
【讨论】:
以上是关于当我尝试在 C# 中执行我的 Windows 窗体时,它是空白的?的主要内容,如果未能解决你的问题,请参考以下文章
如何从文本框(windows窗体)检查主键是不是存在于我的数据库中,c#
如何在 C# 中发布带有其数据库的 Windows 窗体应用程序
c# - 如何在 Windows 窗体中排列元素,如 Windows 资源管理器