locked
Accessing Elements In A User Control When Included On Form RRS feed

  • Question

  • Hi,

    I have created a user control that I have added to a windows form, now once I add the user control I cannot access any elements like buttons, datagrid data sources etc - how do I go about doing this in my form to which I have added my control?

    Wednesday, July 20, 2016 9:27 AM

Answers

  • Hi ShatterStar,

    >>"now once I add the user control I cannot access any elements like buttons, datagrid data sources etc"

    After we add elements to UserControl, in UserControl.Designer.cs file we could find the elements defined in UserControl as private members. For example,

    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button1;

    Private members could not be access from other class. So we can't access user control elements from form. If you want to do it, you could create a public property which return the elements you want to access from outside in your user control.

    public Button ButtonWhichCouldAccessFromOutside
    {
        get
        {
            return this.button1;
        }
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 21, 2016 9:00 AM