Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Reference a Dynamically Created Form 1

Status
Not open for further replies.

PixelGod

Programmer
Aug 24, 2015
16
US
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.
 
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 [tt]Public MyMainForm As Form[/tt].

Then wherever convenient in the program [tt] MyMainForm = New Form [/tt].

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

MyMainForm.Controls.Add(Label1)[/tt]

*********************************************************
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: [tt]Dim MyMainForm as Form[/tt] and then when you are ready to create the form in the procedure use [tt]MyMainForm = New Form[/tt] 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: [tt]MyMainForm.Text = "This is the Main Form"[/tt] 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.


 

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!
 
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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top