Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

C#
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;



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

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

Like this:

C#
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.

C#
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); 
      }      

}


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:

C#
private void richTextBox1_Click(object sender, EventArgs e)
        {

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

        }
 
Share this answer
 
use this
C#
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();
        } 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900