表单收集/创建新实例 [英] Forms Collection/ Create New Instance

查看:62
本文介绍了表单收集/创建新实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个主表格.我还有另外6个不是MDI子级的子代.

在我的表单上,我有2个列表框A和B.在我的列表框A中,我想列出我的项目或应用程序中的所有表单,以便当我单击列表框A中的表单名称之一时,该控件的所有控件表单必须出现在列表框B中.

有人可以帮我使用C#进行这项工作吗?

Marc

I have a main form in my project. I have 6 other ones that are not MDI child.

On my form I have 2 listbox A and B. In my listbox A, I would like to list all the forms in my project or application, so that when I click on one of the form name in listbox A, all the controls of this form must appear in the listbox B.

Can somebody help me make this work with C#, please?

Marc

推荐答案

查看每种表单的控件列表:
Look at each forms Controls list:
listboxB.Items.Clear();
foreach (Control c in myFormInstance.Controls)
   {
   listboxB.Items.Add(c.Name);
   }


两个答案我都有一个错误.

#1:

foreach(myFormInstance.Controls中的控件c)
{listboxB.Items.Add(c.Name); }

对于此答案,我有错误:
名称myFormInstance在当前上下文中不存在.

#2:

表单newForm = new Form();
表格refNewForm = ref(newForm);

对于第二个错误,我有错误:
无效的表达方式ref

我应该怎么办 ?我正在使用Visual Studio 2005.
for both answer i have an error.

#1 :

foreach (Control c in myFormInstance.Controls)
{ listboxB.Items.Add(c.Name); }

For this answer i have the error :
The name myFormInstance does not exist in the current context.

#2 :

Form newForm = new Form();
Form refNewForm = ref(newForm);

For this second one i have the error :
Invalid expression term ref

What should i do ? I am using Visual Studio 2005.


您需要将对所有子窗体的引用保留在主窗体中(也许在创建时),然后您就可以访问您想要的表格.
You''ll need to keep references to all the child forms in the main form (when you create them, maybe) and then you will have access to the forms when you want it.
<br />
// Ensure that this newForm has class-wide scope if you want to access it from a different function than where it is created.<br />
Form newForm = new Form();<br />
<br />
... Open the form, etc...<br />
<br />
... Access the newForm control list ...<br />
foreach (Control c in newForm.Controls)<br />
{<br />
    listboxB.Items.Add(c.Name);<br />
}<br />
<br />
<br />
<br />


这篇关于表单收集/创建新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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