简体   繁体   中英

how to open a form from another form in c#

i am writing a code for serial key registration. if the serial key entered by the user is correct then anothr form must open and the present form must close.

please go thought the code.

namespace ExtTrigger
{
    public partial class Activation : Form
    {
        public Activation()
        {
            InitializeComponent();
        }


    private void ActivateButton_Click(object sender, EventArgs e)
    {
        String key;
        key = string.Concat(textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text);
        if (key == "1234123412341234")
        {
            Properties.Settings.Default.Registered = true;
            MessageBox.Show("hurray", "", MessageBoxButtons.OK);

            Form1 f1= new Form1();
            f1.ShowDialog();
            this.Close();                
        }
        else
            MessageBox.Show("No Match", "", MessageBoxButtons.OK);
    }

    private void Activation_Load(object sender, EventArgs e)
    {

    }
}

my problem is: On clicking on ActivateBotton, Form1 opens but the present form doesnot close.

i have read in few threads that in VB we can change the property: ShutdownMode. How can we do that in c#?

f1.ShowDialog(); blocks the call, it doesn't go to the next line until that new form has been closed.

An option would be to use:

f1.Show();

Show doesn't block the call, it passes to the next statement. It will not wait for the new form to be closed.

since you have show the second form as f1.ShowDialog() so first one remain open untile second one close, try this

Form1 f1= new Form1();
f1.Show();
this.Close();  

The following code should do the trick:

using(Form1 f1 = new Form1())
{
    this.Hide();

    DialogResult result = f1.ShowDialog();
    if(result == DialogResult.OK)
    {
       this.Show();
    }
}

You create your new form within the using-block, then you hide your main form(or the form you are in at the moment) create a DialogResult that gets set by the newly opened form and open this form. Now you can set the results you want to check for inside of your new form and if everything went well inside of you new form you set the DialogResult to OK via:

this.DialogResult = DialogResult.OK;

Now back in our first form you check for the DialogResult and if it is okay you show your main form again. If it was not okay you could just reopen the 2nd form and let the user try again.

Opening a new form is very simple, but the way you do it really depends on your need.

Case 1: I would like to freeze/ block the calling form on secondary form call

In this case you should be using secondaryFormObj.ShowDialog();
Of course, when using this technique your called form, which now acts as a dialog, should "return" an answer to its caller parent on closure. For example:

private void SecondaryForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
     // Just a dummy code example.
     // Always returns Yes result on form closure
     this.DialogResult = System.Windows.Forms.DialogResult.Yes;
}

For more help and examples on this manner you can use MSDN: DialogResult - MSDN

Case 2: I would like to have both forms responding at the same time

In this case you basically need to call secondaryFormObj.Show();
If you want the caller form to be hidden on secondary form call, just invoke this.Hide();
after the call to secondaryFormObj.Show(); in the caller class.

You can also close the caller form using this.Close(); as long as the caller form is not the application's main form.

... And remember

Always make sure you initialized the secondary form object before invoking it with either secondaryFormObj.Show(); or secondaryFormObj.ShowDialog();

Initializing a form is done the same way like every typical object using the new operator.
For example: secondaryFormObj = new Form();

Hopes this helps. Happy coding!

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