How to Add forms dynamically...

Hi...

I want to add Forms dynamically...i'm developing an application which gets names of COM ports present on a PC. So for each COM port i want one seperate form & i want to do this dynamically.

For getting COM present on my PC i am using this line...now in this i want to create seperate forms for each COM port with the names same as COM port names. In this i am getting all my comports names in the variable COMPORTNAME so with the same name i've to create a Form

foreach (string COMPORTNAME in SerialPort.GetPortNames())

{

//Here form should get created dynamically when i run my application

}

can anybody help me in this...

Thanks,

Vinay



Answer this question

How to Add forms dynamically...

  • jmsigler2

    I have yet to work with winforms in VS 2005, but apparently you are right. According to the documentation, this has been added in the 2.0 framework (VS 2005)

    The documentation can be found here: http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.openforms.aspx

    However according to the Kb article I mentioned before (http://support.microsoft.com/default.aspx scid=kb;EN-US;815707), there's is none

    Microsoft Visual C# .NET or Microsoft Visual C# 2005 does not provide a built-in collection for Form objects that are used in a project. This article describes how to build a custom collection class that essentially supports the functionality of a Forms collection.

    The article doest states it applies to VC# 2005 express, but I doubt that the express version doesn't provide access to certain namespaces & classes.

    Must be an error in KB article.

    Thanks for pointing it out, Markku



  • Dan Mikkelsen

    C# doesn't maintain a collection of forms in your application. You will have to implement something yourself to maintain references to all the instances of a form.

    The kb article here http://support.microsoft.com/default.aspx scid=kb;EN-US;815707 explains how to create a collection to maintain a reference to each form.



  • Marlun

    C# does maintain a collection of forms in your application. It's Application.OpenForms.

    Yours Markku


  • Jason Zhang

    Well, I have VC# 2005 express and it's work fine.

    Yours Markku


  • JasonY

    Thanks,

    Its worked a little bit & i modifed my code as per my application requirement but there is one more problem...

    foreach (string COMPORTNAME in SerialPort.GetPortNames())

    {

    Form1 frm = new Form1();

    frm.Text = COMPORTNAME;

    TextBox tb = new TextBox();

    frm..Controls.Add(tb);

    frm.Show();

    }

    But now problem is that how can change or get values from each textbox...Means if there are 3 ports then i'll get 3 forms & in each form i'll get 3 textboxes. So how can get each textbox contents.

    Thanks for your replay,

    Vinay


  • SquishyPBJMom

    Hi Vinay,

    You can create one form in designtime & at rutime you load an new instance of that form for each comport.

    To try is, just add a form to your solution (don't add any code). It will be called Form1 by default. Then, use the following code:

    foreach (string COMPORTNAME in SerialPort.GetPortNames()) {

    Form1 frm = new Form1();

    frm.Text = COMPORTNAME;

    frm.Show();

    }

    This will open one instance of form one for each comport and set the caption of the form to the name of the comport.

    Hope this helps,



  • How to Add forms dynamically...