怎样在C#中实现父窗体向子窗体传值和子窗体向父窗体传值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样在C#中实现父窗体向子窗体传值和子窗体向父窗体传值相关的知识,希望对你有一定的参考价值。
具体是在Form1和Form2中各有一个textbox和button,点击Form1的button将Form1的textbox中的值传入Form2中的textbox中。点击Form2中的button将Form2中的textbox中的值传入Form1中的textbox中,如图:
子窗体按钮事件ChildWindow child = new ChildWindow() textbox1.Text="我的名字是父窗体给的!" ;//第1步,给子窗体传值了
child.ShowDialog();//第2步,调用ShowDialog
if (child.DialogResult==true)//第3步,然后对DialogResult进行判断
this.Title = child.Title;//子窗体给父窗体传值
父窗体按钮事件
textbox2.Text= "我要给子窗体传值";
this.DialogResult = true;//第3步,首先会话结束 参考技术A 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;
//2、利用方法由子窗体向父窗体传值
private void button1_Click(object sender, EventArgs e)
Form1 f1 = new Form1();
f1.chuanzhi(this.textBox1.Text);
f1.Show();
本回答被提问者采纳 参考技术B Form1的代码:
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 MyURLRecond
public partial class Form1 : Form
public Form1()
InitializeComponent();
public Form1(string message)
InitializeComponent();
textBox1.Text = message;
private void button1_Click(object sender, EventArgs e)
Form2 form2 = new Form2(textBox1.Text.Trim());
form2.ShowDialog();
form2.Dispose();
Form2的代码:
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 MyURLRecond
public partial class Form2 : Form
public Form2()
InitializeComponent();
public Form2(string message)
InitializeComponent();
textBox1.Text = message;
private void button1_Click(object sender, EventArgs e)
Form1 form1 = new Form1(textBox1.Text.Trim());
form1.ShowDialog();
form1.Dispose();
c# 不同窗体之间传值和调用
1.子窗体事件刷新父窗体界面值
子窗体定义委托和事件
//声明一个委托 public delegate void DisplayUpdateDelegate(string str); //声明事件 public event DisplayUpdateDelegate ShowUpdate; private void Form2_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { if (ShowUpdate != null) { ShowUpdate(textBox1.Text.ToString()); } }
父窗体
private void button3_Click(object sender, EventArgs e) { Form2 _frm2 = new Form2(); _frm2.ShowUpdate += new Form2.DisplayUpdateDelegate(ShowMessage); _frm2.Show(); } private void ShowMessage(string str) { textBox1.Text = str; }
2.父窗体的事件刷新子窗体的值
父窗体代码
public delegate void ChangeSonWindowDelegate(string str); ChangeSonWindowDelegate _changeSonWindow; private void button1_Click(object sender, EventArgs e) { Form2 _frm2 = new Form2(); _changeSonWindow = new ChangeSonWindowDelegate(_frm2.ShowMessage); _frm2.Show(); } private void button2_Click(object sender, EventArgs e) { _changeSonWindow(textBox1.Text); }
子窗体代码
public void ShowMessage(string str) { textBox1.Text = str; }
以上是关于怎样在C#中实现父窗体向子窗体传值和子窗体向父窗体传值的主要内容,如果未能解决你的问题,请参考以下文章