vb.net - Passing data between forms DIRECTLY -


i'm making "preference form" hold users preferences , when go apply/save want new values transfer main form , updateand close form2. in past have done this:

private sub preferencestoolstripmenuitem_click(sender object, e eventargs) handles preferencestoolstripmenuitem.click     preferences.show() end sub 

and when click "apply/save" button before closes transfer data this:

form1.textbox.text = form2.textbox.text 

is there wrong doing way??

what have been reading should doing this:

private sub preferencestoolstripmenuitem_click(sender object, e eventargs) handles preferencestoolstripmenuitem.click     dim dialog new preferences     dialog.showdialog() end sub 

and when when click "apply/save" take values form2 , store them in private variable (or property) in form2 , when form closes access value this:

private sub preferencestoolstripmenuitem_click(sender object, e eventargs) handles preferencestoolstripmenuitem.click     dim dialog new preferences     dialog.showdialog()      form1.textbox.text = dialog.variable end sub 

why better way of doing this?

update....looking @ code below small sample of options have. best way collect of data object use when serializing?

    <serializable> public class preference  #region "properties"     public property scalelowest string = "5"     public property scalehighest string = "200"     public property scaleinc string = "5"     public property thicklowest double = 0.125     public property thickhighest double = 4     public property thickinc double = 0.125     public property widthlowest double = 0.125     public property widthhighest double = 0.6     public property widthinc double = 0.125     public property lengthlowest double = 1     public property lengthhighest double = 96     public property lengthinc double = 1     public property fractionon boolean = false     public property decimalon boolean = true     public property colorselection string = "colors"     public property finalcolor string = "255, 255, 0"     public property roughcolor string = "255, 255, 100"     public property splashon boolean = false     public property loginon boolean = false #end region      public sub new()         'for creating new instance deserializing     end sub      public sub gatheralldata()          'save defaults         saveserializeobj()      end sub      public sub saveserializeobj()         'get changes?????          'serialize object text file.         dim objstreamwriter new streamwriter("c:\users\zach454\desktop\test.xml")         dim x new xmlserializer(me.gettype)         x.serialize(objstreamwriter, me)         objstreamwriter.close()      end sub      public function loadserializeobj() preference          'check if new file need created         if file.exists("c:\users\454\desktop\test.xml") = false             saveserializeobj()         end if          'deserialize text file new object.         dim objstreamreader new streamreader("c:\users\454\desktop\test.xml")         dim newobj new preference         dim x new xmlserializer(newobj.gettype)         newobj = ctype(x.deserialize(objstreamreader), preference)         objstreamreader.close()          return newobj     end function 

the best option create class have properties form controls. can store these properties , access these when needed.

also there's no reason passing data , forth, can store data off somewhere (database, file, mysettings etc) , load data class. can store , retrieve data class. if need save data somewhere have class object use.

here short example show how can create form (preferences) click save , show values on other form (calling form).

this main form

public class form1     public _frm2 form2      private sub btnshowpreferences_click(sender object, e eventargs) handles btnshowpreferences.click         using _frm2 new form2()             'if clicked save on form show values in              'controls want             if _frm2.showdialog() = windows.forms.dialogresult.ok                 txtfirstname.text = _frm2._preferences.firstname                 txtlastname.text = _frm2._preferences.lastname             end if         end using     end sub  end class 

here example (preferences) class

this class hold properties preferences. example, can change need suit needs.

option strict on  public class preferences  #region "properties"     public property firstname string     public property lastname string #end region      public sub new()      end sub  end class 

the second form (preference) form controls user need interact with.

public class form2      public _preferences new preferences 'create class variable can use later store data      private sub btnsave_click(sender object, e eventargs) handles btnsave.click         'set properties of class form. hold can         'the first form...         _preferences             .firstname = txtfirstname.text             .lastname = txtlastname.text         end          me.dialogresult = windows.forms.dialogresult.ok 'this used determine if user clicked save button...      end sub end class 

i hope get's started, if not understand please let me know.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -