Main() 中的代码可访问或不可访问取决于打开另一个表单

发布于 2024-07-12 21:54:16 字数 1007 浏览 9 评论 0原文

我有一些袖珍电脑应用程序,我遇到了一个严重的问题,如下所述:https://stackoverflow.com/questions/472598< /a> . 在寻找解决方案时,我在 Main(): 中尝试了一些实际上非常愚蠢的代码:

[MTAThread]
        static void Main()
        {
            Application.Run(new Tasks());
            Application.Exit();
        }

并在退出时设置断点。 如果我只是运行应用程序,然后关闭窗口,就会到达断点。 如果我运行应用程序,然后打开另一个窗口:

private void questButton_Click(object sender, EventArgs e)
        {
            QuestionnairesWindow questWindow = new QuestionnairesWindow();
            questWindow.Show();
            this.Hide();
}

然后从它返回到初始窗口:

private void backButton_Click(object sender, EventArgs e)
        {
            Tasks tasksWindow = new Tasks();
            tasksWindow.Show();
            this.Close();
        }

并以与第一次相同的方式关闭初始窗口,则永远不会到达 Apllication.exit() 代码,我的印象是该应用程序并未真正关闭(我无法再次打开它)。 抱歉,如果描述很复杂

编辑:问题是 - 有什么想法为什么它的行为不同吗?

I've some pocket pc app and i'm having a serious problem with it described here: https://stackoverflow.com/questions/472598 . While looking for a solution i tried some actually quite stupid code in Main():

[MTAThread]
        static void Main()
        {
            Application.Run(new Tasks());
            Application.Exit();
        }

and set breakpoint on exit. if i just run the application and then close the window the breakpoint is reached. if i run the application and then open another window:

private void questButton_Click(object sender, EventArgs e)
        {
            QuestionnairesWindow questWindow = new QuestionnairesWindow();
            questWindow.Show();
            this.Hide();
}

and then get back from it to initial window:

private void backButton_Click(object sender, EventArgs e)
        {
            Tasks tasksWindow = new Tasks();
            tasksWindow.Show();
            this.Close();
        }

and close the initial one the same way as the first time, the Apllication.exit() code is never reached and i have an impression that the application isn't really closed ( i can't open it again). sorry if the description is complicated

edit: the question is - any ideas why is it behaving differently?

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

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

发布评论

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

评论(3

花伊自在美 2024-07-19 21:54:16

Main() 中的 new Tasks()TaskstasksWindow = new Tasks(); 不是同一个对象,

您有 2 个 Tasks 对象,所以结束第二个,第一个仍然存在并且永远不会消失。 您需要将当前任务的引用传递给QuestionnairesWindow

您可以使用附加的 QuestionnairesWindow 构造函数来做到这一点:

private Tasks tasks;

public QuestionnairesWindow(Tasks t)
{
  this.tasks = t;
}

使用:

new QuestionnairesWindow(this).Show(); // where this = current `Tasks` created in `Main`

new Tasks() in Main() is not the same object with Tasks tasksWindow = new Tasks();

You got 2 objects of Tasks, so closing second, first is still present and never dies. You need to pass to QuestionnairesWindow the reference of current Tasks.

You can do that with additional QuestionnairesWindow constructor:

private Tasks tasks;

public QuestionnairesWindow(Tasks t)
{
  this.tasks = t;
}

using:

new QuestionnairesWindow(this).Show(); // where this = current `Tasks` created in `Main`
兮颜 2024-07-19 21:54:16

您遇到的问题是,在 backButton_Click 中您正在创建任务窗口的实例。 原来的任务窗口仍在运行,只是被隐藏了。 我怀疑您需要将对原始任务表单的引用传递到 QuestionnairesWindow 中,以便它可以选择再次显示它。

您需要在 QuestionnairesWindow 上添加公共/内部方法,在其中设置哪个任务表单导致 QuestionnairesWindow 打开。 在调用 this.Hide() 之前,您可以使用 this 引用在 questButton_Click 中调用此方法。 该引用将存储在 QuestionnairesWindow 内的私有变量中。

然后在 backButton_Click 中,使用这个私有变量来调用 .Show() 方法,以显示父窗体。

The issue you have is that in backButton_Click you are creating a new instance of the Tasks window. The original Tasks window is still running, it is just hidden. I suspect that you need to pass a reference to the original Tasks form into the QuestionnairesWindow so it can choose to show it again.

You either need to add a public/internal method on your QuestionnairesWindow where you set which Tasks form caused the QuestionnairesWindow to open. You call this method in questButton_Click with the this reference prior to calling this.Hide(). This reference will be stored in a private variable within the QuestionnairesWindow.

Then in the backButton_Click, you use this private variable, for calling the .Show() method, to show the parent form.

各自安好 2024-07-19 21:54:16

这一行似乎是问题所在:

Tasks tasksWindow = new Tasks();

如果我正确理解您的代码,那么在您的 backButton_Click 中,您正在创建任务表单的一个实例,而不是显示您最初隐藏的实例。 这意味着应用程序永远不会终止,因为仍然有一个窗口打开,但隐藏。

您可能想要做的是将 Tasks 表单引用传递给 QuestionnairesWindow 表单。

This line seems to be the problem:

Tasks tasksWindow = new Tasks();

If I understand your code correctly, in your backButton_Click, you're creating a new instance of your Tasks form, isntead of showing the one you originally hid. This means that the application never terminates, because there is still one window open, but hidden.

What you probably want to do is pass the Tasks form reference to the QuestionnairesWindow form.

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