Search code examples
c#visual-studiomdi

Passing Value Between Two Project


I have two project and having trouble on passing value between two project. As usual I have passed the file reference between project.

My project Details Is:

Project1                Project2
All forms and object    Only 1 MDI Forms Containing ManuStrip

I wants to read the data of MDI Forms on showing the project1 forms The Example is as below:

//This is on Project2 MDI Forms

    private void accountMasterToolStripMenuItem_Click(object sender, EventArgs e)
    {
        INVOICE1.Form24 f24 = new INVOICE1.Form24();
        f24.PFrom.Text = label4.Text;
        f24.PTo.Text = label5.Text;
        f24.Namee.Text = textBox1.Text;
        f24.ID.Text = label6.Text;
        f24.ShowDialog();



    }

I Have Created the Properties for the same on project1 forms

   public Label PFrom
    {
        get { return label14; }
        set { label14 = value; }

    }
    public Label PTo
    {
        get { return label16; }
        set { label16 = value; }

    }
    public Label Namee
    {
        get { return label2; }
        set { label2 = value; }

    }
    public Label ID
    {
        get { return label3; }
        set { label3 = value; }

    }

The value passed from MDI To Project1 is not showing on Form24 of Project1. There are no error. The Form24 Showing without value which are passed from MDI Form.

Why The value not showing on form24 of project1 ?. And What is the Solution?.


Solution

  • You may have forgotten to add a project reference to Project1 in Project2. In the Solution Explorer, right-click Project2 and select "Add Reference", then under "Projects" select Project1.

    Also, if the two projects have different namespaces, you'll need to put

    using Project1;  // replace "Project1" with the namespace of your Project1
    

    At the top of the Project2 source file.


    If there are no compiler errors then the problem is not likely to be with project references. Perhaps you have some code in the Form24 constructor or Load event which is clearing those labels


    As a side note, instead of exposing the Labels as properties, just expose their Text property:

    public string PFrom       
    {           
        get { return label14.Text; }           
        set { label14.Text = value; }          
    }