![在此处输入图像说明] [1]我想设计一个c#窗体,当用户点击按钮时,会打开一个新窗体并获取一些值。然后我以父表格的形式使用这些值。 但是当我启动程序并单击按钮时,Visual Studio会打开一个空白的win表单,而我希望它打开我之前设计的子表单。 那是什么原因?我找不到任何解决方案。你有什么想法? 以下是代码:
private void button1__Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
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();
}
}
}
答案 0 :(得分:2)
从一眼看出,对我来说突出的是你的第二种形式定义如下:
public partial class Options : Form
{
//code
}
但是当您尝试向用户显示时,您使用的是Form2
类,而不是Options
类。尝试将button1_Click更改为以下内容:
private void button1__Click(object sender, EventArgs e)
{
Options opt = new Options();
opt.Show();
}
您可能还想确保Options
表单的构造函数调用InitializeComponent
方法:
public partial class Options : Form
{
public Options()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
//Coding for your Options' Form ok button
}
}
答案 1 :(得分:0)
private void button1__Click(object sender, EventArgs e)
{
Form2 newForm = new Form2();
newForm.ShowDialog();
// here you can take your parameters
int x = newForm.x;
int y = newForm.y;
}
// you should have definitions for x and y in your child form
public partial class Form2: Form
{
public int x{get; set;}
public int y{get; set;}
public Form2(){}
// do stuff
}
答案 2 :(得分:0)
抱歉花时间的朋友。我最终通过@ user3189142和Yorye的帮助找到了原因。 :)谢谢!
子表单(选项)未完成。它缺少以下代码:
public Options()
{
InitializeComponent();
}
谢谢大家。