﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » .NET Forums » General .NET Discussion  » Open a form - simple Not!</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Mon, 01 Jun 2026 18:21:00 GMT</lastBuildDate><ttl>20</ttl><item><title>Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9345.aspx</link><description>Hi,&lt;/P&gt;&lt;P&gt;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&lt;/P&gt;&lt;P&gt;Select Case FormName&lt;BR&gt;&amp;nbsp;Case "Form1"&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; My.Forms.Form1.Show()&lt;BR&gt;&amp;nbsp;Case "Form2"&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; My.Forms.Form2.Show()&lt;/P&gt;&lt;P&gt;but obviously I don't want to do that. What's the best way of doing things?&lt;/P&gt;&lt;P&gt;Cheers, Peter</description><pubDate>Thu, 07 Jun 2007 22:29:40 GMT</pubDate><dc:creator>Peter Jones</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9429.aspx</link><description>No problem :)&amp;nbsp; I just typed this out in the post and did not bring it in from code, so if you have an issue with what I posted let me know.&amp;nbsp; But it should at least get you most of the way there. :)</description><pubDate>Thu, 07 Jun 2007 22:29:40 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9425.aspx</link><description>This is why StrataFrame [b]ROCKS[/b]!&lt;br&gt;
&lt;br&gt;
Thanks Trent!  This really (finally) clears up what you've mentioned a bunch of times in different posts about how to handle opening forms in an Application.  I really appreciate it.</description><pubDate>Thu, 07 Jun 2007 17:45:09 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9409.aspx</link><description>You can do that, but you first need to have a centralized collection that can be referenced.&amp;nbsp; You are actually better off doing this outside of the instantiaion for one primary reason...speed and maintainability...OK, that is two reasons :)&amp;nbsp; We created a shared class called MenuSystem which houses all of our instantiation logic.&amp;nbsp; Security, form collections, multiple-instances, and whether the form will appear in the MDI window or on its own, all within one central location.&amp;nbsp; Ours had to be a bit more specialized since it can launch VFP forms or .NET forms.&amp;nbsp; But the logic is basically the same.&lt;/P&gt;&lt;P&gt;You could even create a class that is passed to the MenuSystem that has all of the logic that you need that you could pass over....or just create a bunch of overloads to do this for you.&amp;nbsp; It is just up to you, but the key is creating a class that launches all of your forms according to your environment and needs.&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;Public Notineritable Class MenuSystem&lt;BR&gt;&lt;BR&gt;Private Shared _MenuMdiClient As MDIClient&lt;BR&gt;Private Shared _Forms As New System.Collections.Generics.List(Of SYstem.Windows.Forms.Form)&lt;BR&gt;&lt;BR&gt;'-- Reference to the MDI client to which all client forms will be added.&amp;nbsp; Be sure to set this in your&lt;BR&gt;'&amp;nbsp;&amp;nbsp;&amp;nbsp; New of the parent that houses the MDI client.&lt;BR&gt;Public SHared Property MenuMdiClient As MDIClient&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Get&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Return _MenuMdiClient&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Get&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Set(Byval value as MdiClient)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _MenuMdiClient = value&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End Set&lt;BR&gt;End Property&lt;BR&gt;&lt;BR&gt;'-- This collection contains all of the forms that are loaded&lt;BR&gt;Public Shared Readonly Property Forms As System.Collections.Generics.List(Of System.Windows.Forms.Form)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Get&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Return _Forms&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Get&lt;BR&gt;End Property&lt;BR&gt;&lt;BR&gt;'-- Central method to load all of the &lt;BR&gt;Public Shared Sub LaunchForm(ByVal FormType As System.Type, Byval SecurityKey As String, Byval SingleInstance As Boolean)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Establish Locals&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim loForm As System.WIndows.Forms.Form&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; If SecurityBasics.CurrentUser.GetPermission(SecurityKey).Action = Grant&amp;nbsp;Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Check for a single instance&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If SingleInstance AndAlso&amp;nbsp;FormIsRunning(FormType) Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Exit Sub&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Create and launch the form&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; loForm = CType(Activator.CreateInstance(FormType), System.WIndows.Forms.Form)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; loForm.MdiParent = _MenuMdiClient&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Create a handler so it can remove itself from the collection&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; AddHandler&amp;nbsp;loForm.FormClosed, Addressof HandleFormClosed&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; loForm.Show()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; Private Shared Function FormIsRunning(Byval FormType As System.Type) As Boolean&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim llReturn As Boolean = False&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For Each loForm As Form In _Forms&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If loForm.GetType() Is FormType Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; llReturn = True&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Exit&amp;nbsp;For&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; End If&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Next&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Return llReturn&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Function&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private Shared Sub HandleFormClosed(Byval sender as Object, Byval e as System.EventArgs)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; _Forms.Remove(CType(sender, Form))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;End Class&lt;/FONT&gt;</description><pubDate>Thu, 07 Jun 2007 13:16:30 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9408.aspx</link><description>Would it be possible to put some logic into the form's themselves, to see if an instance is already running?  &lt;br&gt;
&lt;br&gt;
I.e. before the InitializeComponents is called in the constructor, check if the form is already instantiated. Maybe use a shared property to note if the form has been instantiated yet (and hold a reference to the instantiated form).  Or maybe use a shared factory method to create the form instead of using New.  &lt;br&gt;
&lt;br&gt;
Not sure if this is feasible or if there is any way to cancel the instantiation of a form and instead return a reference to an existing instance, but I seem to recall reading about the Singleton pattern and doing it something like this.</description><pubDate>Thu, 07 Jun 2007 12:27:24 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9405.aspx</link><description>If you are running your forms through an MDI environment then you can just reference the MDIChildren collection and create a method to tell you if it is running.&amp;nbsp; We have a MenuSystem class that we pass all of our forms through and then create handlers so that the form will remove itself from our own collection.&amp;nbsp; Either way will work.&amp;nbsp; Below is a sample of how to reference the MDIChildren collection to see if a form is within the MDI collection.&lt;P&gt;[codesnippet]Private Function IsFormRunning(ByVal FormType As System.Type) As Boolean&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Establish Locals&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim llReturn As Boolean = False&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Cycle through all of the children forms within this MDI&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For Each loForm As Form In Me.MdiChildren&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; llReturn = loForm.GetType() Is FormType&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If llReturn Then Exit For&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Next&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Return llReturn&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Function[/codesnippet]&lt;/P&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&lt;/P&gt;&lt;/FONT&gt;</description><pubDate>Thu, 07 Jun 2007 09:51:35 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9397.aspx</link><description>Hi Guys,&lt;/P&gt;&lt;P&gt;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):&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim OpenForms As FormCollection&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OpenForms = Application.OpenForms&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Lets see if the form is already open and, if it is, bring it to the front.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For i As Integer = 0 To System.Windows.Forms.Application.OpenForms.Count - 1&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If OpenForms(i).Tag.ToString = e.Link.Item.Name.ToString Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OpenForms(i).WindowState = FormWindowState.Normal&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OpenTheForm = False&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Exit For&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Next&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;Cheers, Peter&lt;BR&gt;</description><pubDate>Thu, 07 Jun 2007 00:20:27 GMT</pubDate><dc:creator>Peter Jones</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9390.aspx</link><description>Thanks Trent.  Slowly its starting to make sense....  :alien:</description><pubDate>Wed, 06 Jun 2007 18:57:08 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9388.aspx</link><description>The Activator accepts a Param Array as a second parameter which allows you to provide parms to the constructor of a created instance:&lt;/P&gt;&lt;P&gt;[codesnippet]Activator.CreateInstance(GetType(MyForm), New Object() {Parm1})[/codesnippet]&lt;BR&gt;or&lt;BR&gt;&lt;BR&gt;[codesnippet]Activator.CreateInstance(GetType(MyForm), Parm1)[/codesnippet]</description><pubDate>Wed, 06 Jun 2007 18:08:03 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9387.aspx</link><description>Hi Guys,&lt;/P&gt;&lt;P&gt;Thanks for your input and Ben, thanks for taken the time to explain what was happening in that line of code - very helpful.&lt;/P&gt;&lt;P&gt;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!!&lt;/P&gt;&lt;P&gt;Cheers, Peter</description><pubDate>Wed, 06 Jun 2007 17:25:33 GMT</pubDate><dc:creator>Peter Jones</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9373.aspx</link><description>Ben (Chase),&lt;br&gt;
&lt;br&gt;
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"  :blink:</description><pubDate>Wed, 06 Jun 2007 12:00:23 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9370.aspx</link><description>I just went thru that in C# and GetType (that is used in VB) threw me off until Ben (Chase) came to rescue.</description><pubDate>Wed, 06 Jun 2007 11:38:30 GMT</pubDate><dc:creator>Ben Hayat</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9354.aspx</link><description>Yep, the link that Mike pointed you to is correct... you'll need to use Type.GetType() and pass the &lt;EM&gt;full &lt;/EM&gt;name of the form you want to show.&amp;nbsp; So, something like this:&lt;/P&gt;&lt;P&gt;DirectCast(Activator.CreateInstance(Type.GetType(FormName)), Form).Show()&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; The Activator.CreateInstance() accepts&amp;nbsp;a type and creates a new instance of it through reflection to find the default constructor.&amp;nbsp; The DirectCast just CTypes the returned object as a Form, and Show() is pretty self explanatory.&amp;nbsp; &lt;/P&gt;&lt;P&gt;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.</description><pubDate>Wed, 06 Jun 2007 09:20:49 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: Open a form - simple Not!</title><link>http://forum.strataframe.net/FindPost9346.aspx</link><description>Peter, I too had trouble with this once... here's the help i received...&lt;br&gt;
&lt;br&gt;
http://forum.strataframe.net/Topic8063-14-1.aspx&lt;br&gt;
&lt;br&gt;
You'd think there SHOULD be an easier way to do this. Oh well.</description><pubDate>Wed, 06 Jun 2007 07:09:36 GMT</pubDate><dc:creator>StarkMike</dc:creator></item></channel></rss>