怎样在C#中实现父窗体向子窗体传值和子窗体向父窗体传值
具体是在Form1和Form2中各有一个textbox和button,点击Form1的button将Form1的textbox中的值传入Form2中的textbox中。点...
具体是在Form1和Form2中各有一个textbox和button,点击Form1的button将Form1的textbox中的值传入Form2中的textbox中。点击Form2中的button将Form2中的textbox中的值传入Form1中的textbox中,如图:
展开
3个回答
展开全部
子窗体按钮事件
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步,首先会话结束
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步,首先会话结束
推荐于2016-07-28
展开全部
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();
}
}
}
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();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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();
}
}
}
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();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询