vb.net - How do I retain a form's control values when flipping between forms -
it takes 2 forms able come in info on particular transaction. want able flip , forth between these 2 forms retaining entered on each until 'save' button clicked.
i think should able utilize form2.show, me.hide , form1.show, me.hide. first time go form2 goes thru form2 load event (that’s reasonable) knowledge of command contents on form1 have been lost. though form1 hidden (and not closed) contents of controls gone. why?
the sec time go form2 load event not fire because form2 hidden , @ point form1 command contents available. so, in flipping , forth between form1 , form2 works want after go form2 sec time. but, need work first time , every time.
for days i’ve been trying understand this. i’ve commented out every line of code, stepped thru code, googled till i’m bluish in face (there has been lot written this), , still can’t figure out why behavior occurs.
can explain phenomenon? or improve yet tell me need create work.
i have code behind form1 button goes form2
if form2 nil dim form2 new form2 end if form2.show() me.hide()
and code behind form2 button homecoming form1
form1.show me.hide
this missing:
class form1 private f2 form2 ' form1's reference ' form2 instance
later when click go form2, original code needs little tweak:
if f2 nil f2 = new form2(me) ' set declared variable new instance end if f2.show() me.hide()
in case form1 passing reference using the trick shown before using constructor:
sub new(frm form1) ' in form2 f1 = frm end sub
you dont need in form1 because he/it creating own f2 object reference.
the main problem in original code, was: dim form2 new form2
. creating new form2 each time (i suspect resides in event or sub). new instances
can't know command values in previous instances. declaring f1
or f2
shown gives module/form level scope
.
dim
declares variable , type. f1 of type form1. not create object if object variable
new
creates instance of object type (reference types). straight relates sub new
method in class. when utilize new
, sub new
called special needed can take place there. value types integer
not need created or instanced, declared.
where declare (dim
) variable determines scope
. if in sub, variable or object exists in sub. if @ form/class level, has form/class level scope.
vb.net winforms
No comments:
Post a Comment