Hey,
Can any one help me with this problem.. i'ld really appreciate ne help..
wat i wanna do is.. dat i want to dynamically create objects on a way that they could be created in a loop..
so i thot an array of object could help
so i went ahead and wrote this code in a form load event..

Form1[] formarray = new Form1[4];
for (int i = 0; i < 4; i++)
    {
        formarray[i] = new Form1();
        formarray[i].Show(); 
    }

but at the line

formarray[i].Show();

I get this exception

Error creating window handle.

As u mite have guessed from the code.. wat i wanna do is to open 'n' no. of forms while loading.. and n will differ every time .. actually it will depend upon the no of records in some table and i want the no. of forms to be equal to the no. of records.

Hi there,

The problem you have is that in each of the forms you create, you recursively create again another array of forms, so you'll have infinitely many forms. Why don't you use it in some other eventhandler, so it won't create the array automatically as a form loads up. I hope you see what i'm trying to say :)

Best Regards,
Tamas

hmm.. tbut then wat should i do instead of this? ne suggestion?

hi,

Well depends on when and for what do you need it. You can create a button for instance, and when you click it, then create the forms. Or if you want to stick to the form's load event, you can declare a global static bool variable to track down whether your array of forms have been created or not. This way you'll only create your new forms once:

static bool created = false;
        private void Form1_Load(object sender, EventArgs e)
        {
            if (!created)
            {
                created = true;
                Form1[] formarray = new Form1[4];
                for (int i = 0; i < 4; i++)
                {
                    formarray[i] = new Form1();
                    formarray[i].Show();
                }
            }
        }

Hope this helps :)

no.. i wanna create "N" no. of forms.
where N = No. of records in a table
so i want a seperate form for every record in table..
getting me?

sure, then count the number of records in the table, and use the code above, that's it. You only need to modify two lines like this:

Form1[] formarray = new Form1[NoOfRecords];
for (int i = 0; i < NoOfRecords; i++)

but the problem is dat in this code

for (int i = 0; i < 4; i++)
                {
                    formarray[i] = new Form1();
                    formarray[i].Show();
                }

exception is generated at this line

formarray[i].Show();

what is the exception? I tried out the code I've posted before and it works.

yeah.. ur code is working..
can u please explain where was i going wrong?

ofcourse :)
So the load form event is raised when the form is loaded up, that simple. Your initial code said that when your first form is loaded make 4 more forms with the same behaviour. So 4 more forms were created and when each of the form was loaded it created again 4 more. So this process couldn't stop because it was creating more and more ( 1 Form -> 4 Forms -> 16 Forms and so on). The solution was to create a static variable that is independent of the Form object itself. Initially it's set to false. When the first form loads up it checks if it's true or not, if it's false then it directly sets the variable to true and creates the array of the new forms. When the new forms loads up it checks the static variable again, but that time it will be true, thus there will be no more form creation.

I hope you understand now, if it's still unclear feel free to ask, if this solves your problem and answers your question please mark the thread as solved.

yeah.. got it..
But theres one more thing i wanna ask..
Out of these 4 forms there is 1 form which on closing closes all other 3 too.. but dats not the case with other 3 forms..
so y is dat? nd how to stop it?

It's because the first form is the main form, parent of the next 4, if you close that it all close all it's child forms. I would manage the child forms from the main form. I would make a new Form for the records and make an array of that, and call those from the main form. What you are doing right now is making clones of the main form, I don't know why would you need that

Have u seen the sticky note Gadget of windows Vista?
I am learning to make winApps on Visual Studio now a days.. so i thouht making it would be fun and learning... dats y..
so now can u suggest what to do so that no such issue occurs..

well to be honest i don't know what to do. It's not an issue, that's the way it should work. You can create the array of forms and then hide the main form.

Yeah.. did that only.. Neways.. Thanks a lot for ur help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.