INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to Reference a Dynamically Created Form

How to Reference a Dynamically Created Form

(OP)
Hello, having a bit of a problem with referencing dynamically-created forms in my project.

I have a function that will generate a new form when executed. After said form is created, how then do I access it programmatically to add new controls?

For sanity's sake, let's say that I have created a new form named "MainScreen". In my code how can I then reference this form name to begin adding elements? There doesn't seem to be a Controls() style look-up for forms in the project. I have stumbled across various answers for needing System.Reflection and such, but have been unsuccessful in utilizing them.

My goal is to be able to dynamically create these forms and then later dynamically create or alter their content as needed through user interaction and options. Is there anything like:

MyProject.GetByName("MainScreen") or something blatantly obvious that I am overlooking?

Your comments are greatly appreciated.

RE: How to Reference a Dynamically Created Form

I don't have vb running at the moment, but I can think of two possible solutions.

If the created form is to have Global scope (i.e. throughout the program), create a Global variable in a Module. (Modules are Global by default). Create the variable as Public MyMainForm As Form.

Then wherever convenient in the program MyMainForm = New Form .

You can then add controls to it for example Dim Label1 As Label = New Label
With Label1
    .Left = 10
    .Length = 15
    .Text = "My First Label
etc.
End With

MyMainForm.Controls.Add(Label1)


*********************************************************
If you don't need global access, don't create the variable in a public module instead in the appropriate Procedure, just under the procedure declaration: Dim MyMainForm as Form and then when you are ready to create the form in the procedure use MyMainForm = New Form and then continue adding controls as in the above example.

************************************************************************

Once the form has been created, using either of the above methods, you can set its properties: MyMainForm.Text = "This is the Main Form" to set the form's title bar.

**********************************************************************************
If you use method one you can refer to MyMainForm throughout your program, otherwise you can only use it with the declaring procedure - although, just to confuse the issue, you could pass the form as a parameter from procedure to procedure or function to function - but I would not advise this as unless you are very experienced it could cause confusion. However feel free to experiment - I'm just suggesting some options.

Since I'm not currently running vb, just be aware that although I pretty certain that the syntax is correct, you may need to make some minor changes - but Intellisense will guide you there.


RE: How to Reference a Dynamically Created Form


Yeah, what softhemc said. A form is just another object. Create a variable of the appropriate type (Form) with the appropriate scope and go from there.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!

RE: How to Reference a Dynamically Created Form

(OP)
I just checked on this post to see what you guys have suggested, and I MUST thank you for the support. However, since my original post I did discover a work-around procedure.

In order to reference one of my dynamically-created form (this one named "MainWindow"), I used the following code:

CODE

Dim frm as Form
For Each F As Form In Application.OpenForms
   If F.Name = "MainWindow" Then
      frm = F
   End If
Next 

I am then able to begin processing items and adding to this form using the namespace <b>F</b>. I am sure this is convoluted and there is an easier, cleaner way, but I am still working on perfecting this art. :)

Thanks for all of the great help and ideas! I will try it out when I get a chance!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close