Get access to parent control from user control - C#
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 yourUserControl
- set the property in your Form
- assuming your
ListBox
is namedlistBox1
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");
}
}
How to access Parent class function/control from a child user control loaded in a pannel
This may helps:
public partial class Form1 : Form
{
//Other codes
private void loadlogin()
{
login log = new login(this); //CHANGE HERE
mainPannel.Controls.Clear();
mainPannel.Controls.Add(log);
}
//Other codes
}
And
public partial class login : UserControl
{
Form1 _parent; //ADD THIS
public login(Form1 parent)
{
InitializeComponent();
this._parent = parent; //ADD THIS
}
public void button1_Click_1(object sender, EventArgs e)
{
this._parent.mytest(); //CALL WHAT YOU WANT
}
}
Access user control elements from parent
well, here is the problem, you are using multiple instances of same form.
you should rather create a single instance and use it .
public partial class Program: DevExpress.XtraBars.Ribbon.RibbonForm {
UserControl Campus = null;
public Program()
{
InitializeComponent();
Campus = new WindowsFormsApplication1.Campus();
}
private void Program_Load(object sender, EventArgs e) {
// remove this object creation
//var Campus = new WindowsFormsApplication1.Campus();
pnlPanel.Panel2.Controls.Add(Campus);
...
}
public void Reset() {
// remove this object creation
// var Campus = new WindowsFormsApplication1.Campus();
}
private void chkDisplay_EditValueChanged(object sender, EventArgs e) {
string displayInfo = "";
// remove this object creation
// var Campus = new WindowsFormsApplication1.Campus();
}
private void ribbonControl1_MouseDown(object sender, MouseEventArgs e)
{
...
if(hitInfo.Page.Name == "Campus")
{
// remove this object creation
//var Campus = new WindowsFormsApplication1.Campus();
}
...
}
}
Accessing object in parent user control from child user control
Use the below code to access the hidden field from the child control. this.Parent
will give the parent control & use FindControl
to find the control by ID.
HiddenField hfID = this.Parent.FindControl("hfId") as HiddenField;
string hiddenvalue = hfID.Value;
If you change the value of the hidden field on page load, then on button click, the updated value gets reflected.
How to Access Parent Window's Control from user control window
I found the answer finally
Window parentWindow = Window.GetWindow(this);
object obj= parentWindow.FindName("Popup1");
System.Windows.Controls.Primitives.Popup pop = (System.Windows.Controls.Primitives.Popup)obj;
pop.IsOpen = false;
Related Topics
Using String as a Lock to Do Thread Synchronization
Why Does My .Net Application Crash When Run from a Network Drive
Linq - What Is the Quickest Way to Find Out Deferred Execution or Not
How to Programmatically Create, Read, Write an Excel Without Having Office Installed
How to Cast a Generic Enum to Int
Monitor Process Start in the System
Unity Eventmanager with Delegate Instead of Unityevent
Openxml Sdk Having Borders for Cell
Performing a Mouse Click Without Moving Cursor
How to Get Character for a Given Ascii Value
How to Log All Thrown Exceptions
.Net: Unable to Cast Object to Interface It Implements
C# Structs: Unassigned Local Variable
How to Get Current Regional Settings in C#
How to Connect to a Ms Access File (Mdb) Using C#