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
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.:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.