Implements ICloneable. form disappear .
Public
Class FormTemplateInherits FormImplements ICloneablePrivateSub BaseForm_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.LoadEndSubPublicFunction Clone()AsObjectImplements System.ICloneable.CloneReturnMe.MemberwiseCloneEndFunctionEnd
ClassPublicClass MainMenuInherits FormTemplatePublic m_ChildFormNumberAsInteger = 0PublicFormsAsNew List(OfString)PublicSubNew()InitializeComponent()
EndSubPublicSub NewForm(Of tAs FormTemplate)(ByRef iAs t)Dim fAs tf = i.Clone
IfNot Forms.Contains(f.GetType.ToString)Thenm_ChildFormNumber += 1
f.Show()
Forms.Add(f.GetType.ToString)
AddHandler i.FormClosing,AddressOf Form_ClosingAddHandler i.LostFocus,AddressOf Form_LostFocusElsef.BringToFront()
EndIfEndSubPublicSub Form_LostFocus(ByVal sAsObject,ByVal eAs System.EventArgs)Trymynav.BindingSource =
NothingCatch exAs ExceptionEndTryEndSubPublicSub Form_Closing(ByVal sAsObject,ByVal eAs FormClosingEventArgs)Forms.Remove(s.GetType.ToString)
m_ChildFormNumber -= 1EndSubEnd
ClassPublic
Class Form2inherits mainmenu
PrivateSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.ClickDim fAsNew Form1Me.NewForm(f)f.MdiParent =
MeEndSubEnd
ClassThe 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]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form1Me.NewForm(f)f.MdiParent =
MeEnd 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.
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...
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.
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 ?
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_ClosingAddHandler F.LostFocus, AddressOf Form_LostFocusEnd Subbut 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.
i actually went on a round trip u know. :(
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.
Dim
f As New Form3Me.NewForm(f)'f.MdiParent = Mef.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:)
Ah... this was a MDI child... i forgot... then Show(Me) should have no effect... what do you mean by "forms overlapped" ?
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.GotFocusMe.mynavigator = MainMenu.mynavmynavigator.BindingSource =
Me.DataSourceEnd Subnow 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_LostFocusEnd 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 SubPublic Sub Form_Closing(ByVal s As Object, ByVal e As FormClosingEventArgs)mynav.BindingSource =
NothingEnd Suboh 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
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 Sub2
Dim
f As New Form3f.MdiParent =
MeMe.NewForm(f)'f.MdiParent = Mei was putting the f.mdiparent after the form is shown.