Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Form1. And a UserControl1.
in Form1 I click a Label and make UserControl1 show.
In UserControl1 I update a public string value.
Now I need that value to be on the Label in Form1.

What is the best approach?

What I have tried:

using only c# default implementations.
Posted
Updated 12-Feb-17 15:51pm
v2
Comments
[no name] 11-Feb-17 21:17pm    
yourLabel.Text = yourControl.StringProperty


Vague questions get vague answers.
Karthik_Mahalingam 12-Feb-17 1:43am    
on button click?

See here: Transferring information between two forms, Part 2: Child to Parent[^] - it's written about forms, but a Parent Form and a Child UserControl is exactly the same process, and even code.
 
Share this answer
 
Comments
_Q12_ 12-Feb-17 21:52pm    
Thank you mister Originalgriff.
Here is my approach:
More logical and more simpler I think.
Just hide and unhide the usercontrol, and transfer data between through public variables.
//UserControl1
 public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        //Close the UserControl from Form1
        private void label1_Click(object sender, EventArgs e)
        {
            this.Visible = false; 
        }


        public string data = ""; 
        //crystal
        private void button1_Click(object sender, EventArgs e)
        {
            data = "crystal";
            Close();
        }
        //gas
        private void button2_Click(object sender, EventArgs e)
        {
            data = "gas";
            Close();
        }
        //cell
        private void button3_Click(object sender, EventArgs e)
        {
            data = "cell";
            Close();
        }

//--------------------------------------------------------------------------------

//Form1

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            controlClicked = "pictureBox1";
            userControl11.Visible = true;
        }

        
        string mainData = "";
        string controlClicked = "";
        private void userControl11_VisibleChanged(object sender, EventArgs e)
        {
            if (userControl11.Visible == false )
            {
                if (userControl11.data == "crystal")
                {
                    mainData = "crystal";
                    if (controlClicked == "pictureBox1")
                    {
                        pictureBox1.BackgroundImage = Image.FromFile("1.png");
                    }
                }
                if (userControl11.data == "gas")
                {
                    mainData = "gas";
                }
                if (userControl11.data == "cell")
                {
                    mainData = "cell";
                }

                //-------------------------
            }
        }
 
Share this answer
 

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