Click here to Skip to main content
15,867,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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

C#
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

C#
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

C#
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!
    }
  }
}


What I have tried:

I'm stuck with this problem for so many hours now. I have tried googling to finds related solutions but nothing that much helpful came-up. So had to post it here. Any help will be greatly appreciated. Thanks in advance!
Posted
Updated 5-Nov-19 19:35pm
Comments
F-ES Sitecore 6-Nov-19 4:43am    
Generally speaking, you don't. Components should be self-contained with no reliance or knowledge about about thing else. If you want one component to change something about itself based on the actions of another component then have the component that has changed raise an event like "XYZButtonClicked". The control that needs to update itself should provide a method that allows this like "UpdateABCText", along with any parameters it needs to update the component like new text etc. The parent form that the controls are on will subscribe to the XYZButtonClicked event provided by the first component and when it is received it will then call the UpdateABCText method of the second component. If data needs to be passed along then the component raising the event can pass that with the event it raises, the parent form reads that data and passes it as params to the method in the second control.

1 solution

Instead of Visual true / false from server side, do this from client side using css
 
Share this answer
 
Comments
F-ES Sitecore 6-Nov-19 4:38am    
I don't think this is a website
Adnan Maruf 6-Nov-19 12:30pm    
Thanks for your answer! but this a C# Winform application and not a webpage :)

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