Refer to Form by String (Visual Studio 2008) Part 2
Posted on 2009-12-28
Thanks to everyone for their suggestions on how to refer to a form by using a string variable. I now have the code working fine and is posted below. The code in bold is the core code for the string variable usage, the rest of the code determines how many of the same MdiChild (frm_Junk in this case) are already open and passes this number to the next instance of the same MdiChild to be opened via a queue.
I am allowing the user to open multiple instances of frm_Junk because he/she can display different information in each instance and visually compare this information.
Dim str_Form As String = "frm_Junk"
Dim myForm As New Form
myForm = CreateForm(str_Form)
Cursor.Current = Cursors.WaitCursor
' Determine number of forms open.
For Each obj_Form In Application.OpenForms
If obj_Form.name.ToString.IndexOf(str_Form) >= 0 Then
int_Count += 1
' Provide form offset pointer.
If int_Count = 0 Then
int_Forms = 0
int_Forms += 1
' Open form.
myForm.MdiParent = Me
frm_Junk has the following code that positions the form within the MdiParent prior to the form actually being displayed. I am doing this to display the form where I want it on the screen and prevent a brief display of the form in some arbitrary position.
Then problem I am having is frm_Junk opens as soon as encounter the myForm = CreateForm(str_Form) line in the calling program (above). The result is the rest of the lines in the calling program arent processed, nothing is put in queue, and frm_Junk is opened as a regular form rather than a MdiChild.
Public Class frm_Junk
Public Sub New()
' Position form prior to loading.
'Dim f As Form = New Form()
Dim int_Count As Integer = 0
Dim int_Forms As Integer = queue_Parameters.Dequeue
Dim int_Location_x As Integer
Dim int_Location_y As Integer
If int_Forms > 0 Then
Me.Text += " [" & int_Forms & "]"
Me.Location = New Point(int_Location_x + (int_Mdi_x_Offset_Increment * int_Forms), _
int_Location_y + (int_Mdi_y_Offset_Increment * int_Forms))
Me.StartPosition = FormStartPosition.Manual
QUESTION: How can I refer to the form as a string (as Im doing in the calling program), yet still have the Public Sub New constructor in frm_Junk? I really need to be able to position each instance of frm_Junk at a specific position on the MdiParent without the tacky looking screen flicker resulting from frm_Junk first opening and then getting repositioned?
Thank you in advance for your suggestions!