Get access to parent control from user control - C#

后端 未结 9 1799
甜味超标
甜味超标 2020-11-29 06:42

How do I get access to the parent controls of user control in C# (winform). I am using the following code but it is not applicable on all types controls such as ListBox.

相关标签:
9条回答
  • 2020-11-29 06:50

    According to Ruskins answer and the comments here I came up with the following (recursive) solution:

    public static T GetParentOfType<T>(this Control control) where T : class
    {
        if (control?.Parent == null)
            return null;
    
        if (control.Parent is T parent)
            return parent;
    
        return GetParentOfType<T>(control.Parent);
    }
    
    0 讨论(0)
  • 2020-11-29 07:00

    You can use Control.Parent to get the parent of the control or Control.FindForm to get the first parent Form the control is on. There is a difference between the two in terms of finding forms, so one may be more suitable to use than the other.:

    The control's Parent property value might not be the same as the Form returned by FindForm method. For example, if a RadioButton control is contained within a GroupBox control, and the GroupBox is on a Form, the RadioButton control's Parent is the GroupBox and the GroupBox control's Parent is the Form.

    0 讨论(0)
  • 2020-11-29 07:01

    You can get the Parent of a control via

    myControl.Parent
    

    See MSDN: Control.Parent

    0 讨论(0)
  • 2020-11-29 07:02

    Control has a property called Parent, which will give the parent control. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.parent.aspx

    eg Control p = this.Parent;

    0 讨论(0)
  • 2020-11-29 07:03

    Description

    You can get the parent control using Control.Parent.

    Sample

    So if you have a Control placed on a form this.Parent would be your Form.

    Within your Control you can do

    Form parentForm = (this.Parent as Form);
    

    More Information

    • MSDN: Control.Parent Property

    Update after a comment by Farid-ur-Rahman (He was asking the question)

    My Control and a listbox (listBox1) both are place on a Form (Form1). I have to add item in a listBox1 when user press a button placed in my Control.

    You have two possible ways to get this done.

    1. Use `Control.Parent

    Sample

    MyUserControl

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.Parent == null || this.Parent.GetType() != typeof(MyForm))
                return;
    
            ListBox listBox = (this.Parent as MyForm).Controls["listBox1"] as ListBox;
            listBox.Items.Add("Test");
        }
    

    or

    2.

    • put a property public MyForm ParentForm { get; set; } to your UserControl
    • set the property in your Form
    • assuming your ListBox is named listBox1 otherwise change the name

    Sample

    MyForm

    public partial class MyForm : Form
    {
        public MyForm()
        {
            InitializeComponent();
            this.myUserControl1.ParentForm = this;
        }
    }
    

    MyUserControl

    public partial class MyUserControl : UserControl
    {
        public MyForm ParentForm { get; set; }
    
        public MyUserControl()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            if (ParentForm == null)
                return;
    
            ListBox listBox = (ParentForm.Controls["listBox1"] as ListBox);
            listBox.Items.Add("Test");
    
        }
    }
    
    0 讨论(0)
  • 2020-11-29 07:04

    If you want to get any parent by any child control you can use this code, and when you find the UserControl/Form/Panel or others you can call funnctions or set/get values:

    Control myControl= this;
    while (myControl.Parent != null)
    {
    
        if (myControl.Parent!=null)
        {
            myControl = myControl.Parent;
            if  (myControl.Name== "MyCustomUserControl")
            {
                ((MyCustomUserControl)myControl).lblTitle.Text = "FOUND IT";
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题