C# 窗体之间怎么传值?

2025-02-24 22:41:15
推荐回答(1个)
回答1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 传值练习
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//1、利用构造函数由父窗体向子窗体传值
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this.textBox1.Text);
f2.Show();
}

//利用方法由子窗体向父窗体传值
public void chuanzhi(string data)
{
this.textBox1.Text = data;
}
}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 传值练习
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//1、利用构造函数由父窗体向子窗体传值
public Form2(string name)
{
InitializeComponent();
this.textBox1.Text = name;
}