C#(Winfrom)窗体传值问题(子窗体回传值给父窗体)
运行后,点击Form1的button1调出Form2,点击Form2的Button1,关闭Form2,同时form2上的Textbox中的值复制到Form1中的Textb...
运行后,点击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 中。
其他信息: 未将对象引用设置到对象的实例。”请问这是怎么回事儿? 展开
代码如下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 中。
其他信息: 未将对象引用设置到对象的实例。”请问这是怎么回事儿? 展开
2个回答
展开全部
实现思路:在子窗体定义委托 父窗体接收
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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();
}
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;
}
}
}
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;
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询