none
How to pass values between UserControls?? C# Winform RRS feed

  • Question

  • So i've a Form1 with three UserControl's as UC1,UC2 & UC3. Form1 contains a Button, UC1 and UC3. Both UC1 & UC3 are initially hidden when the application runs and the Form1 button click event makes the UC1 visible. UC1 contains a panel and inside the UC1 load event I'm adding UC2 into the panel through panel.Controls.Add() method at application run time. UC2 contains a Button and the click event of this button makes UC3 visible in Form1. UC3 contain a textbox. Now when I click the UC2 Button, I want it to pass a string value to UC3 textbox. How do I do that? or generally I need a simple solution to send values from one UserControl to another UserControl.

    Here is all my code...

    Form1.cs

    namespace Problem
    {
       public partial class Form1 : Form
      {
    
         public Form1()
         {
            InitializeComponent();
            uC11.u3Visible += UC11_u3Visible;// Event came from UC1
         }
    
         private void UC11_u3Visible(object sender, EventArgs e)
         {
            uC31.Visible = true;
            uC11.Visible = false;
         }
    
         private void button1_Click(object sender, EventArgs e)
         {
            uC11.Visible = true;
         }
      }
    }

    UC1.cs

    namespace Problem
    {
      public partial class UC1 : UserControl
      {
          public event EventHandler u3Visible;//Declaring a public event
          public UC1()
          {
             InitializeComponent();
          }
    
          private void UC1_Load(object sender, EventArgs e)
          {
              UC2 xy = new UC2();
              xy.Name = "UsrCntrl2";
              xy.Location = new Point(18, panel1.Controls.Count * 73);
              panel1.Controls.Add(xy);// Adding UC2 in UC1
              xy.open += Xy_open;// UC2 button click event
          }
    
          private void Xy_open(object sender, EventArgs e)
          {
              EventHandler handler = u3Visible;
              if (handler != null)
              {
                  handler(this, new EventArgs());// Creating another event to make UC3 visible 
                  //upon button click from UC2.
              }
          }
       }
    }

    UC2.cs

    namespace Problem
    {
       public partial class UC2 : UserControl
     {
    
        public event EventHandler open;//Declaring a public event
        public UC2()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            EventHandler handler = open;
            if (handler != null)
            {
                handler(this, new EventArgs());// Button click sending this event
            }
            string text = "Hello There!";// "I want this string to pass into UC3 textbox"
            var ass = new UC3(text);
    
    
        }
      }
    }

    UC3.cs

    namespace Problem
    {
       public partial class UC3 : UserControl
    {
        public UC3()
        {
            InitializeComponent();
        }
        public UC3(string text)
        {
            InitializeComponent();
            TB.Text = text;// why textbox not showing this value?
            MessageBox.Show(text);// while this MessageBox showing it!
        }
      }
    }

    Any help will be greatly appreciated. Thanks in advance!



    Wednesday, November 6, 2019 2:35 AM

Answers

  • Hi Adnan Maruf,
    >> var ass = new UC3(text);

    This statement will create a new UC3 UserControl which different from the one you created before, so textbox doesn't show the value.
    MessageBox is a pop-up window, so it can show the value.
    To handle the issue, you can follow the steps.
    1. In Form1.cs, define a property UC3 to get an instance of the uC31 which in Form1 you created before.

        public UC3 uc3
        {
                get { return uC31; }
        }

    2. In UC2.cs, use the property Application.OpenForms to get an instance of the form1.

        private void Button1_Click(object sender, EventArgs e)
        {
                EventHandler handler = open;
                if (handler != null)
                {
                    handler(this, e);// Button click sending this event
                }
                string text = "Hello There!";// "I want this string to pass into UC3 textbox"
                Form1 form1 = (Form1)Application.OpenForms["Form1"];
                form1.uc3.changetext(text);
         }

    3. In UC3.cs, define a method changetext.

        public void changetext(string str)
        {
                textBox1.Text = str;
        }

    Hope this can help you.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.





    Wednesday, November 6, 2019 9:22 AM

All replies

  • Hi Adnan Maruf,
    >> var ass = new UC3(text);

    This statement will create a new UC3 UserControl which different from the one you created before, so textbox doesn't show the value.
    MessageBox is a pop-up window, so it can show the value.
    To handle the issue, you can follow the steps.
    1. In Form1.cs, define a property UC3 to get an instance of the uC31 which in Form1 you created before.

        public UC3 uc3
        {
                get { return uC31; }
        }

    2. In UC2.cs, use the property Application.OpenForms to get an instance of the form1.

        private void Button1_Click(object sender, EventArgs e)
        {
                EventHandler handler = open;
                if (handler != null)
                {
                    handler(this, e);// Button click sending this event
                }
                string text = "Hello There!";// "I want this string to pass into UC3 textbox"
                Form1 form1 = (Form1)Application.OpenForms["Form1"];
                form1.uc3.changetext(text);
         }

    3. In UC3.cs, define a method changetext.

        public void changetext(string str)
        {
                textBox1.Text = str;
        }

    Hope this can help you.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.





    Wednesday, November 6, 2019 9:22 AM
  • Hi Daniel Zhang, I can't thank you enough Sir, it worked as I was expecting. I really appreciate your help. Thank you so much :) ! Just a small thing, I think in your (Step3->) it's a typo but you actually meant UC3.cs and not UC2.cs.
    • Edited by Adnan Maruf Wednesday, November 6, 2019 5:17 PM
    Wednesday, November 6, 2019 5:13 PM
  • Hi Daniel Zhang, I can't thank you enough Sir, it worked as I was expecting. I really appreciate your help. Thank you so much :) ! Just a small thing, I think in your (Step3->) it's a typo but you actually meant UC3.cs and not UC2.cs.
    Thank you for pointing out.

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, November 7, 2019 1:18 AM