在C#一个程序中,将一个窗体中的数据传送到另一个窗体

使用多个窗体搭建的程序,需要用到窗体间的数据传递,常用两种方法:

方法一

1,进入子窗体的Designer.cs,将子窗体中的私有控件控件定义为public

 2.在主窗口程序Form1.cs中将子窗口实例化

 3.直接引用子船体中的控件

方法二:

在Program里 建一个结构体;里面定义一个静态变量,这个变量在FORM1 和FORM2 中可以通用;直接赋值取值就好;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Form1Form2
{
public struct mm
{
public static string m;
};
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

FORM1
mm.m = textBox1.Text;
FORM2
textBox1.Text = mm.m;

猜你喜欢

转载自www.cnblogs.com/runningsoybean/p/11549847.html