Visual Basic Question

Status
Not open for further replies.
I'm working on a project at work (using visual basic unfortunately) and I'm having some trouble loading a form. Lets say I know the name of some unloaded form as some string variable. How would I go about loading it just knowing this information? IIRC, the forms collections only iterates through the currently loaded forms so thats out. I know this is a very annoying question but any help would be greatly appreciated.
 
Originally posted by jerkins:
PreciousRoy, Global forms collection? I didn't think that existed. Ubound(Forms) gives me an error when I try to run it.

Let me retype that now that I've had a chance to try it out on a VB6 machine:

code:<HR>
Code:
Dim f as Form

For Each f in Forms
  If f.Name = strForm then f.Load
Next

Now, I'm not sure if the Forms collection only has loaded forms or not...that I can't tell you. But you can load controls by name, I'll look it up if I have a chance. I don't have Vb6 so I Remote Desktoped into a coworkers machine while he was in the bathroom. :)
 

DaleDBeast

Wise, Aged Ars Veteran
172
The problem is that the Global Forms collection does not include unloaded forms. So the workaround is to pre-load all the forms that you may need in this special way.

If you have 5 forms that you want to show using variable names, then at startup time you could preload each by referring to a property on each. Such as:
code:<HR>
Code:
    varA = Form1.Name
    varB = Form2.Name
    varC = Form3.Name
    '--OR
    Form1.Load
    Form2.Load
    Form3.Load

Either way, this will just load the forms, but not display them, and
later on, this code will then work as expected:

code:<HR>
Code:
    For x = 0 To Forms.Count - 1
        If Forms(x).Name = varB Then Forms(x).Show
    Next

Finally, to eliminate even the possibility of hard-to-realize memory leaks, at application shutdown time, from your main form it would be wise to run some code like this:
For x = 0 To Forms.Count - 1
If Forms(x).Name <> Me.Name Then
Unload Forms(x)
End If
Next
 
Originally posted by jerkins:
So how about it PreciousRoy? Did you remember or figure out how to do it? Unfortunately DaleDBeast's solution isn't generic enough for what I need to do.

I actually got tied up and this is the first chance I've had to get back to the idea since I last posted. I dug up some extensive code that creates everything in the world on the fly (using Controls.Create), and builds a completely functional form from any database table. However, I can't seem to find any code that creates a form in this manner, just everything on it. Crap. I'll keep thinking, but maybe there's another way you can approach this issue?
 

doshin

Ars Tribunus Militum
2,179
Potentially bad coding practices below
=======================================================

Have you tried using the CallByName function? I haven't used it to load forms, but other functions in different ways.

I wouldn't recommend it per se, but you may want to investigate it.

Also, you may have to create helper functions to actually load the window.
 

doshin

Ars Tribunus Militum
2,179
You have sufficiently perplexed me, so here is my two minute solution.

Create a collection keyed off form name and a property of type Form. Its a reference so dont sweat memory concerns. Next populate the collection with the list of forms that you want enumerated keyed in with Form Name like this:

FormsCollection.Add("Form 1", frmForm1);
FormsCollection.Add("Form 2", frmForm2);

When You need to, call:

FormsCollection("Form 1").FormRef.Show

Voila.

Also merely setting a reference like will NOT load it into memory so it should be fairly non invasive to the code you have wrote already.

edit: Centurion, finally.

[This message was edited by doshin on March 08, 2002 at 15:52.]
 

DaleDBeast

Wise, Aged Ars Veteran
172
DaleDBeast: That's a pretty good solution. It still requires recompiling if the list of possible forms changes, though.


Think about this for a moment:
Lets say Your application has 10 forms that may be called based on the string returned from the db. The total possibility can never be more than 10 without a recompile anyway, as you can't add an 11th form without recompiling the executable.

Back to the original question, I see two options left untried: One is the PropertyBag Interface, which allows persisting and retrieving objects and classes such as forms, and the other is the VB IDE's programmibility - there might be an interface in there that might work. Unfortunately, other options like the CallByName won't work, and calling a Windows API to load the form(I've seen this somewhere) are unlikely to leave VB in a stable posture.
 

Aeirould

Ars Tribunus Angusticlavius
7,535
Subscriptor
Hell, if all you care about is avoiding a recompile, combine PreciousRoy's bag-o-controls with a blank form in your project. Instead of having the database/INi file/whatever you're using to let the app know it's got a new form, have that method inform what controls are needed.

In the code, check the delivery mechanism, clone the blank form and Controls.Create to your heart's content to make the "dynamic" form.

It's not exactly an elegent solution, but it will allow you to make any form you desire (provided your implementation of the delivery mechanism is done well) without ever recompiling.
 

HL7

Ars Praefectus
3,523
Subscriptor++
Originally posted by DaleDBeast:
Lets say Your application has 10 forms that may be called based on the string returned from the db. The total possibility can never be more than 10 without a recompile anyway, as you can't add an 11th form without recompiling the executable.

You're right.

jerkins, why isn't Dale's solution generic enough, considering this?
 
Status
Not open for further replies.