开发者

About form closing at runtime in C#

I have two forms named fr开发者_运维百科mRegistration & frmMain in my project in c#.

I have set frmRegistration as my start form.

User enters data in the frmRegistration form & presses submit button to get registered. Then, I want to close frmRegistration form & show frmMain form to the user.

I'm trying this by using Dispose() method of the frmRegistration. But, when I use this method, it disposes all my application execution because frmRegistration is the startup form.

I don't want this to happen. Can anyone solve this problem?

thanks.


Use Show() and Hide() methods.

    private void btnSubmit_Click(object sender, EventArgs e)
    {
      ...
      var frm = new frmMain();
      frm.Location = this.Location;
      frm.StartPosition = FormStartPosition.Manual;
      frm.Show();
      this.Hide();
    }  

UPDATE:
If you don't want to have frmRegistration in memory, start your program in main form and add this in your MainForm's Shown event:

    var frm = new frmRegistration();
    frm.Location = this.Location;
    frm.StartPosition = FormStartPosition.Manual;
    frm.FormClosing += delegate { this.Show(); };
    frm.Show();
    this.Hide();  

Now you can just close the registration form and automatically get back to main form.


Try setting frmMain as start up form and hiding it initialy, show frmRegistration, do what you have to do, and Dispose it.


You can also change you Program.cs main class with the Main() function to start frmRegistration and after positive DialogResult or another check it will then start with frmMain - as your main form and message loop.


At least there are two options: 1.Turn your Start up form into a singleton When you need to hide it, call it's hide method

2.Have new different startup form, call it MainApp form or whatever, have it set to invisible, the you can do what ever you like with the other non-startup forms.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜