在 C# 中控制另一个已打开的表单

发布于 2024-11-27 01:57:04 字数 597 浏览 0 评论 0原文

我正在将 Compact Framework 2.0 用于 Windows CE 5.0 中的应用程序。

我在不同形式之间更改控制时遇到问题。

我创建了一个简单的示例来展示这一点。

想象一下您有两个表单,第一个表单有一个按钮用于加载第二个表单。

当您单击按钮并加载第二个表单时,您可能想要一个控件以便返回到第一个表单,或者您可能只想在加载第二个表单后关闭第一个表单。

下面是一个关于在加载第二个表单后关闭第一个表单的示例:

public partial class Form1 : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
        Form2 secondForm = new Form2();
        secondForm.Show();            
    }
}

但不幸的是,这会关闭两个表单,而不仅仅是当前表单(form1)。

所以我认为这必须在第二个表单代码中进行,但我不知道如何从中访问第一个表单。

有什么建议吗?

I'm using Compact Framework 2.0 for an application in Windows CE 5.0.

I'm having trouble changing control between different forms.

I created a simple example in order to show this.

Imagine that you have two forms, the first form has a button in order to load the second form.

When you click the button and the second form is loaded you may wanna have a control in order to go back to the first form or may be you simply want to close the first form once the second one is loaded.

Here is an example about closing the first form once the second one is loaded:

public partial class Form1 : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
        Form2 secondForm = new Form2();
        secondForm.Show();            
    }
}

But unfortunately this closes both forms, not only the current one (form1).

So I think this must be made inside the second form code, but I don't know how to access the first form from it.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦言归人 2024-12-04 01:57:04

两个窗体都关闭的原因是 Form2 的父窗体是 Form1。 WinForms 的工作方式是,如果关闭父级 (this.Close()),则所有子级都会自动关闭。

执行此操作的典型方法实际上是使用 secondaryForm.ShowDialog()。这将使第一个表单保留在后台,但使其不可选择。如果您确实想摆脱当前表单,请将其隐藏:

this.Hide();
Form2 secondForm = new Form2();
secondForm.Show();

您可能需要一个方法,连接到 secondaryForm.Closed 事件来调用 this.Show() 以确保您的表单表格重新出现。

The reason both forms are closing is because Form2's parent is Form1. The way WinForms works, is that if the parent is closed (this.Close()) then any children will be closed automatically.

The typical way to do this would actually be to use secondForm.ShowDialog(). This would keep the first form in the background, but make it un-selectable. If you do want to get rid of the current form Hide it instead:

this.Hide();
Form2 secondForm = new Form2();
secondForm.Show();

You'll probably then want a method, hooked up to the secondForm.Closed event to call this.Show() to ensure that your form re-appears.

み格子的夏天 2024-12-04 01:57:04

Form1 是在 Program.cs 中创建并启动的 MainForm。
如果此表格已关闭。整个应用程序结束。
尝试像之前显示的 lan 一样隐藏 Form1 或使用控制器类来协调这两个表单。

Form1 is you MainForm which is created and started in the Program.cs.
If this Form is closed. The whole Application ends.
Try to Hide Form1 like lan showed before or use a Controller-Class to coordinate through the both Form´s.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文