如何在新表单加载时关闭以前的表单 [英] how to close previous form when new form load

查看:85
本文介绍了如何在新表单加载时关闭以前的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式form1和form2。 form1包含一个richtextbox和6个文本框。和form2包含5个文本框和一个按钮。当我点击richtextbox然后打开form2,我填写了form2 6个文本框的所有值。当我点击form2的按钮然后所有条目显示在form1的richtextbox中。

但问题是现在按钮点击创建form1的对象,所以它再打开一次。

实际上我想点击richtextbox时关闭form1。并打开form2。当form2的单击按钮然后它显示form1。

i使用以下coading



i have two forms form1 and form2. form1 contain a richtextbox and 6 textboxes. and form2 contains 5textboxes and a button. when i click on richtextbox then form2 opens, and i filled all values of form2 6 textboxes. when i click button of form2 then all entries shows in richtextbox of form1.
but problem is that now button click creates object of form1 so its open one more time.
actually i want to close form1 when i click on richtextbox. and open form2. and when click button of form2 then its shows form1.
i am using following coading

private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
            Hide();
           f2.textBox1.Text = textstreet1.Text;
            f2.textBox2.Text = textstreet2.Text;
            f2.textBox3.Text = textcity.Text;
            f2.textBox4.Text = textstate.Text;
            f2.textBox5.Text = textcountry.Text;
            f2.textBox6.Text = textzipcode.Text;
             f2.richTextBox1.Text= textstreet1.Text + Environment.NewLine + textstreet2.Text + Environment.NewLine + textcity.Text + Environment.NewLine + textstate.Text + Environment.NewLine + textcountry.Text + Environment.NewLine + textzipcode.Text;










private void richTextBox1_Click(object sender, EventArgs e)
        {
            
            Form3 child = new Form3(textBox1);
            child.Show();
            
        }

推荐答案

首先,你可以简单地调用Close();在richTextBox1_Click事件中。



像这样:



Firstly, you could simply call Close(); in the richTextBox1_Click event.

Like this:

private void richTextBox1_Click(object sender, EventArgs e)
        {

            Form3 child = new Form3(textBox1);
            child.Show();
            Close();

        }





否则,这是我的首选方法,使用课程存储您的所有地址信息。





Else, and this is my preferred approach, use a class to store all your address information.

public class AddressInfo
{
      public string street1 {get;set;};
      public string street2 {get;set;};
      public string city {get;set;};
      public string state {get;set;};
      public string country {get;set;};
      public string zipcode {get;set;};

      public string CompleteAddress()
      {
          Return string.format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}",
                 street1, street2, city, state, country, zipcode); 
      }      

}





这个类在Form3上作为公共财产制作,是填充在你的button1_Click事件中。



然后你将richTextBox1_Click事件更改为:





This class is then made as a public property on Form3 and is populated in your button1_Click event.

Then you change the richTextBox1_Click event to be:

private void richTextBox1_Click(object sender, EventArgs e)
        {

            Form3 child = new Form3(textBox1);
            child.Showdialog();
            richTextBox1.Text = child.AddressInfo.CompleteAddress();

        }


使用此

use this
private void richTextBox1_Click(object sender, EventArgs e)
        {

           
           Form2 f2 = new Form2();
           f2.Show();
           this.Close();

        }

        private void button1_Click(object sender, EventArgs e)
        {
        Form1 f1= new Form1();
        f1.Show();
        this.Close();
        } 


这篇关于如何在新表单加载时关闭以前的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆