StrataFrame Forum
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      


12»»

Open a form - simple Not!Expand / Collapse
Author
Message
Posted 06/05/2007 10:49:31 PM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: 11/12/2008 5:30:54 AM
Posts: 234, Visits: 1,176
Hi,

No doubt there is an easy solution to this but after serveral hours and failing to find it why not ask the experts? I have a project that contains a meu form (DevExpress NavBar) which is populated from the database. So, I click on an item and I now have a form name which I want to open. Here's the hard part - I can't find any way to iterate through all the forms in my project and .Show the right one, i.e. I can find a forms collection (open forms I can see but not all the forms in my project). I'm just about ready to write a large Case statement to, e.g

Select Case FormName
 Case "Form1"
          My.Forms.Form1.Show()
 Case "Form2"
          My.Forms.Form2.Show()

but obviously I don't want to do that. What's the best way of doing things?

Cheers, Peter

Post #9345
Posted 06/06/2007 7:09:36 AM


StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: 07/09/2008 2:20:16 PM
Posts: 436, Visits: 944
Peter, I too had trouble with this once... here's the help i received...

http://forum.strataframe.net/Topic8063-14-1.aspx

You'd think there SHOULD be an easier way to do this. Oh well.
Post #9346
Posted 06/06/2007 9:20:49 AM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: 10/21/2008 9:20:58 AM
Posts: 2,685, Visits: 1,887
Yep, the link that Mike pointed you to is correct... you'll need to use Type.GetType() and pass the full name of the form you want to show.  So, something like this:

DirectCast(Activator.CreateInstance(Type.GetType(FormName)), Form).Show()

The Type.GetType() is a method that accepts a string name for the form (i.e.: "MyNamespace.MyFormName") and returns the System.Type for that form.  The Activator.CreateInstance() accepts a type and creates a new instance of it through reflection to find the default constructor.  The DirectCast just CTypes the returned object as a Form, and Show() is pretty self explanatory. 

So, the only change you'll need to make is to change the FormName that you're storing off in DevEx's menu from just "Form1" to "MyNamespace.Form1" so that you will have the full type name of the form you want to show.


www.bungie.net
Post #9354
Posted 06/06/2007 11:38:30 AM


StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: Forum Members
Last Login: 10/31/2007 5:20:05 PM
Posts: 374, Visits: 1,197
I just went thru that in C# and GetType (that is used in VB) threw me off until Ben (Chase) came to rescue.

..ßen
Post #9370
Posted 06/06/2007 12:00:23 PM
StrataFrame VIP

StrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIP

Group: StrataFrame Users
Last Login: Yesterday @ 4:08:06 PM
Posts: 1,323, Visits: 3,452
Ben (Chase),

Could you post a little code sample that shows how to do this if you want to pass values to a constructor of the form? This topic is in the category of "I get it until I have to code it...then I'm confused"
Post #9373
Posted 06/06/2007 5:25:33 PM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: 11/12/2008 5:30:54 AM
Posts: 234, Visits: 1,176
Hi Guys,

Thanks for your input and Ben, thanks for taken the time to explain what was happening in that line of code - very helpful.

All in all this is sooooo depressing - it seems that every time I learn a bit more about .Net it only serves to underline how little I know!!

Cheers, Peter

Post #9387
Posted 06/06/2007 6:08:03 PM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: Yesterday @ 5:04:58 PM
Posts: 4,780, Visits: 4,744
The Activator accepts a Param Array as a second parameter which allows you to provide parms to the constructor of a created instance:

Activator.CreateInstance(GetType(MyForm), New Object() {Parm1})

or

Activator.CreateInstance(GetType(MyForm), Parm1)
Post #9388
Posted 06/06/2007 6:57:08 PM
StrataFrame VIP

StrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIP

Group: StrataFrame Users
Last Login: Yesterday @ 4:08:06 PM
Posts: 1,323, Visits: 3,452
Thanks Trent. Slowly its starting to make sense....
Post #9390
Posted 06/07/2007 12:20:27 AM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: 11/12/2008 5:30:54 AM
Posts: 234, Visits: 1,176
Hi Guys,

A follow up - I want to check if a form is already open before I instasiate a new one. I know (thought I knew) how to do this with (and other variations on this theme):       

        Dim OpenForms As FormCollection
        OpenForms = Application.OpenForms

        ' Lets see if the form is already open and, if it is, bring it to the front.
        For i As Integer = 0 To System.Windows.Forms.Application.OpenForms.Count - 1
            If OpenForms(i).Tag.ToString = e.Link.Item.Name.ToString Then
                OpenForms(i).WindowState = FormWindowState.Normal
                OpenTheForm = False
                Exit For
            End If
        Next

However, OpenForms.Count always returns zero (even the first time in when just the menu form itself if running). I've been Googling madly to see if there is any reference to the OpenForms.Count being zero and I couldn't find anything - lots of reference to the property in examples of working code - no reference to any problems/caveats.

Any ideas?

Cheers, Peter

Post #9397