Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
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
Posted

Look at each forms Controls list:
listboxB.Items.Clear();
foreach (Control c in myFormInstance.Controls)
   {
   listboxB.Items.Add(c.Name);
   }
 
Share this answer
 
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.
 
Share this answer
 
Comments
fjdiewornncalwe 24-Nov-10 11:54am    
Sorry. There is a bit of a typo in my answer. I don't smoke, but I must have been smoking something really good yesterday to come up with that one. I'll revise the answer and have it up shortly.
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 />
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900