如何在 C# 中的同一项目中从另一个 WinForm 访问一个 WinForm 的文本框值? [英] How to access a textbox value of one WinForm from another WinForm with in same project in C#?

查看:25
本文介绍了如何在 C# 中的同一项目中从另一个 WinForm 访问一个 WinForm 的文本框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过填充新表单来获取新值.我想访问 New WinForm 中存在的那些控件值.并更新当前 WinForm 中的值?

I want to take new values by populating a new Form. And i want to access those values of controls present in the New WinForm. And update values in Current WinForm ?

谁能帮我解决这个问题.

Can any one help me out of this problem.

谢谢!!!!!!!!!!!!

Thanku!!!!!!!!!!

推荐答案

最简单的方法(在我看来)是通过属性公开它.

The simplest way (in my opinion) is to expose this through properties.

示例:

在 Form2 中

public string MyTextBoxValue
{
    get
    {
        return myTextBox.Text;
    }
    set
    {
        myTextBox.Text = value;
    }
}

在 Form1 中(显示其他表单时):

In Form1 (when displaying the other form):

Form2 form2 = new Form2();
form2.MyTextBoxValue = "whatever it should be";
form2.Show();

..然后,当您想从其他形式读取值时:

..and then, when you want to read the value from the other form:

string someVariable = form2.MyTextBoxValue;

这种方式Form1不需要知道使用哪种控件(如果有的话)来表示Form2中的值,这意味着这可以Form2 中的任何更改,而无需更改 Form1 的代码.它还可以在将值分配给 Form2 中的 TextBox 控件之前对值执行验证,以防对有效值存在限制.

This way Form1 does not need to be aware of which kind of control that is used (if any) to represent the value in Form2, which means that this can change in Form2 without any need for changes in the code of Form1. It also makes it possible to perform validation on the value before assigning it to the TextBox control in Form2, in case there might be restrictions on valid values.

这篇关于如何在 C# 中的同一项目中从另一个 WinForm 访问一个 WinForm 的文本框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆