Send values from one form to another form(将值从一个表单发送到另一个表单)
问题描述
我想在两个表单 (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; }
}
这篇关于将值从一个表单发送到另一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将值从一个表单发送到另一个表单
基础教程推荐
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
