简体   繁体   English

来自winform应用程序的C#如何打开另一个winform应用程序并向它发送值?

[英]C# from winform application how to open another winform application and send value to it?

I have 2 winform applications, say form1 and form2 .我有 2 个 winform 应用程序,比如form1form2

They are programmed through separate visual studio programs它们通过单独的视觉工作室程序进行编程

form1 is programmed in WindowsFormsApplication1.sln form1在 WindowsFormsApplication1.sln 中编程

form2 is programmed in WindowsFormsApplication2.sln form2在 WindowsFormsApplication2.sln 中编程

I want to open form2 (=WindowsFormApplication2.exe) by clicking a button in form1我想通过单击form1的按钮打开form2 (=WindowsFormApplication2.exe)

I created a method in WindowsFormApplication1.sln我在 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此方法打开 WindowsFormApplication2.exe

Then I need WindowsFormApplication2 MessageBox show the value got from WindowsFormApplication1.exe.然后我需要 WindowsFormApplication2 MessageBox 显示从 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如果我想将一个值从form1传递到同一个 .sln 中的form2 (也就是说,WindowsFormApplication1.sln 有 form1 和 form2),这很容易

I could just use我可以用

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

Constructor Form2 :构造函数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. C# 程序有一个static void Main()方法,它是应用程序的入口点。 You should see something like this in your Program.cs file in your winform2 project:您应该在 winform2 项目的Program.cs文件中看到类似的内容:

/// <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.如果你想让你的 winform2 应用程序接受命令行参数,你可以从这个方法中捕获它们。 But first you need to modify the method signature to take in a string[] of arguments:但首先你需要修改方法签名以接收一个string[]参数:

// 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.现在,传递给此应用程序的任何命令行参数都将位于args数组中。

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()) .接下来,我们需要修改 winform2 应用程序的主窗体构造函数以接收一个或多个字符串参数,以便我们可以在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):例如,您可以修改表单的构造函数以接收字符串(我以Form1为例):

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.在这种情况下,我期待这样的字符串: /Title:"Some Form Title" ,因此我将查看args数组并尝试找到匹配项。

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.现在您可以从命令行启动您的 WinForm 应用程序并传入一个字符串,该字符串将成为标题。 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.:在这里,我从命令行运行.exe并传递/Title:"Custom Title From Command Line" ,但如果您以编程方式启动应用程序,则可以将此字符串分配给ProcessStartInfo实例的Arguments属性。:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM