By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,464 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,464 IT Pros & Developers. It's quick & easy.

create a variable to hold all forms created in a solution

P: n/a
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 <variabletypeListOfFoms;

thanks,

Feb 6 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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<Tclass:

public List<Formforms;

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<Tor 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.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)

"rGenius" <ru*********@gmail.comwrote in message
news:11*********************@k78g2000cwa.googlegro ups.com...
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 <variabletypeListOfFoms;

thanks,

Feb 6 '07 #2

P: n/a
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<TypeallForms = new List<Type>();
foreach (Type type in allForms.ToArray())
{
Form curForm =
Assembly.GetEntryAssembly().CreateInstance(type.Fu llName) as Form;
allFormInstance.Add(curForm);
}

thanks,

Feb 6 '07 #3

P: n/a
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<TypeformTypes = new List<Type>();
formTypes.Add(typeof(MyForm));
formTypes.Add(typeof(MyForm2));

// formTypes contains 2 Types: MyForm and MyForm2

List<Formforms = 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<Formforms = 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.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)

"rGenius" <ru*********@gmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
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<TypeallForms = new List<Type>();
foreach (Type type in allForms.ToArray())
{
Form curForm =
Assembly.GetEntryAssembly().CreateInstance(type.Fu llName) as Form;
allFormInstance.Add(curForm);
}

thanks,

Feb 6 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.