Results 1 to 5 of 5

Thread: Creating forms Dynamically

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    6

    Creating forms Dynamically

    Hi
    Can anyone tell me if it is possible to create a form dynamically in .NET by passing a classname as a parameter

    for example, image the following code works for a second

    private Function NewForm(frmName as string) as Form
    NewForm = CreateObject(frmName)
    end Function

    I would invoke it like this:
    myForm = NewForm("Form2")

    Now this won't work since CreateObject only works on COM objects - so, is there something equivalent I could do to create this form in the function if I only know the className.

    Remember, I can't use the 'New' keyword as New expects you to supply an explicit classname (I only have the classname as a string parameter)

    any help would be greatly appreciated
    Cheers
    Mat

  2. #2
    Addicted Member
    Join Date
    Mar 2001
    Location
    Devon, UK
    Posts
    181
    Think your getting your knickers in a twist.
    You can dynamically create a new form by doing
    Dim MyForm as New Form
    MyForm.Show

    If you are passing a classname you wouldn't do it as a string. You could do the following.

    Public Class MyForm
    Inherits System.Windows.Forms.Form

    Public Sub New(ByVal sFormName As String)
    Me.Name = sFormName
    Me.Text = sFormName
    End Sub

    End Class


    Dim FormTest As New MyForm("Hello")
    FormTest.Show()
    Wind and waves resolves all problems.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    6
    Chris
    Don't think you understood my question. I know you can create a form by the method you described - thats basic stuff, but I need to take it a stage further. I have created a number of complex forms graphically and I want to display a particular form from within separate class.
    Say I have two forms Form1 and Form2 created graphically with a load of controls on them.

    Now I could write a class that contains a function thus

    Function CreateForm (strFormClassType as string) as Form
    if strFormClassType = "Form1" then
    CreateForm = new Form1
    elseif strFormClassType = "Form2" then
    CreateForm = new Form2
    endif
    End Function

    BUT, I don't want to have to hardcode all those forms in that CreateForm function (otherwise, everytime I add a new form i will need to add another if statement to this function)

    See what I am getting at? Is there some way to have the new form created based on the strFormClassType string passed into the function???

  4. #4
    Addicted Member
    Join Date
    Mar 2001
    Location
    Devon, UK
    Posts
    181
    Basically you need to create a custom forms collection (as forms does not exist in vb.net) The code can be found here
    http://www.dotnet247.com/247referenc...21/107856.aspx
    Wind and waves resolves all problems.

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    6
    Thanks Chris
    This was exactly what I was looking for. I was trying to implement my own forms colection which was why I needed to pass in a form class to retrieve from the collection.
    This will do the same hing for me
    Thanks again
    Mat

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width