如果我关闭其中一个表单,它们都会关闭 [英] If I close one of my forms, they all close

查看:20
本文介绍了如果我关闭其中一个表单,它们都会关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是赢得表单 (C#) 的新手.我正在制作的当前表单可以选择创建一个新的(空白)实例,但如果我关闭第一个实例,所有其他实例也会关闭.这不是我想要发生的事情.(尽管关闭从第一个打开的任何表单并不会关闭任何其他表单)

I am new to win forms (C#). The current form that I'm making has the option to create a new (blank) instance of itself, but if I ever close that first instance, all the others close as well. This is not what I want to happen. (Closing any form opened up from the first doesn't close any others, though)

我在想这可能是因为我正在从其中一个副本/对象中创建一个新副本,因此它与第一个对象相关联,因此它在创建时关闭;但是,如果我从第一个打开的表单中打开另一个表单,然后关闭从第一个打开的那个表单,我从中打开的那个就不会关闭.

I was thinking it may be because I am creating a new copy from within one of the copies/objects, so it is tied to that first object so it closes when it does; however, if I open up another form from the one opened from the first one and then close that one that was opened from the first one, the one that I opened up from it doesn't close.

我想要它以便我仍然可以关闭第一个表单而不关闭其他表单,并且当最后一个关闭时,程序停止运行.

I want it so that I can still close that first form without the others closing, and that when the last one closes, the program stops running.

有没有办法做到这一点?

Is there any way to do this?

推荐答案

您可以根据需要运行任意数量的表单,但每个表单都在单独的线程中

You can run as many forms as you need but each in the separate thread

using System;
using System.Threading;
using System.Windows.Forms;

public partial class MyForm: Form
{
    public MyForm()
    {
        InitializeComponent();
    }

    private void Button1Click(object sender, EventArgs e)
    {
        var t = new Thread(() => Application.Run(new MyForm()));
        t.Start();
    }
}

这篇关于如果我关闭其中一个表单,它们都会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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