将值从一个表单发送到另一个表单

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; }
}

这篇关于将值从一个表单发送到另一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)