简体   繁体   中英

Switching between forms and sending variable correctly

I am wondering how can I correctly switch between forms by button click event.

I have Form1 and Form2. Form1 have: -TextBoxForm1 -ButtonForm1 Form2 have: -TextBoxForm2 -ButtonForm2

I would like to on_click ButtonForm1 event go to the Form2. Then I want to write some message to TextBoxForm2 and press ButtonForm2 it will go to the Form1 again and message from TextBoxForm2 will appear in TextBoxForm1.

Everything works fine, but I have one problem. When I close application and I wanna debug and start it again, some errors appear like:"application is already running".

Form1:

public static string MSG;
public Form1()
    {
        InitializeComponent();
        TextBoxForm1.Text = MSG;
    }
private void ButtonForm1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        this.Hide();
//There is probably my fault but when I was trying this.Close(); everything shutted down
        form2.Show();
    }

Form2:

private void ButtonForm2_Click(object sender, EventArgs e)
    {
        Form1.MSG = TextBoxForm2.Text;
        Form1 form= new Form1();
        form.Show();
        this.Close();
    }

How can I do this correctly please? :) I am beginner, thank you!

I would not go the route of using a STATIC for passing between forms as you mention you are a beginner, but lets get the closing to work for you.

In your main form create a new method to handle an event call as Hans mentioned in the comment. Then, once you create your second form, attach to its closing event to force form 1 to just become visible again.

// Inside your Form1's class..

void ReShowThisForm( object sender, CancelEventArgs e)
{
   // since this will be done AFTER the 2nd form's click event, we can pull it
   // into your form1's still active textbox control without recreating the form
   TextBoxForm1.Text = MSG;
   this.Show();
}

and where you are creating form2

private void ButtonForm1_Click(object sender, EventArgs e)
{
   Form2 form2 = new Form2();
   form2.Closing += ReShowThisForm;
   this.Hide();
   form2.Show();
}

and in your second form click, you should only need to set the static field and close the form

private void ButtonForm2_Click(object sender, EventArgs e)
{
   Form1.MSG = TextBoxForm2.Text;
   this.Close();
}

Easy solution is to use modal form. Display Form2 temporarily , when it is displayed Form1 is invisible.

var form2 = new Form2();
this.Visible = false; // or Hide();
form2.ShowDialog(this);
this.Visible = true;

And to pass data you can define property in Form2 , to example:

public string SomeData {get; set;}

Form1 has to set SomeData , which you take in Shown and display. Same property can be used to get data from Form2 (before closing).

// form 1 click
var form2 = new Form2() { SomeData = TextBoxForm1.Text; }
this.Visible = false;
form2.ShowDialog(this);
this.Visible = true;
TextBoxForm1.Text = form2.SomeData;

// form 2 shown
TextBoxForm2.Text = SomeData;

// form 2 click
SomeData = TextBoxForm2.Text;
Close();

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