instantiation

mzim

Well-known member
Joined
Jun 3, 2004
Messages
187
Location
Other side of the rock
Programming Experience
1-3
instantiation(solve)

why it cannot instantiate the form?
what's wrong w/ it?
VB.NET:
   Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
   		Dim f As Form1
   		checkinstantiation(f)
   	End Sub
   	Function checkinstantiation(ByVal fr As Form)
   		If fr Is Nothing Then
  			fr = New fr()<-----it gets an error here:[i]"fr is not defined"[/i]
   			fr.Show()
   			fr.MdiParent = Me
   		Else
   			fr.Activate()
   		End If
   	End Function
 	Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
   		Dim f1 As Form2
   		checkinstantiation(f1)
   	End Sub
 	Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
   		Dim f2 As Form3
   		checkinstantiation(f2)
thanks in advance
 
Last edited:

mzim

Well-known member
Joined
Jun 3, 2004
Messages
187
Location
Other side of the rock
Programming Experience
1-3
ok i solve it...
VB.NET:
 Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
 		Dim f As New Form1()
 		checkinstantiation(f)
 	End Sub
 	Function checkinstantiation(ByVal fr As Form)
 		Dim x As Form
 		For Each x In Me.MdiChildren
 			If fr.Name = x.Name Then
 				x.Activate()
 				Exit Function
 			End If
 		Next
 		fr.MdiParent = Me
 		fr.Show()
 	End Function
 	Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
 		Dim f1 As New Form2()
 		checkinstantiation(f1)
 	End Sub
 	Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
 		Dim f2 As New Form4()
 		checkinstantiation(f2)
 	End Sub
 

mzim

Well-known member
Joined
Jun 3, 2004
Messages
187
Location
Other side of the rock
Programming Experience
1-3
thanks paszt for giving me the clearer one..
i have this code of mine..
is it advisable to do it this way.
i just make a function to call in each menu click event
VB.NET:
 Dim f1 As Form2
 	Dim f As Form1
 	Dim f2 As Form4
 	Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
 		If f Is Nothing Then
 			f = New Form1()
 			checkinstantiation(f)
 		Else
 			f.Activate()
 		End If
 	End Sub
    
 	Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
 		If f1 Is Nothing Then
 			f1 = New Form2()
 			checkinstantiation(f1)
 		Else
 			f1.Activate()
 		End If
 	End Sub
 	Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
 		If f2 Is Nothing Then
 			f2 = New Form4()
 			checkinstantiation(f2)
 		Else
 			f2.Activate()
 		End If
 	End Sub
 
 	Function checkinstantiation(ByVal fr As Form)
  		Dim x As Form
  		For Each x In Me.MdiChildren
  			If fr.Name = x.Name Then
  				x.Activate()
  				Exit Function
  			End If
  		Next
  		fr.MdiParent = Me
  		fr.Show()
  	End Function
more power
 
Last edited:
Top Bottom