【发布时间】:2015-04-19 06:42:18
【问题描述】:
![在此处输入图像描述][1]我想设计一个 c# windows 窗体,当用户单击按钮时,会打开一个新窗体并获取一些值。然后我以父形式使用该值。 但是当我启动程序并单击按钮时,Visual Studio 会打开一个空白的 win 表单,而我预计它会打开我之前设计的子表单。 那么是什么原因呢?我找不到任何解决方案。你的想法是什么? 以下是代码:
表格1
private void button1__Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
表格2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Date_Time
{
public partial class Form2 : Form
{
private Label label1;
private Label label2;
private Label label3;
private TextBox txtYear;
private TextBox txtMonth;
private Button btnOk;
private TextBox txtDay;
public void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.txtYear = new System.Windows.Forms.TextBox();
this.txtMonth = new System.Windows.Forms.TextBox();
this.txtDay = new System.Windows.Forms.TextBox();
this.btnOk = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(91, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Change in Years: ";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(13, 36);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(99, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Change in Months: ";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(13, 62);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(88, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Change in Days: ";
//
// txtYear
//
this.txtYear.Location = new System.Drawing.Point(109, 6);
this.txtYear.Name = "txtYear";
this.txtYear.Size = new System.Drawing.Size(100, 20);
this.txtYear.TabIndex = 3;
//
// txtMonth
//
this.txtMonth.Location = new System.Drawing.Point(109, 33);
this.txtMonth.Name = "txtMonth";
this.txtMonth.Size = new System.Drawing.Size(100, 20);
this.txtMonth.TabIndex = 4;
//
// txtDay
//
this.txtDay.Location = new System.Drawing.Point(109, 59);
this.txtDay.Name = "txtDay";
this.txtDay.Size = new System.Drawing.Size(100, 20);
this.txtDay.TabIndex = 5;
//
// btnOk
//
this.btnOk.ImageKey = "(none)";
this.btnOk.Location = new System.Drawing.Point(73, 85);
this.btnOk.Name = "btnOk";
this.btnOk.Size = new System.Drawing.Size(75, 23);
this.btnOk.TabIndex = 6;
this.btnOk.Tag = "";
this.btnOk.Text = "&Ok";
this.btnOk.UseVisualStyleBackColor = true;
//
// Options
//
this.ClientSize = new System.Drawing.Size(238, 120);
this.Controls.Add(this.btnOk);
this.Controls.Add(this.txtDay);
this.Controls.Add(this.txtMonth);
this.Controls.Add(this.txtYear);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Options";
this.Text = "Options";
this.ResumeLayout(false);
this.PerformLayout();
}
}
}
【问题讨论】:
-
表单是empty,因为根本没有控件?构造函数调用
InitializeComponents? -
这不是空的Yorye。我现在将完成孩子的表单代码。
-
空白意味着空,就我而言。设计表单和在表单中输入值(或通过代码操作)是有区别的
-
你的问题要么是你打开了一个 new 表单,要么你的构造函数没有像上面提到的那样调用
InitializeComponent。 -
How to return a value from a Form in C#? 的可能重复项 - 如果您想在每次单击按钮时打开同一个实例,则需要保存一个引用给它(而不是每次都使用
new)