Click here to Skip to main content
15,885,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to pass values from a form to user control :)
Posted
Updated 10-Dec-09 19:44pm
v2

You can either declare Public Property in User control

CSS
Public string MyProperty {get;set;}


and access it like

CSS
myUserControl.MyPorperty="something";

where myUserControl is instance of you User Control

or if it is just for once

declare it in your constructor like

CSS
public partial class MyUserControl : UserControl
{
     public MyUserControl(string myString)
     {
         //use myString
     }
}


and initialise you User Control like

CSS
MyUserControl myUserControl = new MyUserControl("someting");
 
Share this answer
 
v2
Comments
CHill60 5-Jan-15 3:02am    
I really hope the OP hasn't been waiting for this solution for the last 5 years!
er_ashu 14-Jan-15 1:23am    
hope the answer may help others.
Where do I put myControl.MyProperty = "string Value"; ? What "parent" are you refering to?
 
Share this answer
 
Create a property inside your user control, and assign this value from the parent form.
for example
inside the control
public string MyProprty {get;set;}

inside the parent 
myControl.MyProperty = "string Value";
 
Share this answer
 
Comments
Zj Pineda 16-May-14 3:43am    
i did this to show if it is working,
UserControl1 usr = new UserControl1();
usr.MyProprty = "Boom";
MessageBox.Show(usr.MyProprty + "");
yea it's working.
but if i add a messagebox inside my usercontrol .
like this.
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(MyProprty+"");
}
popping up an empty box -_- idk what to do.
Define public properties for the user control. Then the form can pass values by setting these properties.
 
Share this answer
 
Comments
Member 13796203 30-Apr-18 9:17am    
Please can you report a small example? thank you!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900