Visual Studio Open&用按钮关闭另一个表单

时间:2018-03-28 16:16:42

标签: c# visual-studio

我想runbtn可以打开Form2的10个线程,比stopbtn可以关闭所有Form2,在stopbtn关闭所有Form2后我想要runbtn可以重新打开所有Form2。

namespace WindowsFormsApp1
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }
        Form2 formAction = new Form2();
        private void runbtn_Click(object sender, EventArgs e)
        {
            formAction.Show();  
        }

        private void stopbtn_Click(object sender, EventArgs e)
        {
            formAction.Close(); 
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这是我用runbtn打开Form2的10个线程,用stopbtn关闭所有Form2,然后我可以用runbtn再次重新打开所有Form2。感谢您的帮助。

namespace WindowsFormsApp1
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }        
        List<Form> formAction;
        private void runbtn_Click(object sender, EventArgs e)
        {            
            if (formAction == null)
            {
                formAction = new List<Form>() {
                new Form2(), new Form2(), new Form2(), new Form2(), new Form2(),
                new Form2(), new Form2(), new Form2(), new Form2(), new Form2()
                };
                foreach (var form in formAction)
                {
                    form.Show();
                }
            }
            else
            {
                MessageBox.Show("Form already open!");
            }                     
        }
        private void stopbtn_Click(object sender, EventArgs e)
        {
            if (formAction == null)
            {
                MessageBox.Show("It's already close!");
            }
            else
            {
                foreach (var form in formAction)
                {
                    form.Close();
                    formAction = null;
                }
            }            
        }        
    }
}