Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have to pass string lines from form to usercontol in windows form and collect all the lines to plot graph in usercontrol.

What I have tried:

i have tried using public properties in user control but not working . here is the code i tried
in user control
public string stringA
{ get; set;}

in form control
usercontrol user = new usercontrol();
user.stringA = "HI";

I have cross checked in usercontrol whether the string is passed from form to usercontrol or not
textbox1.text = stringA;
showing empty
Posted
Updated 9-Oct-21 6:17am
Comments
Member 15021328 10-Oct-21 2:04am    
please show detailed code on both form and usercontrol to pass a string from form to usercontrol

To add to what Dave says, creating a new user control doesn't do anything useful on it's own either:
usercontrol user = new usercontrol();
user.stringA = "HI";
Unless that instance is added to the Forms Controls collection (or the Controls collection of a control on the form already) then it isn't displayed at all.
Probably what you need to do is access the control instance you already have on your form - just use it's name - and set it's property as Dave showed you.
 
Share this answer
 
Comments
Member 15021328 10-Oct-21 2:15am    
please show detailed code on both form and usercontrol to pass a string from form to usercontrol
OriginalGriff 10-Oct-21 2:38am    
You are kidding, right?
You can't write this on your own?

MyUserControlName.MyUserControlPropertyName = "A string";
The textbox thing isn't showing you the content of stringA because the textbox.Text is only set once.

In order for that trick to work, you would have to change your stringA property setting code to include setting the Text property of the TextBox:
C#
private string _stringA;
public string StringA
{
    get
    {
        return _stringA;
    }
    set
    {
        if (value != _stringA)
        {
            _stringA = value;
            textBox1.Text = _stringA;
        }
    }
}
 
Share this answer
 
Comments
Dave Kreskowiak 10-Oct-21 13:07pm    
I'm not here to write your code for you.

I've already shown you how to implement a property your UserControl, and you already know how to set a property on a control. The rest it up to your implementation of the control itself and what you want to happen when setting that property, which I already gave you an example of.

The problem is not really the code so much as your understanding of how UserControls work, what properties are used for, and how to use them.

Controls take planning to work properly. The only way you're going to learn how to do that is to try, fail, and learn your mistakes, try, fail, and learn from your mistakes, try, fail, learn...

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