问题描述
我想两种形式(C#)之间传递值。我该怎么办呢?
I want to pass values between two Forms (c#). How can I do it?
我有两种形式:Form 1和Form。
I have two forms: Form1 and Form2.
Form1中包含一个按钮。当我点击该按钮,窗体2应打开并Form1上应该是无效的模式(即未选择)。
Form1 contains one button. When I click on that button, Form2 should open and Form1 should be in inactive mode (i.e not selectable).
窗体2包含一个文本框,一个提交按钮。当我输入窗体2的文本框中输入任何信息,点击提交按钮时,窗体2应关闭并Form1中应提交的价值凸显。
Form2 contains one text box and one submit button. When I type any message in Form2's text box and click the submit button, the Form2 should close and Form1 should highlight with the submitted value.
我该怎么办呢?有人可以帮我用一个简单的例子做到这一点。
How can i do it? Can somebody help me to do this with a simple example.
推荐答案
有几种方法可以解决这个但这是我倾向于使用该模式。
There are several solutions to this but this is the pattern I tend to use.
//In Form1's button handler
using(Form2 form2 = new Form2())
{
if(form2.ShowDialog() == DialogResult.OK)
{
someControlOnForm1.Text = form2.TheValue;
}
}
和...
//In Form2
//Create a public property to serve the value
public string TheValue
{
get { return someTextBoxOnForm2.Text; }
}
这篇关于从一种形式发送值到另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!