【问题标题】:How to open a C# windows form in another correctly by clicking a button?如何通过单击按钮正确打开另一个 C# windows 窗体?
【发布时间】: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

标签: c# winforms


【解决方案1】:

抱歉耽搁了朋友们的时间。在@user3189142 和Yorye 的帮助下,我终于找到了原因。 :) 谢谢!

子表单(选项)不完整。它缺少以下代码:


 public Options()
    {
        InitializeComponent();
    }

谢谢大家。

【讨论】:

    【解决方案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
        }
    }
    

    【讨论】:

    • @MojtabaShoja 表单的构造函数是否调用了InitializeComponent 方法?
    • 我试过@user3189142。没有反应。实际上我在这里复制代码时出错了。最初两者都称为选项。对不起:))
    • 哪个表单@user3189142?表格1?
    【解决方案3】:
    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
    }
    

    【讨论】:

    • 这根本没有回答问题-“但是当我启动程序并单击按钮时,Visual Studio 会打开一个空白的 win 表单,而我希望它会打开我之前设计的子表单.那是什么原因呢?”
    • @Sayse OP 说表单实际上并不是空白的,因为没有控件。这也是我理解的回答他的需求。在 OP 使问题更清楚之前,不能真的说这是一个无效的答案。
    • @YoryeNathan - 唯一似乎很清楚的是操作在打开表单时遇到问题,而不是返回值,这个答案解释了如何做。我同意这个问题不清楚
    • 请看这张照片。希望你能理解我的问题。 :) oi61.tinypic.com/9hjdjk.jpg
    • @MojtabaShoja 所以它没有控件,你为什么不这么说?您要么没有在构造函数中调用 InitializeComponents,要么错误地实例化了错误的(不同的、空的)表单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    相关资源
    最近更新 更多