C#从winform应用程序如何打开另一个winform应用程序并向其发送值?
我有两个winform应用程序,比如C#从winform应用程序如何打开另一个winform应用程序并向其发送值?,c#,winforms,parameters,C#,Winforms,Parameters,我有两个winform应用程序,比如form1和form2 它们通过单独的visual studio程序进行编程 form1在WindowsFormsApplication1.sln中编程 private void button3_Click(object sender, EventArgs e) { var p = new Process(); p.StartInfo.FileName ="WindowsFormsApplication2.exe"; p.
form1
和form2
它们通过单独的visual studio程序进行编程
form1
在WindowsFormsApplication1.sln中编程
private void button3_Click(object sender, EventArgs e)
{
var p = new Process();
p.StartInfo.FileName ="WindowsFormsApplication2.exe";
p.StartInfo.Arguments = "10";
p.Start();
}
form2
在WindowsFormsApplication2.sln中编程
我想通过单击form1
中的按钮打开form2
(=WindowsFormApplication2.exe)
我在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();
}
此方法打开WindowsFormApplication2.exe
然后我需要WindowsFormApplication2消息框显示从WindowsFormApplication1.exe获取的值。清楚了吗
这应该很清楚。。。我解释得再简单不过了
其他人通过评论或回答框回答的不是我想要的 如果我想将一个值从
form1
传递到form2
,它们位于相同的.sln中(也就是说,WindowsFormApplication1.sln有form1和form2),这非常简单
我可以用
Form2 form2 = new Form2(textBox1.Text);
form2.Show();
建造商表格2
:
public Form2(string smth)
{
InitializeComponent();
label1.Text = smth;
}
但这不是我想要的
我想一切都很清楚。请告诉我如何解决这个问题C#程序有一个static void Main()
方法,它是应用程序的入口点。您应该在winform2项目的程序.cs
文件中看到类似的内容:
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
现在,传递到此应用程序的任何命令行参数都将位于args
数组中
接下来,我们需要修改winform2应用程序的主窗体构造函数,以接受一个或多个字符串参数,这样我们就可以在写有application.Run(new Form1())
的行中传递它们
例如,您可以修改表单的构造函数以接受字符串(我使用Form1
作为示例):
在启用表单接受字符串输入后,现在修改对构造函数的调用,以将字符串传递给表单。在本例中,我需要一个字符串,如:/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));
}
现在,您可以从命令行启动WinForm应用程序并传入一个字符串,该字符串将成为标题。在这里,我从命令行运行.exe
,并传递/Title:“来自命令行的自定义标题”
,但如果您以编程方式启动应用程序,则可以将此字符串分配给ProcessStartInfo
实例的Arguments
属性
“但我无法在winform2中获取此值”。您是否尝试查看winform2输入方法(
Main
)中的args
值?@John我想显示从winform1传递到winform2 MessageBox的值。Show@Grant我编辑了我的问题,你能看一下吗?我编辑了我的问题,你能看一下吗?我看不出你的编辑与这个答案有什么关系。我已经展示了如何创建一个winforms应用程序,它接受(并处理)从命令行传递给它的参数。如回答中所述,从其他项目启动应用程序时,可以通过ProcessStartInfo.arguments
属性传递这些参数。请让我知道哪部分没有意义,我将尝试改进答案。如果您使用问题中的第一个代码示例启动我的应用程序(WinFormsTest.exe
),则表单的标题将是10
。清楚了吗?这应该很清楚。。。我解释得再简单不过了因此,在应用程序中修改Program.cs中的入口点(使.exe能够读取命令行),并修改Form1.cs(使窗体能够接收输入)。我使用了构造函数,但您可以公开公共属性或方法。从技术上讲,你也不必做这部分。在Main
方法中,我可以直接在类的实例上设置表单的Text
属性,然后将该实例传递给Run
命令:var form1=new form1{Text=titleArg};应用程序运行(form1)
关键是要理解主窗体的构造函数代码不是在.exe
中运行的第一个代码。它总是Main()
(几乎可以立即启动启动表单)。还要注意我的图像中的背景窗口。您将看到应用程序是从命令行启动的,其中包含一个参数,该参数成为表单标题
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));
}