怎样在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中,如图: 展开
 我来答
笨蛋是你我
2018-05-11 · TA获得超过106个赞
知道小有建树答主
回答量:192
采纳率:69%
帮助的人:14.7万
展开全部
子窗体按钮事件
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();
}

}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
pot2217331
2011-01-20 · TA获得超过226个赞
知道小有建树答主
回答量:201
采纳率:100%
帮助的人:86.1万
展开全部
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();
}

}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式