How to open a new form from another form

后端 未结 11 1547
太阳男子
太阳男子 2020-12-03 00:29

I have form which is opened using ShowDialog Method. In this form i have a Button called More. If we click on More it should open another form and it should close the curren

相关标签:
11条回答
  • 2020-12-03 01:20

    ok so I used this:

    public partial class Form1 : Form
    {
        private void Button_Click(object sender, EventArgs e)
        {
            Form2 myForm = new Form2();
            this.Hide();
            myForm.ShowDialog();
            this.Close();
        }
    }
    

    This seems to be working fine but the first form is just hidden and it can still generate events. the "this.Close()" is needed to close the first form but if you still want your form to run (and not act like a launcher) you MUST replace it with

    this.Show();
    

    Best of luck!

    0 讨论(0)
  • 2020-12-03 01:23

    You could try adding a bool so the algorithm would know when the button was activated. When it's clicked, the bool checks true, the new form shows and the last gets closed.

    It's important to know that forms consume some ram (at least a little bit), so it's a good idea to close those you're not gonna use, instead of just hiding it. Makes the difference in big projects.

    0 讨论(0)
  • 2020-12-03 01:24

    You need to control the opening of sub forms from a main form.

    In my case I'm opening a Login window first before I launch my form1. I control everything from Program.cs. Set up a validation flag in Program.cs. Open Login window from Program.cs. Control then goes to login window. Then if the validation is good, set the validation flag to true from the login window. Now you can safely close the login window. Control returns to Program.cs. If the validation flag is true, open form1. If the validation flag is false, your application will close.

    In Program.cs:

       static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            /// 
    
            //Validation flag
            public static bool ValidLogin = false;
    
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
    
                Application.Run(new Login());
    
                if (ValidLogin)
                {
                    Application.Run(new Form1());
                }
            }
    
        }
    

    In Login.cs:

           private void btnOK_Click(object sender, EventArgs e)
            {
                if (txtUsername.Text == "x" && txtPassword.Text == "x")
                {
                    Program.ValidLogin = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Username or Password are incorrect.");
                }
            }
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
    0 讨论(0)
  • 2020-12-03 01:25

    In my opinion the main form should be responsible for opening both child form. Here is some pseudo that explains what I would do:

    // MainForm
    private ChildForm childForm;
    private MoreForm moreForm;
    
    ButtonThatOpenTheFirstChildForm_Click()
    {
        childForm = CreateTheChildForm();
        childForm.MoreClick += More_Click;
        childForm.Show();
    }
    
    More_Click()
    {
        childForm.Close();
        moreForm = new MoreForm();
        moreForm.Show();
    }
    

    You will just need to create a simple event MoreClick in the first child. The main benefit of this approach is that you can replicate it as needed and you can very easily model some sort of basic workflow.

    0 讨论(0)
  • 2020-12-03 01:25

    Try this..

    //button1 will be clicked to open a new form
    private void button1_Click(object sender, EventArgs e)
    {
        this.Visible = false;     // this = is the current form
        SignUp s = new SignUp();  //SignUp is the name of  my other form
        s.Visible = true;
    }
    
    0 讨论(0)
提交回复
热议问题