none
Working on 2 projects under single solution RRS feed

  • Question

  • Hello, I am working on 2 project under single solution in C# desktop application. Here I want to update a Label on form1 of project1 which is dependent on project2 string variable (a calculation is happened on project2). Note that this is all required on runtime.

    I have tried so many techniques; I have made a static variable on project1 and send it to project2 i.e., Process.Start("Project2.exe", textbox1.Text) for calculation and on button click from project2 a static method is called which is made on project1 for what I want to update a label on form, but because of static method label is not accessible there.


    Wednesday, September 26, 2018 11:43 AM

All replies

  • What kind of project is P2? A class library?

    More context would be great.

    Wednesday, September 26, 2018 12:01 PM
  • both are the projects which contains forms.
    Wednesday, September 26, 2018 12:13 PM
  • Hello,

    See the following on anonymous pipes.

    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, September 26, 2018 1:43 PM
    Moderator
  • >I have tried so many techniques

    Simply add reference to the project 2 in project 1.

    a static method is called

    In this situation you need to organize access to the form and to the label on the form. Simplest way - have a static reference to the form.

    But, by my opinion, you doing something wrong...


    Sincerely, Highly skilled coding monkey.

    Wednesday, September 26, 2018 1:51 PM
  • > both are the projects which contains forms.

    Stefan ask about what project have as result - *.EXE or *.DLL? Difference is - first is self-executable file and second need some another executable to make it work.

    Is there form or not - does not matter.


    Sincerely, Highly skilled coding monkey.

    Wednesday, September 26, 2018 1:55 PM
  • This was not the question..

    But in this case:

    You need three projects. Your two application (UI) projects and one class library. Place the common (calculation code) in the class library. Then reference the class library in both UI projects and use the common code.

    Wednesday, September 26, 2018 1:56 PM
  • hi, you can use shared memory or send a message from module 1 to module 2 using standard WinAPI functions ... no problem if one module is DLL and second one is EXE.
    Wednesday, September 26, 2018 4:02 PM
  • Thanks for the reply Stefan, but the communication between the projects is easily done using static variables, but the question is how could I update a label or textbox on runtime where both project1 and project2 is on running condition. Actually, I want to change the text of label or textbox of project1 using the value of project2.

    Thursday, September 27, 2018 5:16 AM
  • Both have there executable files Andrey under single solution.
    Thursday, September 27, 2018 5:17 AM
  • I had made a static variable in project1 and on the run of project2 it calls there and calculation happened there after that I want to update to update text of label or textbox.

    Is there any option to assign static variable to non-static label or textbox? I know this is creepy

    Thursday, September 27, 2018 5:23 AM
  • Greetings Naveed.

    So the two projects are executables, running at the same time, correct?

    There's probably a proper robust way of doing this using remoting or a service or something, but there is also a quick and dirty fix that a lazy programmer like myself would use.

    In the project sending the information, write the text to a file. In the project receiving the information, use a timer to look for the file every few seconds (say five seconds, or whatever) and read it if it exists then delete the file.

    Thursday, September 27, 2018 5:31 AM
  • Greetings again Naveed.

    There should be nothing stopping you assigning a static variable to a non-static label. Can you show us some code that illustrates the problem and tell us the exact error you are getting?

    Thursday, September 27, 2018 5:44 AM
  • Hi M Naveed Qamar,

    I would suggest that project2 could save the string variable into a text file after it changed. set a timer control in project form, which read the text file and modify the label's text.

    Best regards,

    Zhanglong


    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, September 27, 2018 7:41 AM
    Moderator
  • When it is easily done, what is your problem?

    Just use an observable.

    BUT: using (global) static variables for I passing values is poor style. And it is hard to test, cause it is a global state.

    Thursday, September 27, 2018 8:49 AM
  • >Both have there executable files

    Move common/shared parts into third project, DLL type,


    Sincerely, Highly skilled coding monkey.

    Thursday, September 27, 2018 9:04 AM
  • > how could I update a label or textbox on runtime where both project1 and project2 is on running condition.

    There was mentioned PIPES between process. 

    But I would say again - if it required there are a problem with task definition or code organisation,


    Sincerely, Highly skilled coding monkey.

    Thursday, September 27, 2018 9:06 AM
  • namespace Poject1
    {
        public partial class Form1: Telerik.WinControls.UI.RadForm
        {
    
            public static string Points="";
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
              Process.Start("Project2.exe");
            }
    
            public static void CallProject1()
            {
              Label1.Text=Points; // this line alert error "Object reference not set to the instance of an object" when I had made Label1 public static
            }
        }
    }
    
    using Project1;
    namespace Poject2
    {
        public partial class Form2: Telerik.WinControls.UI.RadForm
        {
            string AddPoints="2";
            public Form2()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
             Form1.Points=this.AddPoints;
             Form1.CallProject1();
            }
        }
    }

    Above was just a demonstration of what I really want.


    Thursday, September 27, 2018 10:53 AM
  • Instead of 

            private void button1_Click(object sender, EventArgs e)
            {
              Process.Start("Project2.exe");
            }

    use 

            private void button1_Click(object sender, EventArgs e)
            {
              Project2.Form2 form2 = new Project2.Form2;
              form2.Form1 = this;
              form2.Show();
            }
    
    public void UpdateLabel(string text)
    {
      mylabel.Text = text;
    }

    in form 2:

            Form1 Form1 {get; set; }
    
            private void button1_Click(object sender, EventArgs e)
            {
             Form1.UpdateLabel("My texte for label");
            }


    Sincerely, Highly skilled coding monkey.

    Thursday, September 27, 2018 11:02 AM
  • No, two form projects do not work in the manner you are trying to use them. You can have two or more Windows form projects in a .NET solution, but they are independent of each other as individual exe(s) that would be generated.

    You can have more than one form in a .NET Windows forms based solution and the forms within the same Windows form project can communicate with each other. 

    But no, what you are thinking will not work.

     
    Thursday, September 27, 2018 11:06 AM
  • Why starting the second project as process?

    private void button1_Click(object sender, EventArgs e)
    {
        Process.Start("Project2.exe");
    }
    

    When it must be a separate process, then you need inter-process communication (IPC). This can be (named) pipes, shared memory or TCP/IP. Even Windows messaging.

    When it must not be a separate process, then you can do what Andrey already wrote.

    It's time that you explain in detail, why you want to exchange what data on what purpose.

    Thursday, September 27, 2018 11:50 AM