Click here to Skip to main content
12,078,489 members (40,707 online)
Rate this:
 
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 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 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.
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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160212.1 | Last Updated 5 Jan 2015
Copyright © CodeProject, 1999-2016
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