Implements ICloneable. form disappear .

PublicClass FormTemplate

Inherits Form

Implements ICloneable

PrivateSub BaseForm_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load

EndSub

PublicFunction Clone()AsObjectImplements System.ICloneable.Clone

ReturnMe.MemberwiseClone

EndFunction

EndClass

PublicClass MainMenu

Inherits FormTemplate

Public m_ChildFormNumberAsInteger = 0

PublicFormsAsNew List(OfString)

PublicSubNew()

InitializeComponent()

EndSub

PublicSub NewForm(Of tAs FormTemplate)(ByRef iAs t)

Dim fAs t

f = i.Clone

IfNot Forms.Contains(f.GetType.ToString)Then

m_ChildFormNumber += 1

f.Show()

Forms.Add(f.GetType.ToString)

AddHandler i.FormClosing,AddressOf Form_Closing

AddHandler i.LostFocus,AddressOf Form_LostFocus

Else

f.BringToFront()

EndIf

EndSub

PublicSub Form_LostFocus(ByVal sAsObject,ByVal eAs System.EventArgs)

Try

mynav.BindingSource =Nothing

Catch exAs Exception

EndTry

EndSub

PublicSub Form_Closing(ByVal sAsObject,ByVal eAs FormClosingEventArgs)

Forms.Remove(s.GetType.ToString)

m_ChildFormNumber -= 1

EndSub

EndClass

PublicClass Form2

inherits mainmenu

PrivateSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.Click

Dim fAsNew Form1

Me.NewForm(f)

f.MdiParent =Me

EndSub

EndClass

The code works nice, but the form disappears if i click on the button like 4-5 times . that is it is nicely bringing form1 on front as in the main menu code, and then just disappears if this is done multiple times., okay i can go about disabling the menu, but this code seemed the best whereby this kind of bug should be handled., waht could it be about the form_closed is not triggered .

[8387 byte] By [hrubesh] at [2007-12-24]
# 1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim f As New Form1

Me.NewForm(f)

f.MdiParent = Me

End Sub

i also want to know how memory usage is affected like that... i am creating an instance of form1 .. new form1 each time to see if it exists.. . my initial setup was that i had dim f as form1.. is memory allocation for that already made at when mainmenu is initialiseD? okay wat is the difference.:

case 1 :class mainmenu { dim f as form1 }

case 2: class mainmenu{ dim f as new form1 }

my point of view ( im not sure) is the memory allocation is same in both case, and case 2: initialised vars.

so i guess (2) will run faster. but now.,

case 3 :

class mainmenu{ Private Sub Button1_Click( ) Handles Button1.Click

Dim f As New Form1

} okay how is memory allocation compared to case 1 and 2 and then how will case 3 differ if event occurs several times- thats using the reference from code above.

hrubesh at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

Cloning a form using MemberwiseClose is a really bad idea. MemberwiseClose will just copy the internal member fields of the Form class and amongst them there is at least one that's going to be very unhappy if you clone it. That's the window handle and if you clone it you can end up with a couple of forms that use the same window handle and this is not going to work or it will work randomly.

I'm not sure what are you trying to achieve but you should find another way. Why do you want to clone the FormTemplate anyway ? Just create the Form you need and add it to the MDIParent...

MikeDanes at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
my main menu contains a menu that will contain about 50-70 items and the code is really bad on each one im minimising it so i have a method that will handle creation of any form and add required handlers and so on. plus the main menu itself is a base class for all programs im writing. i dont know how to go about that now.
hrubesh at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4

I still don't get it. In Button1_Click you create new form, Form1, and you pass it to Me.NewForm. OK.

Now why NewForm needs to Clone the form ? Why it doesn't simply use the passed in form ?

MikeDanes at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

true somewhere i got lost, my initial purpose was not to have the new Type in my click method. it was dim s as bird

and then in the click

newform(s).. .

anyway., im back to

Public Sub NewForm(ByRef F As Form)

F.Show()

AddHandler F.FormClosing, AddressOf Form_Closing

AddHandler F.LostFocus, AddressOf Form_LostFocus

End Sub

but now i get a new problem,, when i click on the forms., they stay behind, the usual event is that they get focus and brought to front automatically isnt it? my form are staying behind. why is that ? is is about the event? doesnt make sense does it.

hrubesh at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6
i actually went on a round trip u know. :(
hrubesh at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7

Try

f.Show(Me)

instead of

f.Show()

This will set the owner of the f form to be the MainForm so it stays on top it.

MikeDanes at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8

Dim f As New Form3

Me.NewForm(f)

'f.MdiParent = Me

f.mdiparent was uncommented,.. it was working. but forms overllapped and wouldnt come forward

now with your f.show(me)... i guess it is doing the same thing isnt it?

but it works fine:)

hrubesh at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 9

Ah... this was a MDI child... i forgot... then Show(Me) should have no effect... what do you mean by "forms overlapped" ?

MikeDanes at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 10

But now my navigator would not obey right.

if i open 2 forms each having a dgv.,, ckeck

Private Sub DGV_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus

Me.mynavigator = MainMenu.mynav

mynavigator.BindingSource = Me.DataSource

End Sub

now this code site inside a custom navigator.

and this sits on a form that inherits from mainmenu. the commented code have been really commented out.

Public Sub NewForm(ByVal F As Form)

F.Show(Me)

AddHandler F.FormClosing, AddressOf Form_Closing

'AddHandler F.LostFocus, AddressOf Form_LostFocus

End Sub

'Public Sub Form_LostFocus(ByVal s As Object, ByVal e As System.EventArgs)

' Try

' mynav.BindingSource = Nothing

' Catch ex As Exception

' End Try

' End Sub

Public Sub Form_Closing(ByVal s As Object, ByVal e As FormClosingEventArgs)

mynav.BindingSource = Nothing

End Sub

hrubesh at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 11

oh show me is having full effect.

overlap.

the form on top is remaining on top even though focus is changing to the one i clkick behind.

the one behind doesnt come front,. with show (me) i dont have this problem

hrubesh at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 12

stupid things that were hanging me:

1

--

' Private Sub DGV_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus

' mynavigator.BindingSource = Nothing

' Me.mySave = Nothing

'End Sub

2

Dim f As New Form3

f.MdiParent = Me

Me.NewForm(f)

'f.MdiParent = Me

i was putting the f.mdiparent after the form is shown.

hrubesh at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...