Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB
How to pass values from a form to user control Smile | :)
Posted 10-Dec-09 20:43pm
Edited 10-Dec-09 20:44pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Define public properties for the user control. Then the form can pass values by setting these properties.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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";
  Permalink  
Comments
Member 9950702 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You can either declare Public Property in User control
 
Public string MyProperty {get;set;}
 
and access it like
 
myUserControl.MyPorperty="something";
where myUserControl is instance of you User Control
 
or if it is just for once
 
declare it in your constructor like
 
public partial class MyUserControl : UserControl
{
     public MyUserControl(string myString)
     {
         //use myString
     }
}
 
and initialise you User Control like
 
MyUserControl myUserControl = new MyUserControl("someting");
  Permalink  
v2
Comments
CHill60 at 5-Jan-15 3:02am
   
I really hope the OP hasn't been waiting for this solution for the last 5 years!
er_ashu at 14-Jan-15 1:23am
   
hope the answer may help others.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Where do I put myControl.MyProperty = "string Value"; ? What "parent" are you refering to?
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 512
1 Maciej Los 445
2 Sergey Alexandrovich Kryukov 421
3 CPallini 200
4 _Asif_ 135
0 Sergey Alexandrovich Kryukov 6,813
1 OriginalGriff 5,342
2 Peter Leow 3,837
3 Mika Wendelius 2,850
4 CHill60 2,748


Advertise | Privacy | Mobile
Web02 | 2.8.150224.1 | Last Updated 5 Jan 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100