Object passing with Windows Forms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eXtreme

    Object passing with Windows Forms

    I do the following:

    1 - create a parent form called Options & put a listbox on the form lstURLs.
    2 - create a child form called OptionsAdd
    3 - in the parent form I create a line "Form dlgOptionsAdd = new
    OptionsAdd(ref this.lstURLs);"
    4 - in the the child form I modify "public OptionsAdd() {}" to be "public
    OptionsAdd(ref ListBox lstURLs){}"

    When I do this I'm only able to access the ListBox in the OptionsAdd method
    of the OptionsAdd form.

    How do I make the ListBox accessible to all the functions in the OptionsAdd
    form?

    Thanks,


  • Steven Cheng[MSFT]

    #2
    RE: Object passing with Windows Forms

    Hi eXtreme,

    Welcome to MSDN newsgroup.
    As for the accessing controls from different form, and sharing data
    problem, I think we can use the following approachs to resolve it:

    1. For Data displaying / manipulating GUI problem, we're recommended to
    sparate the UI and the data. Typically, many ones are using the MVC
    design-pattern for such scenario:

    #Implementing MVC Design Pattern in .NET


    #UIPAB Part 2 : MVC, UIPAB Essentails and Demo


    For your scenario, I think we can define a certain Data container class
    which hold the underlying datas (which will be used to fill the controls on
    the form). This class has some member properties which hold the reference
    of the List, Array, DataSet/DataTable.....

    Then, in our Form/SubForm's constructor, we will pass an instance of this
    class. And when we need to displaying data on the form, we just retrieve
    the data from that member instance and bind data to the controls on the
    form. Also, when open a sub form, we provide the member property in the
    parent form in the construction of the sub form(sub form also has a certain
    field/propety to hold this reference). Thus, when user take action on the
    subform to modify data, we just update that object instance in subform's
    code and the parent form can directly retrieve the updated fresh data from
    the same instance. How do you think of this?

    If there're anything unclear, please feel free to post here. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)





    Comment

    • Steven Cheng[MSFT]

      #3
      RE: Object passing with Windows Forms

      Hi eXtreme,

      Have you got any further progress on resolving the problem or does the
      suggestions in my last reply helped a little?
      If there're any thing else we can help, please feel free to post here.

      Thanks,

      Steven Cheng
      Microsoft Online Support

      Get Secure! www.microsoft.com/security
      (This posting is provided "AS IS", with no warranties, and confers no
      rights.)

      Comment

      Working...