如何在Windows窗体中将值一个窗体传递给另一窗体 [英] How to pass the Value one form to another form in windows form

查看:59
本文介绍了如何在Windows窗体中将值一个窗体传递给另一窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#将值从一个表单传递到Windows表单应用程序中的另一表单?

How to pass the value one form to another form in Windows Form Application using C#?

推荐答案

这是有关表单协作的常见问题.最可靠的解决方案是在表单类中实现适当的接口.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA


可以通过不同的方法来实现.
一种是在其中一种表单上使用属性.
There are different ways to do this.
One is using a Property on one of your Forms.
MyForm frm = new MyForm();
// It is possible to set MyValue at any point in your code, as long as you have a reference to an instance of MyForm.
frm.MyValue = "some value";
frm.Show();


另一种是将其传递给构造函数.


Another is passing it to the constructor.

public partial class MyForm : Form
{
   private String _myValue;
   public Form1(String myValue)
   {
      InitializeComponent();
      _myValue = myValue;
      // Possibly use myValue here.
   }
   // Do stuff with _myValue here.
}

用法如下:

Usage would look like this:

MyForm frm = new MyForm("some value");
frm.Show();


另一种方法是使用方法...


Another approach could be to use a Method...

public partial class MyForm : Form
{
   private String _myValue;
   public void SetValue(String myValue)
   {
      _myValue = myValue;
     // Possibly do stuff with myValue here.
   }
   // Or use _myValue here.
}

用法:

MyForm frm = new MyForm();
frm.Show();
// Once again, you can use this anywhere in your code, as long as you have a reference to the instance of MyForm.
frm.SetValue("some value");

希望它会有所帮助! :)

Hope it helps! :)


为解决此问题,我喜欢采用以下两种方法之一:

方法1:使用构造函数

首先假设有两种形式Form1和Form2.您想要将任何值从Form1传递到Form2.只需使用Form2的构造函数将其传递即可.然后,您必须像这样定义Form2的构造函数:

Well, For the solution of this problem, I like to follow one of the following two methods:

Method 1: Using Constructor

First assume there are two forms Form1 and Form2. You want to pass any value from Form1 to Form2. Simply use the constructor of Form2 to pass it on. Then you have to define the constructor of Form2 like this:

public Form2(String anyValue)
{
    Value=anyValue;
    //do whatever you want
}



声明一个全局变量(此处为Value)以接收传递的值,并在任何需要的Form2中使用它.

在Form1中:像这样调用Form2:



Declare a global variable (here Value) to receive the passed value and use it in Form2 wherever you want.

In Form1: you call Form2 like this:

Form2 frm2=new Form2(anyStringValue);
frm2.Show();
//do else



方法2:使用封装功能/属性.

假设您要将一个字符串值从Form1传递到Form2.首先在Form2中声明一个String类型的全局变量,然后对其进行封装.



Method2: Using Encapsulation Capability/Property.

Assume you want to pass a string value from Form1 to Form2. First in Form2 declare a global variable of type String, then encapsulate it.

String receivedValue;


现在封装该字段(假设您知道如何封装一个字段).它应该看起来像这样:


Now encapsulate this field (assuming that you know how to encapsulate a field). It should look like this:

public String ReceivedValue
        {
            get { return receivedValue; }
            set { receivedValue = value; }
        }



从Form1调用Form2时,使用封装的字段/属性传递数据.



From Form1 while calling Form2 use encapsulated field/property to pass data.

Form2 frm2=new Form2();
frm2.ReceivedValue=_any_Value_From_Form1;


这篇关于如何在Windows窗体中将值一个窗体传递给另一窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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