如何使用委托打开多个表单? [英] How to Open Multiple Forms Using delegates?

查看:62
本文介绍了如何使用委托打开多个表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用代表时遇到问题。我有多个窗口Forms.from首先来自

我放一个按钮。当我点击那个按钮时。所有表格必须一次打开。

HI I am having problem using with delegates. I have multiple windows Forms.from that first from
i place a one button. when i was click on that button .all the forms has to open at a time.

推荐答案

您是否在询问如何创建多个表单并显示它们按住主窗体上的按钮?为什么在Button的Click EventHandler中使用Delegate而不是仅使用简单的for循环或逐步代码?



动态创建新对象时,您可能需要跟踪它们,如果您在循环中创建它们,在方法中,您需要保留对它们的有效引用。



考虑你有两种类型的辅助形式,'Form2',Form3。此示例:
Are you asking how to create multiple Forms, and show them, when a Button is clicked on a Main Form ? Why use a Delegate instead of just using a simple for-loop, or step-by-step code, in the Button's Click EventHandler ?

When you create new objects dynamically, you probably need to keep track of them, and if you create them in a loop, in a method, you will need keep a valid reference to them.

Consider you have two types of "secondary" Forms, 'Form2, 'Form3. This example:
private List<Form2> theFormTwos = new List<Form2>();

private List<Form3> theFormThrees = new List<Form3>();

private void ButtonOnMainForm_Click(object sender, EventArgs e)
{
    // create two Form2's
    for (int i = 0; i < 2; i++)
    {
        theFormTwos.Add(new Form2());
        // show the Form now ?
    }

    // create three Form3's
    for (int j = 0; j < 3; j++)
    {
        theFormThrees.Add(new Form3());
        // show the Form now ?
    }
}

将每个创建的'Form2和'Form3实例存储在一个单独的列表中。



当然,一旦显示一个表单,它就是可以在Application.OpenForms FormCollection中访问,但是如果你处理或隐藏(将其'Visible属性设置为'false),那么它将不再出现在Application.OpenForms中。

is storing each created instance of 'Form2 and 'Form3 in a separate List.

Of course, once a Form is shown, then it is accessible in the Application.OpenForms FormCollection, but if you dispose of, or hide (set its 'Visible property to 'false), that Form, it will no longer appear in Application.OpenForms.


假设不需要以模态方式打开多个表单(也就是说,每个表单都会争取焦点),为什么你不能在按钮点击事件中创建每个表单的实例并调用每个show()方法?您没有提及在打开后需要访问每个表单的属性,因此它应该很简单:

Assuming that the multiple forms don't need to be opened modally (that is, each form would be fighting for focus), why wouldn't you just be able to create an instance of each form in your button click event and call the Show() method on each? You don't mention needing to access each form's properties after they're open so it should be simple:
protected void button1_Click(object sender, EventArgs e)
{
	new Form2().Show();
	new Form3().Show();
	new Form4().Show();
}


这篇关于如何使用委托打开多个表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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