问题描述
我想在两个表单 (c#) 之间传递值.我该怎么做?
I want to pass values between two Forms (c#). How can I do it?
我有两种表格:Form1 和 Form2.
I have two forms: Form1 and Form2.
Form1 包含一个按钮.当我单击该按钮时,Form2 应该打开并且 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).
Form2 包含一个文本框和一个提交按钮.当我在 Form2 的文本框中键入任何消息并单击提交按钮时,Form2 应该关闭并且 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.
// Form 1
// inside the button click event
using(Form2 form2 = new Form2())
{
if(form2.ShowDialog() == DialogResult.OK)
{
someControlOnForm1.Text = form2.TheValue;
}
}
还有……
// Inside Form2
// Create a public property to serve the value
public string TheValue
{
get { return someTextBoxOnForm2.Text; }
}
这篇关于将值从一个表单发送到另一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!