c# 中,子窗口调用子窗口的控件怎么实现?
比如我主窗口是form1,创建了两个子窗口frm1,frm2,frm中有一个textbox1,frm2要控制frm1中的textbox1,怎么实现,我试过在frm2定义一...
比如我主窗口是form1,创建了两个子窗口frm1,frm2,frm中有一个textbox1,frm2要控制frm1中的textbox1,怎么实现,我试过在frm2定义一个frm1类,但是会窗口一个新的frm1出来,这不是我想要的。
你看看我错在哪里:
form1是主窗口,有一个按钮,单击后在form2中的textbox1中添加“hello”;
form2是form1创建的,有一个textbox1,;
form3也是form1创建的,和form1一样,有一个按钮,单击后在form2中的textbox1中添加“hello”;
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 test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool tag=true;
Form2 child = new Form2();
Form3 c3 = new Form3(child);
private void button1_Click(object sender, EventArgs e)
{
child.write("hello");
if (tag)
{
child.Show();
tag = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
c3.Show();
}
}
} 展开
你看看我错在哪里:
form1是主窗口,有一个按钮,单击后在form2中的textbox1中添加“hello”;
form2是form1创建的,有一个textbox1,;
form3也是form1创建的,和form1一样,有一个按钮,单击后在form2中的textbox1中添加“hello”;
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 test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool tag=true;
Form2 child = new Form2();
Form3 c3 = new Form3(child);
private void button1_Click(object sender, EventArgs e)
{
child.write("hello");
if (tag)
{
child.Show();
tag = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
c3.Show();
}
}
} 展开
3个回答
展开全部
mark
应该要用委托来实现的,今天没时间看做个记号
form 1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace formExample
{
public partial class Form1 : Form
{
public Form2 form2 = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
form2.Show();
}
private void button3_Click(object sender, EventArgs e)
{
form2.str += "hello";
form2.Refresh();
}
private void button2_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3(this.form2);
form3.SayHello += new EventHandler(sayhello);
form3.Show();
}
private void sayhello(object sender, EventArgs e)
{
form2.str += "hello";
form2.Refresh();
}
}
}
form 2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace formExample
{
public partial class Form2 : Form
{
public string str = "";
public Form2()
{
InitializeComponent();
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
textBox1.Text = str;
}
}
}
form3:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace formExample
{
public partial class Form3 : Form
{
public Form2 form2 = new Form2();
public event EventHandler SayHello;
protected virtual void OnSayHello(EventArgs e)
{
if (SayHello != null)
SayHello(this, e);
}
public Form3()
{
InitializeComponent();
}
public Form3(Form2 form2)
: this()
{
this.form2 = form2;
}
private void button1_Click(object sender, EventArgs e)
{
OnSayHello(new EventArgs());
}
}
}
这个问题蛮有意思的!~
应该要用委托来实现的,今天没时间看做个记号
form 1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace formExample
{
public partial class Form1 : Form
{
public Form2 form2 = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
form2.Show();
}
private void button3_Click(object sender, EventArgs e)
{
form2.str += "hello";
form2.Refresh();
}
private void button2_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3(this.form2);
form3.SayHello += new EventHandler(sayhello);
form3.Show();
}
private void sayhello(object sender, EventArgs e)
{
form2.str += "hello";
form2.Refresh();
}
}
}
form 2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace formExample
{
public partial class Form2 : Form
{
public string str = "";
public Form2()
{
InitializeComponent();
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
textBox1.Text = str;
}
}
}
form3:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace formExample
{
public partial class Form3 : Form
{
public Form2 form2 = new Form2();
public event EventHandler SayHello;
protected virtual void OnSayHello(EventArgs e)
{
if (SayHello != null)
SayHello(this, e);
}
public Form3()
{
InitializeComponent();
}
public Form3(Form2 form2)
: this()
{
this.form2 = form2;
}
private void button1_Click(object sender, EventArgs e)
{
OnSayHello(new EventArgs());
}
}
}
这个问题蛮有意思的!~
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
frm1=new Form();
frm2=new Form(frm1); //把frm1作为参数传给frm2
在frm2中把传递来的frm1存储起来(定义一个frm2的成员),把frm1的textbox1设置成公共的,然后在frm2中即可调用frm1的textbox1了。
frm2=new Form(frm1); //把frm1作为参数传给frm2
在frm2中把传递来的frm1存储起来(定义一个frm2的成员),把frm1的textbox1设置成公共的,然后在frm2中即可调用frm1的textbox1了。
更多追问追答
追问
可否贴个实例,谢谢了~~~!!!!
追答
Form1:
class Form1:Form
{
public TextBox textBox1=new TextBox();
public Form1()
{
}
}
Form2:
class Form2:Form
{
private Form1 frm1;
public Form2()
{
}
public Form2(Form1 form)
{
frm1 = form;
}
public void operateForm1()
{
//对frm1的TextBox操作,例如
MessageBox.Show(frm1.textBox1.Text, "操作frm1", MessageBoxButtons.OK);
}
}
应用:
Form1 frm1=new Form1();
Form2 frm2=new Form2(frm1);
frm2.operateForm1();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
new 一个
追问
new一个之后会产生新的窗口,这不是我想要的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询