简体   繁体   中英

How to open a C# windows form in another correctly by clicking a button?

![enter image description here][1]I want to design ac# windows form which when user clicks a button, a new form opens and gets some values. Then I use that values in parent form. But when I start the program and click the button, Visual Studio opens a blank win form, while I expected it opens the child form that I designed before. So what is the reason? I can't find any solutions. What is your ideas? Here are the codes:


Form1

private void button1__Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.Show();
        }

Form2

        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();

        }
    }
}

From a glance, the thing that stands out to me is your second form is defined as so:

public partial class Options : Form
{
    //code
}

But when you try to show it to the user, you are using a Form2 class instead of an Options class. Try changing your button1_Click to the following:

private void button1__Click(object sender, EventArgs e)
{
    Options opt = new Options();
    opt.Show();
}

You might also want to make sure that the constructor for the Options form is calling the InitializeComponent method:

public partial class Options : Form
{
    public Options()
    {
        InitializeComponent();
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        //Coding for your Options' Form ok button
    }
}
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
}

Sorry for taking your time friends. I found the reason finally by @user3189142's and Yorye's help. :) Thank you!

The child form(Options) was not complete. It was missing following code:


 public Options()
    {
        InitializeComponent();
    }

Thanks all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM