SoFunction
Updated on 2025-03-07

C# AE's practical operation to return to the previous and next levels

I won't say much nonsense, let's just read the code~

try
      {
        //Judge whether you can return to the previous view        if (())
        {
          //Perform an operation          ();
          //refresh          ();
        }
      }
      catch (Exception ex)
      {
        throw;
      }
 try
      {
        if (())
        {
          ();
          ();
        }
      }
      catch (Exception ex)
      {
        throw;
      }

Supplement: C#——Click the button to return to the previous window method

1. Realize the hiding and display of the parent form

Assume Form1 is the parent form and Form2 is the new form

Form1's button button1 code:

private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);//Pass form1 to form2();//Hide yourself//();// Both display methods are OK();
}

Form2 code:

public partial class Form2 : Form
{
Form1 _form1;
public Form2(Form1 form1)
{
InitializeComponent();
_form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
//Close yourself and display Form1();
_form1.Show();
}
}

2. Multi-threaded form closed

Form1 is the welcome interface, close is the close button, and Form 2 is the main interface

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
th = new Thread(new ThreadStart(StartNewForm));
}
private Thread th;
private void StartNewForm()
{
From1 mf = new From2();
(mf);//Start the main interface}
private void close_Click(object sender, EventArgs e)
{
();//Close the form();//Start another thread}
}

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.