-2

I have 2 winform applications, say form1 and form2.

They are programmed through separate visual studio programs

form1 is programmed in WindowsFormsApplication1.sln

form2 is programmed in WindowsFormsApplication2.sln

I want to open form2(=WindowsFormApplication2.exe) by clicking a button in form1

I created a method in WindowsFormApplication1.sln

private void button3_Click(object sender, EventArgs e)    
{
     var p = new Process();
     p.StartInfo.FileName ="WindowsFormsApplication2.exe";
     p.StartInfo.Arguments = "10";
     p.Start();
}

This method opens WindowsFormApplication2.exe

Then I need WindowsFormApplication2 MessageBox show the value got from WindowsFormApplication1.exe. Is this clear?

This should be clear... I cannot explain more simply than this


What other people have answered through comment or answerbox, is not what I want

If I want to pass a value from form1 to form2 that are in same .sln (That is, WindowsFormApplication1.sln has form1 and form2), this is so easy

I could just use

Form2 form2 = new Form2(textBox1.Text);    
form2.Show();

Constructor Form2:

public Form2(string smth)    
{
     InitializeComponent();
     label1.Text = smth;
}

But this is not what I want

I think everything is clear. Please tell me how to solve the problem

biglol
  • 36
  • 3

1 Answers1

3

C# programs have a static void Main() method that is the entry point for the application. You should see something like this in your Program.cs file in your winform2 project:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

If you want to enable your winform2 application to accept command-line arguments, you capture them from this method. But first you need to modify the method signature to take in a string[] of arguments:

// Add a string[] argument to the Main entry point method
static void Main(string[] args)

Now any command line arguments passed to this application will be in the args array.

Next, we need to modify your winform2 application's main form constructor to take in one or more string arguments, so we can pass them in the line that says Application.Run(new Form1()).

For example, you can modify your form's constructor to take in a string (I'm using Form1 as an example):

public partial class Form1 : Form
{
    // Add a string argument to the form's constructor. 
    // If it's not empty, we'll use it for the form's title.
    public Form1(string input)
    {
        InitializeComponent();

        if (!string.IsNullOrEmpty(input)) this.Text = input;
     }
}

After we enable the form to take a string input, we now modify the call to the constructor to pass a string to our form. In this case, I'm expecting a string like: /Title:"Some Form Title", so I'll look at the args array and try to find a match.

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    // Try to find the 'Title' argument
    var titleArg = args?.FirstOrDefault(arg => arg.StartsWith("/Title:", 
        StringComparison.OrdinalIgnoreCase));

    // Now strip off the beginning part of the argument
    titleArg = titleArg?.Replace("/Title:", "");

    // And now we can pass this to our form constructor
    Application.Run(new Form1(titleArg));
}

Now you can launch your WinForm application from the command line and pass in a string, which will become the title. Here I'm running the .exe from the command line and passing /Title:"Custom Title From Command Line", but you could just assign this string to the Arguments property of your ProcessStartInfo instance if you're launching the application programmatically.:

enter image description here

Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • I've edited my question could you please look at it? – biglol Feb 01 '19 at 01:33
  • I don't see how your edit pertains to this answer. I've shown how to create a winforms application that accepts (and processes) arguments passed to it from the command line. As mentioned in the answer, these arguments can be passed via the `ProcessStartInfo.Arguments` property when launching the application from your other project. Please let me know what part isn't making sense and I will try to improve the answer. – Rufus L Feb 01 '19 at 02:13
  • 1
    If you launched my app (`WinFormsTest.exe`) using the first code sample in your question, the title of the form would be `10`. Is this clear? This should be clear... I cannot explain more simply than this. ;) – Rufus L Feb 01 '19 at 02:19
  • So in the application you modify the entry point in Program.cs (to give the .exe the ability to read the command line), and you modify the Form1.cs (to give the form the ability to receive input). I used the constructor, but you could expose public properties or methods instead. Technically you don't have to do this part either. In the `Main` method I could have set the `Text` property of the form directly on an instance of the class and then passed that instance to the `Run` command:`var form1 = new Form1 { Text = titleArg }; Application.Run(form1);` – Rufus L Feb 01 '19 at 02:33
  • The key is to understand that the main form's constructor code is **NOT** the first code to run in your `.exe`. It's always `Main()` (which almost immediately launches your startup form). Also notice the background window in my image. You'll see that the application was launched from the command line with a parameter, which became the form title – Rufus L Feb 01 '19 at 02:44