create a variable to hold all forms created in a solution

R

rGenius

hi all,

im planning to create a lookup usercontrol where i need to specify a
custom property (a form) to be loaded up once a button is pressed.

usercontrol contains:
textbox
button

now how can i create a variable in my usercontrol to list all forms in
my project.

something like:

public <variabletype> ListOfFoms;

thanks,
 
D

Dave Sexton

If you're just wondering how to get all of the opened Forms you can do that
in the 2.0 Framework with the Application.OpenForms property, which returns
an instance of the FormCollection class (a read-only collection).

If you need to be able to add to a local field an arbitrary number of Forms
created by your UserControl, then you'll probably want a collection that
expands on its own when new Forms are added.

In the 2.0 Framework you can use the generic List<T> class:

public List<Form> forms;

In earlier versions of the Framework, use the ArrayList class instead:

public ArrayList forms;

If you only need a fixed number of Forms then you could use an array of
forms:

public Form[] forms = new Form[10];

although a List<T> or ArrayList would probably be just fine. If you use an
array you'll have to assign the Forms by index, but if you use one of the
list types you'll be able to add them with the Add method.
 
R

rGenius

thanks Dave for a speedy reply,

its what i want, the only thing is how to populate the list with the
list of forms.

i've been trying these to get it done.

List<Type> allForms = new List<Type>();
foreach (Type type in allForms.ToArray())
{
Form curForm =
Assembly.GetEntryAssembly().CreateInstance(type.FullName) as Form;
allFormInstance.Add(curForm);
}

thanks,
 
D

Dave Sexton

Hi,

The allForms list is empty, so ToArray will return an empty array. Also,
where and how is allFormInstance defined?

If allForms were to be populated with the Types first, then your loop would
work to iterate over the array correctly. If allFormInstance is something
like, List<Form>, then your code should successfully add instances of each
Form Type in the allForms list to the allFormInstance list as long as each
Form is constructed successfully.

The code could be more succinct:

List<Type> formTypes = new List<Type>();
formTypes.Add(typeof(MyForm));
formTypes.Add(typeof(MyForm2));

// formTypes contains 2 Types: MyForm and MyForm2

List<Form> forms = new List<Form>();

foreach (Type type in formTypes)
{
forms.Add(Activator.CreateInstance(type));
}

// forms contains two instances: one of MyForm and one of MyForm2

Just in case, note that the code above isn't useful at all since I
hard-coded typeof(). It could easily have replaced it with a call to the
constructor of each of the two forms:

List<Form> forms = new List<Form>();
forms.Add(new MyForm());
forms.Add(new MyForm2());

If you don't know the Types of Forms at design-time then you'll have to use
reflection, as in the previous example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top