C#(Winfrom)窗体传值问题(子窗体回传值给父窗体)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#(Winfrom)窗体传值问题(子窗体回传值给父窗体)相关的知识,希望对你有一定的参考价值。
运行后,点击Form1的button1 调出Form2,点击Form2的Button1,关闭Form2,同时form2上的Textbox中的值复制到Form1中的Textbox1中。
代码如下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 WindowsFormsApplication5数据传输单向
public partial class Form1 : Form
public Form1()
InitializeComponent();
private string a;
public Form1(string a)
this.a = a;
InitializeComponent();
private void Form1_Load(object sender, EventArgs e)
this.textBox1.Text = a;
private void button1_Click(object sender, EventArgs e)
Form Form30 = new Form2();
Form30.TopLevel = false;
Form30.Parent = panel1;
//Form2.BackColor = Color.Black;
Form30.Size = panel1.Size;
Form30.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
Form30.Show();
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 WindowsFormsApplication5数据传输单向
public partial class Form2 : Form
public Form2()
InitializeComponent();
private void button1_Click(object sender, EventArgs e)
Form1 frm1 = (Form1)this.Owner;
frm1.textBox1.Text = this.textBox1.Text;
this.Close();
可是运行时总是弹出“未处理的“System.NullReferenceException”类型的异常出现在 WindowsFormsApplication5数据传输单向.exe 中。
其他信息: 未将对象引用设置到对象的实例。”请问这是怎么回事儿?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication5数据传输单向
public partial class Form1 : Form
public Form1()
InitializeComponent();
public void a(Form2 f2)
this.textBox1.Text = f2.a;
private void button1_Click_1(object sender, EventArgs e)
Form2 f2 = new Form2();
f2.f1 = this;
f2.Show();
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 WindowsFormsApplication5数据传输单向
public partial class Form2 : Form
public Form1 f1;
internal string a;
public Form2()
InitializeComponent();
private void button1_Click_1(object sender, EventArgs e)
f1.a(this);
f1.Show();
this.Close();
private void Form2_Load(object sender, EventArgs e)
a = this.textBox1.Text;
本回答被提问者采纳
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#(Winfrom)窗体传值问题(子窗体回传值给父窗体)的主要内容,如果未能解决你的问题,请参考以下文章