简体   繁体   English

如何从C#中的另一个表单打开表单

[英]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. 我的问题是:单击ActivateBotton时,将打开Form1,但不会关闭当前表单。

i have read in few threads that in VB we can change the property: ShutdownMode. 我读过一些线程,可以在VB中更改属性:ShutdownMode。 How can we do that in c#? 我们如何在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. Show不会阻止呼叫,它会传递到下一条语句。 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 由于您已将第二个窗体显示为f1.ShowDialog()因此第一个窗体保持打开状态直到第二个窗体关闭,请尝试执行此操作

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. 您可以在using-block中创建新表单,然后隐藏主表单(或当前所在的表单),并创建一个DialogResult,该对话框由新打开的表单设置并打开该表单。 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: 现在,您可以在新表单内部设置要检查的结果,如果新表单内部一切正常,则可以通过以下方式将DialogResult设置为OK:

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. 现在回到我们的第一个表单中,检查DialogResult,如果可以,则再次显示主表单。 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 情况1:我想冻结/阻止二次表单调用中的调用表单

In this case you should be using secondaryFormObj.ShowDialog(); 在这种情况下,您应该使用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 有关这种方式的更多帮助和示例,您可以使用MSDN: DialogResult-MSDN

Case 2: I would like to have both forms responding at the same time 情况2:我希望两种形式都同时回复

In this case you basically need to call secondaryFormObj.Show(); 在这种情况下,您基本上需要调用secondaryFormObj.Show();
If you want the caller form to be hidden on secondary form call, just invoke this.Hide(); 如果您希望在二次表单调用中隐藏调用者表单,只需调用this.Hide();
after the call to secondaryFormObj.Show(); 在调用secondaryFormObj.Show(); in the caller class. 在呼叫者类中。

You can also close the caller form using this.Close(); 您也可以使用this.Close();关闭呼叫者表单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(); 始终确保在使用secondaryFormObj.Show();调用辅助表单对象之前初始化了该辅助表单对象secondaryFormObj.Show(); or secondaryFormObj.ShowDialog(); secondaryFormObj.ShowDialog();

Initializing a form is done the same way like every typical object using the new operator. 初始化表单的方式与使用new运算符的每个典型对象相同。
For example: secondaryFormObj = new Form(); 例如: secondaryFormObj = new Form();

Hopes this helps. 希望这会有所帮助。 Happy coding! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM