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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Varaiable holding the reference to the form you created? 1

Status
Not open for further replies.

Akart

IS-IT--Management
Nov 14, 2002
71
0
0
AU
Hello,

Can someone tell me how to create a varaiable holding the reference to a form?

Regards,

Akart
 
' set a variable to be an instanvce of the form
dim x as frmYouCreated
' set the varaiable as an instance of the form
x= new frmYouCreated()
' show the form
x.Show()

 
Thanks for your reply.

I still need a little help tho.

I made a module that has a public form where i reference the open form.

#Region "Forms"
Public ImportForm As Form
#End Region

I then have the following code, when executed opens my form and will only allow it to open once.

It feels like there is a problem with the logic, any suggestions would be appreciated!!

Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
Try
ImportForm.Close()
ImportForm = Nothing
ImportForm = New Import()
ImportForm.MdiParent = Me.MdiParent
ImportForm.Show()
Catch ex As Exception
ImportForm = New Import()
ImportForm.MdiParent = Me.MdiParent
ImportForm.Show()
Finally

End Try
End Sub
 
It is only opening it once as you close the existing form!
Let us look at the code and see what it does - i will comment existing code:

#Region "Forms"
' create a variable for holding a generic form. since the
' code only uses the "Import" form i would have made this
' Public ImportForm As Import
' but there really is no need.
Public ImportForm As Form
#End Region
.
.
Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
Try
' if there is an existing instance of the form
' referenced by "ImportForm" then close it. This
' is why you beleive that the code only allows you
' to open one instance. in fact it does open the new
' instance after first closing the existing instance.
ImportForm.Close()
' general cleanup of variables.
ImportForm = Nothing
' create a reference to a new "Import" form and assign
' the reference to our variable
ImportForm = New Import()
' general parent setting. Though this is setting the
' parent as the parent of the calling form?! Not
' knowing the entire code, i am not sure if this is
' the case.
ImportForm.MdiParent = Me.MdiParent
' show the form that is referenced by our "ImportForm"
' variable
ImportForm.Show()
Catch ex As Exception
' The catch ignores all errors and doesn't care
' what it was. in fact the first time through this code
' this code will be executed after the code attempts
' to close a form referenced by our varaiable that does
' not reference a form!
' this creates a reference to the form
ImportForm = New Import()
' sets the parent
ImportForm.MdiParent = Me.MdiParent
' shows the form
ImportForm.Show()
Finally
' no finally code
End Try
End Sub

A suggested implementation would be as follows:

Public ImportForm As Import

Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
Try
' we do not want to close the existing form referenced
' by the "ImportForm" variable.
' create a reference to a new "Import" form and assign
' the reference to our variable
ImportForm = New Import()
' general parent setting, this should allow us to get to
' the form created via the parent-child relationship,
' rather than the ImportForm variable
' I called this code from my actual parent, so the
' reference is me.
ImportForm.MdiParent = Me
' show the form that is referenced by our "ImportForm"
' variable
ImportForm.Show()
Catch ex As Exception
' The catch ignores all errors and doesn't care
' what it was. but we should know while developing!
' so we might be able to avert poor coding
MsgBox(ex.Message, MsgBoxStyle.Critical, ex.Source)
Finally
' we no longer need to reference the created child form
' via our variable, in fact we should not! this child
' may close without us catching the close. plus, we
' would then need to keep all references in an array.
' but shice the mdiparent provides this control auto-
' matically, we will reference it in that fashion.
' clean up the variable
ImportForm = Nothing
' let us see how many children the mdiparent has! you
' can close the children and this value is correct for
' the count of actual live children.
Debug.WriteLine("Children=" + Me.MdiChildren.Length.ToString)
' the above code might be employed like this if the
' original parent assignment was correct
' Debug.WriteLine("MdiParent.Children=" + Me.MdiParent.MdiChildren.Length.ToString)
End Try
End Sub

Hope this helps, sorry i didn't monitor my emails more closely and get back to you before now.


 
Hi, Thanks for replying but I am a goose.
I did not mention in my previous post that getting the form to open once only was what i wanted.

My question should have been this:

I then have the following code, when executed opens my form and will only allow it to open once which is what i want it to do.

However, is there a way to check if the form is already open, instead of closing the form (which causes an error if the form is not open which is why i had to open the form in the catch section aswell as in the try section)

I hope i have explained it better this time.

Your post was informative so i will give it a star.

Regards,

Akart
 
Akart,

This is a singleton pattern. Look it up in Help

Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top