locked
How to access controls on another form RRS feed

  • Question

  • Hi, I need to access a control that is on a separate form, I have 2 forms, my main form and the details form,so my main form calls my details form, but from my details form I need to access some of the information on the datagrid that is on the main form, I was thinking on using properties but there are too many fields that I need to access, so I was wondering if there is another way. Thanks.
    Friday, February 9, 2007 5:55 PM

Answers

  • When wanting to have one form make changes to another or it’s controls, it is possible to declare the target control(s) as public members instead of private ones... this however is generally considered bad practice as it violates the encapsulation concept of object oriented programming and instead the common suggestion is to manually create properties or methods on the parent form that provide access to the private control... in your case though that sounds like an undesirable option because of the amount of properties you may need.

    Try exposing the control as a public member, despite being poo-pooed it’s still perfectly legal and would give you the kind of access you are looking for.

    Friday, February 9, 2007 6:25 PM
  • Hi

    You can pass the MainForm reference to the DetailsForm ctor and also have a few public methods/properties that could get you want from the MainForm. For instance....

    class DetailsForm : Form
    {
        private MainForm _mainForm;
       
        public DetailsForm(MainForm mainForm)
        {
            _mainForm = mainForm;
        }
       
        // When you want something from the MainForm...
        // In MainForm: object GetValueFromGrid(int row, int col);
        object o = _mainForm.GetValueFromGrid(row, col);
       
        // replace object with whatever you want it to be returned.   
    }

     

    There is an advantage in doing that way. It keeps the functionality of the DetailsForm independent of the UI control that you are using. All you want is the data, just have a method convenient enough to provide that. Also MainForm does not need to very aware of who might need the data.

     

    Hope that helps !!!

    Regards

    Vivek Ragunathan

    Friday, February 9, 2007 7:53 PM

All replies

  • When wanting to have one form make changes to another or it’s controls, it is possible to declare the target control(s) as public members instead of private ones... this however is generally considered bad practice as it violates the encapsulation concept of object oriented programming and instead the common suggestion is to manually create properties or methods on the parent form that provide access to the private control... in your case though that sounds like an undesirable option because of the amount of properties you may need.

    Try exposing the control as a public member, despite being poo-pooed it’s still perfectly legal and would give you the kind of access you are looking for.

    Friday, February 9, 2007 6:25 PM
  • Hi

    You can pass the MainForm reference to the DetailsForm ctor and also have a few public methods/properties that could get you want from the MainForm. For instance....

    class DetailsForm : Form
    {
        private MainForm _mainForm;
       
        public DetailsForm(MainForm mainForm)
        {
            _mainForm = mainForm;
        }
       
        // When you want something from the MainForm...
        // In MainForm: object GetValueFromGrid(int row, int col);
        object o = _mainForm.GetValueFromGrid(row, col);
       
        // replace object with whatever you want it to be returned.   
    }

     

    There is an advantage in doing that way. It keeps the functionality of the DetailsForm independent of the UI control that you are using. All you want is the data, just have a method convenient enough to provide that. Also MainForm does not need to very aware of who might need the data.

     

    Hope that helps !!!

    Regards

    Vivek Ragunathan

    Friday, February 9, 2007 7:53 PM
  • thank you both for the useful information, I'll use the method Vivek suggested. I didn't know it was bad practice though.
    Friday, February 9, 2007 9:08 PM
  •  

    What I would suggest is that on the form that has the datagrid create a public property that has only the get method and returns the grid.

    Lets say if you have used a DataGridView with the name dtgData, the property would look something like this:

    public DataGridView DtgData {

         get { return dtgData; }

    }

     

    I hope this is of help as this does not violate any oops rules.

    Regards

    Saturday, February 10, 2007 9:53 AM