I am trying to pull data from user control 1 and insert the data into a label on user control 2. I have been doing a lot of research on this including the link here: http://www.daniweb.com/software-development/csharp/code/217193/passing-data-between-forms but haven't found a suitible answer that will actually work. I am trying retain the original instance so that a new instance will not block my original user control instance. Is there anybody who might have clear inststructions on how to do this? Thanks in advance!

What version of VS? What version of .NET? Are both UserControls being used on the same form or different forms? Do you have some screen shots?

I'm confused by "I am trying retain the original instance so that a new instance will not block my original user control instance." Please provide more details.

Edited 2 Years Ago by cgeier

Try the following:

Create your user controls:

  • Click "File"
  • Select "New"
  • Select "Project"
  • Click "Visual C#"
  • Select "Windows"
  • Select "Windows Forms Control Library"
  • Change "Name" as desired (ex: "MyUserControls"
  • Click "OK"

Add second UserControl:

  • Click "Project"
  • Select "Add New Item"
  • Select "User Control"
  • Click "Add"

When following the tutorial

How to pass data between two forms in C#

do the following:

In "UserControl1.cs":

  • Add "ScheduleInfo.cs" code
  • Add "ValueUpdatedEventArgs.cs" code
  • Add "ChildFrm.cs" code to "UserControl1.cs"
  • Add any desired controls to "UserControl1"

UserControl2:

  • Add label named "label1"

In "UserControl2.cs" add the following code:

public void SetLabel1Text(string userValue)
{
    this.label1.Text = userValue;
}//SetLabel1Text

Build Windows Forms Control Library project (ex: "MyUserControls")

  • Click "Build"
  • Click "Build MyUserControls"

Create a Windows Forms Application project:

  • Click "File"
  • Select "New"
  • Select "Project"
  • Click "Visual C#"
  • Select "Windows"
  • Select "Windows Forms Application"
  • Change "Name" as desired (ex: "UserControlPassData")
  • Click "OK"

If desired, add second form:

  • Click "Project"
  • Select "Add New Item"
  • Select "Windows Form"
  • Click "Add"

Add user controls to Toolbox:

  • Click "View"
  • Click "ToolBox"
  • Right-click in ToolBox
  • Select "Choose Items..."
  • Click ".NET Framework Components"
  • Click "Browse" button
  • Choose your Windows Forms Control Library dll (ex: "MyUserControls.dll")
  • Click "Open"
  • Click "OK"

Add "UserControl1" to "Form1"

  • Click "View"
  • Click "ToolBox"
  • Click "UserControl1"
  • Click on "Form1" (to add control to Form1)

Add "UserControl2" using the same process as used to add "UserControl1".

Note: If adding UserControl2 to "Form2", then right-click on "UserControl2" and select "Properties". Change "Modifiers" for "UserControl2" from "Private" to "Public".

In "Form1.cs" we are going to use much of the code from the tutorial that is for "MainFrm.cs", with a few modifications. I will post the code below with the modifications. See the tutorial for more info about how it works.

Form1:

  • Add a button named "showFrm2Btn" to Form1
  • Double-click "showFrm2Btn" to create the click event

"Form1.cs":

public partial class Form1 : Form
{
    //create new instance of Form2
    private Form2 _myForm2 = new Form2();

    //ScheduleInfo is defined in our
    // Windows Forms Library "MyUserControls".
    //
    //create new instance of 
    //MyUserControls.ScheduleInfo

    private MyUserControls2.ScheduleInfo _schedule = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void userControl1_ValueUpdated(object sender, MyUserControls.EventArgsValueUpdated e)
    {
        //event handler
        //update data on this form with data from 

        //'e' is an instance of 'EventArgsValueUpdated'
        //and contains our data. To access the data,
        //use e.<variable name>

        //store received data in a variable in this form
        _schedule = new MyUserControls2.ScheduleInfo();

        _schedule = e.Schedule;

        //call "SetLabel1Text" in "userControl2"
        //to set/update the label text
        _myForm2.userControl2.SetLabel1Text(e.Schedule.Name);

    }//userControl1_ValueUpdated

    private void Form1_Load(object sender, EventArgs e)
    {
        //add event listener
        userControl1.ValueUpdated += new MyUserControls.EventHandlerValueUpdated(userControl1_ValueUpdated);
    }

    private void showFrm2Btn_Click(object sender, EventArgs e)
    {
        //show Form2
        _myForm2.Show();
    }
}

Note: The above code in "Form1.cs", assumes that "UserControl2" is on "Form2". If "UserControl2" is also on "Form1", then remove "showFrm2Btn_Click" and change "_myForm2.userControl2.SetLabel1Text(e.Schedule.Name)" to "userControl2.SetLabel1Text(e.Schedule.Name)" in "userControl1_ValueUpdated".

Edited 2 Years Ago by cgeier

Comments
For the good work! :)
This article has been dead for over six months. Start a new discussion instead.