1、父窗体
/// <summary> /// 弹窗 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPopWin_Click(object sender, EventArgs e) { FormItem formItem = new FormItem(this); formItem.itemno = "1"; formItem.Show(); } /// <summary> /// 传值 /// </summary> /// <param name="str"></param> public void setValue(string str) { txtSub.Text = str; }
2、子窗体
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace oms { public partial class FormItem : Form { public string itemno = ""; FrmMain frmMain = new FrmMain(); public FormItem(FrmMain frm) { frmMain = frm; InitializeComponent(); } /// <summary> /// 设置父窗体的值 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSet_Click(object sender, EventArgs e) { // 方法1 FormMain.Designer.cs 控件的属性 private 调整为 public // public System.Windows.Forms.TextBox txtLine; string str = txtTemp.Text; frmMain.txtLine.Text = str; // 方法2 定义一个 public 函数 frmMain.setValue(str); } /// <summary> /// 初始化传值 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormItem_Load(object sender, EventArgs e) { txtTemp.Text = itemno; } } }