从主窗体打开现有形式 [英] Open an existing form from the main form

查看:87
本文介绍了从主窗体打开现有形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了两种形式: Form1中窗体2 Form1中是主要形式。有一个在 Form1上的一个按钮,如果我按一下按钮,然后窗体2 将弹出。我想对窗体2 的东西。

I designed two forms: Form1 and Form2. Form1 is the main form. There is a button in Form1, if I click the button, then Form2 will pop out. I want to do something on Form2.

// click button in Form1.
private void button1_Click(object sender, EventArgs e)
    {
        Form form2= new Form();
        form2.ShowDialog();
    }



窗体2 是一种新的形式,而不是现有的形式。

But Form2 is a new form rather than an existing form.

这是错误的。

如何?谢谢你。

推荐答案

您正在创建Form类不是,你在你的项目有窗体2的实例。创建一个先前创建,然后调用的ShowDialog它Form2的实例。

You are creating instance of Form class not the Form2 which you have in your project. Create instance of Form2 which you created earlier and then call ShowDialog in it.

您可能会注意到在Program.cs中类似Application.Run(新Form1的() );在这里,我们创建Form1的实例,并通过运行方式。

You might have notice the in the program.cs something like Application.Run(new Form1()); Here we create the instance of Form1 and pass to Run method.

做到这一点通过创建窗体2的实例并调用的ShowDialog()方法来显示它

Do it this way by creating instance of Form2 and calling ShowDialog() method to show it

Form2 form2= new Form2();
form2.ShowDialog();

这篇关于从主窗体打开现有形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆