积极答复者
c#winform程序,一个窗口控制另一个窗口richtextbox显示问题

问题
-
我想要实现的程序是:form1主窗口中按钮打开form2和form3,三个窗口都有richtextbox,可以实现三个窗口交叉操作他们的richtextbox(就是他们可以在另外两个窗口的richtextbox中添加字符)。
遇到的问题:子窗口如何form2如何操作父窗口的richtextbox?两个子窗口如何互相操作?
而且涉及线程问题,要考虑“从来不是创建它的线程操作”这个问题。
父窗口操作子窗口的问题暂时有个想法在下面回复中。
form1代码
private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.richTextBoxAppendText("咳咳,测试测试~喂喂~");
}private void button3_Click(object sender, EventArgs e)
{
Form2 fr2 = new Form2();
fr2.Show();
}form2代码
private delegate void _ViewRecvie(string strTemp);//定义显示委托
//------------------------ricktxtbox显示程序-------------------
public void richTextBoxAppendText(string strBUF)
{
if (this.InvokeRequired)
{
Invoke(new _ViewRecvie(richTextBoxAppendText), new object[] { strBUF });
}
else
{
this.richTextBox1.AppendText(strBUF);
}
}private void button1_Click(object sender, EventArgs e)
{
this.richTextBoxAppendText("咳咳,喂喂,测试测试~~~");
}程序编译没报错,richTextBox设置成public了。但是在form1中点按钮就没反应,form2的按钮点了之后就有显示。
请各位大侠,帮帮忙~~
- 已编辑 Mclovin 2013年1月17日 3:40
答案
-
我写了一个完整的示例演示了你上面的问题
示例如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace X.WinForms.Tutorials.Q2013011701 { /// <summary> /// 所有示列的父窗体 /// </summary> public partial class BaseForm : Form { public RichTextBox ContentControl { get; set; } public Panel ButtonContainer { get; set; } public virtual void AppendText(string fValue) { if (this.ContentControl == null) return; this.ContentControl.AppendText(fValue); } public BaseForm() { this.ButtonContainer = new Panel { Dock = DockStyle.Bottom, Height = 40, Padding = new Padding(7) }; this.ContentControl = new RichTextBox { Dock = DockStyle.Fill }; this.Controls.Add(ButtonContainer); this.Controls.Add(ContentControl); } private void InitializeComponent() { this.SuspendLayout(); // // BaseForm // this.ClientSize = new System.Drawing.Size(284, 261); this.Name = "BaseForm"; this.ResumeLayout(false); } } /// <summary> /// 主窗体 /// </summary> public partial class MainForm : BaseForm { public List<ChildForm> Children { get; set; } public MainForm() { this.Children = new List<ChildForm>(); InitTutorials(); } void InitTutorials() { var btnCallAllChildren = new Button { Dock = DockStyle.Left, Text = "向所有子窗体添加一个新的 GUID", Width = 200, }; var btnCallA = new Button { Dock = DockStyle.Left, Text = "向 A 窗体添加一个新的 GUID", Width = 200, }; var btnCallB = new Button { Dock = DockStyle.Left, Text = "向 B 窗体添加一个新的 GUID", Width = 200, }; btnCallAllChildren.Click += (o, e) => this.Children.ForEach(r => r.AppendText(Guid.NewGuid().ToString())); btnCallA.Click += (o, e) => { var form = this.Children.FirstOrDefault(r => r.Text == "A"); if (form == null) { form = new ChildForm(this) { Text = "A" }; this.Children.Add(form); } if (!form.Visible) form.Visible = true; form.AppendText(Guid.NewGuid().ToString()); }; btnCallB.Click += (o, e) => { var form = this.Children.FirstOrDefault(r => r.Text == "B"); if (form == null) { form = new ChildForm(this) { Text = "B", Visible = true }; this.Children.Add(form); } if (!form.Visible) form.Visible = true; form.AppendText(Guid.NewGuid().ToString()); }; this.ButtonContainer.Controls.Add(btnCallAllChildren); this.ButtonContainer.Controls.Add(btnCallA); this.ButtonContainer.Controls.Add(btnCallB); } } /// <summary> /// 子窗体 /// </summary> public partial class ChildForm : BaseForm { public MainForm Parent { get; set; } public ChildForm(MainForm fParent) { this.Parent = fParent; InitTutorials(); } void InitTutorials() { var btnCallParent = new Button { Dock = DockStyle.Left, Text = "向父窗体添加一个新的 GUID", Width = 200, }; btnCallParent.Click += (o, e) => this.Parent.AppendText(Guid.NewGuid().ToString()); this.ButtonContainer.Controls.Add(btnCallParent); } } }
知识改变命运,奋斗成就人生!
- 已标记为答案 Mclovin 2013年1月17日 7:03
-
参考上面的回答,我也查了一些实例,找到一些方法,这里发出来希望能给遇到跟我相同问题的人一些帮助。
一:两个子窗口之间相互发送:
在主窗口中,定义静态对象,public static Form2 f2 = new Form2();在按钮中f2.Show();
这样在form3中直接:Form1.f2.richTextBox1.AppendText("测试测试");就可以了。(子窗口之间都一样的)
还有一种方法:(直接贴代码了)
private partial class Form1:Form
{
public static List<Form2> frm2 {set;get;}
public static List<Form3> frm3 {set;get;}
public Form1()
{
InitializeComponent();
frm2 = new List<Form2>();
frm3 = new List<Form3>();
}
private void button1_Click(Object sender,EventArgs e)//这只写form2部分,form3一样的
{
var form2 = frm2.FirstOrDefault(r=>r.Text = "Form2");
if(form2 == null)
{
form2 = new Form2() {Text = "Form2"}
frm2.Add(form2);
form2.Owner = this;//注意这句代码就是子窗口向父窗口发送的关键
if(!form2.Visible)
{
form2.Visible = true;
}
}
}
在子窗口中:(只写form3向form2发送了,俩子窗口都一样的)
public partial class Form3:Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(Object sender,EventArgs e)
{
var form = Form1.frm2.FirstOrDefault(r=>r.Text = "Form2");//这里就查找在Form1中增加的Form2
form.richTextBox1.AppendText("测试测试");
}
}
这洋酒可以子窗口之间互相发送了;
父窗口向子窗口发送就简答了,就不详细发了(在父窗口中直接用实例调用子窗口控件就可以);
子窗口向父窗口发送需要用到前文标记的代码:form2.Owner = this;有了这句之后在子窗口Form2中:
private void button1_Click(Object sender,EventArgs e)
{
Form1 F1 = this.Owner as Form1;//这里是实例了Form2的Owner而不是从新new了一个Form1。
F1.richTextBox1.AppentText("测试测试GOTOForm1~~~");
}
在第一种方法中,在f2.Show();前面加上2.Owner = this;就可以了。
我把我的方法发出来了,我是新手有很多也不是很明白,我是参考大家的方法写的,有不好不对不准确等的地方,请指正!~~~
全部回复
-
private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();f2.Show();
f2.richTextBoxAppendText("咳咳,测试测试~喂喂~");
}嗯,我发现我的问题还要更复杂一些,刚我发现了可以反复按添加字符串的方法。实际问题,不光是form1操作form2,在form1中也有richtextbox,form2的按钮如何操作form1中的richtextbox呢?
我如果有三个窗口,form1主窗口button1打开form2,button2打开form3,怎么实现form2的按钮操作form3的richtextbox(不是重新实例化form3,然后.show();就是在form1打开的这个form3中显示),这个问题帮忙看下好么
-
private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();f2.Show();
f2.richTextBoxAppendText("咳咳,测试测试~喂喂~");
}嗯,我发现我的问题还要更复杂一些,刚我发现了可以反复按添加字符串的方法。实际问题,不光是form1操作form2,在form1中也有richtextbox,form2的按钮如何操作form1中的richtextbox呢?
我如果有三个窗口,form1主窗口button1打开form2,button2打开form3,怎么实现form2的按钮操作form3的richtextbox(不是重新实例化form3,然后.show();就是在form1打开的这个form3中显示),这个问题帮忙看下好么
class form1
{
Form form2;
Form form3;
Form1(){form2 = new Form2();form3 =new Form3(); form2.Form3Box = form3.richTextBox;}private void button2_Click(object sender, EventArgs e)
{form2.Show();
form2.richTextBoxAppendText("咳咳,测试测试~喂喂~");
}private void button3_Click(object sender, EventArgs e)
{form3.Show();
form3.richTextBoxAppendText("咳咳,测试测试~喂喂~");
}}
class Form2
{
private void button2_Click(object sender, EventArgs e)
Form3Box.AppendText("咳咳,测试测试~喂喂~");
{
}}
-
private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();f2.Show();
f2.richTextBoxAppendText("咳咳,测试测试~喂喂~");
}嗯,我发现我的问题还要更复杂一些,刚我发现了可以反复按添加字符串的方法。实际问题,不光是form1操作form2,在form1中也有richtextbox,form2的按钮如何操作form1中的richtextbox呢?
我如果有三个窗口,form1主窗口button1打开form2,button2打开form3,怎么实现form2的按钮操作form3的richtextbox(不是重新实例化form3,然后.show();就是在form1打开的这个form3中显示),这个问题帮忙看下好么
class form1
{
Form form2;
Form form3;
Form1(){form2 = new Form2();form3 =new Form3(); form2.Form3Box = form3.richTextBox;}private void button2_Click(object sender, EventArgs e)
{form2.Show();
form2.richTextBoxAppendText("咳咳,测试测试~喂喂~");
}private void button3_Click(object sender, EventArgs e)
{form3.Show();
form3.richTextBoxAppendText("咳咳,测试测试~喂喂~");
}}
class Form2
{
private void button2_Click(object sender, EventArgs e)
Form3Box.AppendText("咳咳,测试测试~喂喂~");
{
}}
form2.Form3Box Form3Box是什么?参数?方法? 我按照这个写了就报错了
form1的代码报错:form2不包含“Form3Box”的定义
form2的代码报错:上下文不存在Form3Box
-
我写了一个完整的示例演示了你上面的问题
示例如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace X.WinForms.Tutorials.Q2013011701 { /// <summary> /// 所有示列的父窗体 /// </summary> public partial class BaseForm : Form { public RichTextBox ContentControl { get; set; } public Panel ButtonContainer { get; set; } public virtual void AppendText(string fValue) { if (this.ContentControl == null) return; this.ContentControl.AppendText(fValue); } public BaseForm() { this.ButtonContainer = new Panel { Dock = DockStyle.Bottom, Height = 40, Padding = new Padding(7) }; this.ContentControl = new RichTextBox { Dock = DockStyle.Fill }; this.Controls.Add(ButtonContainer); this.Controls.Add(ContentControl); } private void InitializeComponent() { this.SuspendLayout(); // // BaseForm // this.ClientSize = new System.Drawing.Size(284, 261); this.Name = "BaseForm"; this.ResumeLayout(false); } } /// <summary> /// 主窗体 /// </summary> public partial class MainForm : BaseForm { public List<ChildForm> Children { get; set; } public MainForm() { this.Children = new List<ChildForm>(); InitTutorials(); } void InitTutorials() { var btnCallAllChildren = new Button { Dock = DockStyle.Left, Text = "向所有子窗体添加一个新的 GUID", Width = 200, }; var btnCallA = new Button { Dock = DockStyle.Left, Text = "向 A 窗体添加一个新的 GUID", Width = 200, }; var btnCallB = new Button { Dock = DockStyle.Left, Text = "向 B 窗体添加一个新的 GUID", Width = 200, }; btnCallAllChildren.Click += (o, e) => this.Children.ForEach(r => r.AppendText(Guid.NewGuid().ToString())); btnCallA.Click += (o, e) => { var form = this.Children.FirstOrDefault(r => r.Text == "A"); if (form == null) { form = new ChildForm(this) { Text = "A" }; this.Children.Add(form); } if (!form.Visible) form.Visible = true; form.AppendText(Guid.NewGuid().ToString()); }; btnCallB.Click += (o, e) => { var form = this.Children.FirstOrDefault(r => r.Text == "B"); if (form == null) { form = new ChildForm(this) { Text = "B", Visible = true }; this.Children.Add(form); } if (!form.Visible) form.Visible = true; form.AppendText(Guid.NewGuid().ToString()); }; this.ButtonContainer.Controls.Add(btnCallAllChildren); this.ButtonContainer.Controls.Add(btnCallA); this.ButtonContainer.Controls.Add(btnCallB); } } /// <summary> /// 子窗体 /// </summary> public partial class ChildForm : BaseForm { public MainForm Parent { get; set; } public ChildForm(MainForm fParent) { this.Parent = fParent; InitTutorials(); } void InitTutorials() { var btnCallParent = new Button { Dock = DockStyle.Left, Text = "向父窗体添加一个新的 GUID", Width = 200, }; btnCallParent.Click += (o, e) => this.Parent.AppendText(Guid.NewGuid().ToString()); this.ButtonContainer.Controls.Add(btnCallParent); } } }
知识改变命运,奋斗成就人生!
- 已标记为答案 Mclovin 2013年1月17日 7:03
-
private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();f2.Show();
f2.richTextBoxAppendText("咳咳,测试测试~喂喂~");
}嗯,我发现我的问题还要更复杂一些,刚我发现了可以反复按添加字符串的方法。实际问题,不光是form1操作form2,在form1中也有richtextbox,form2的按钮如何操作form1中的richtextbox呢?
我如果有三个窗口,form1主窗口button1打开form2,button2打开form3,怎么实现form2的按钮操作form3的richtextbox(不是重新实例化form3,然后.show();就是在form1打开的这个form3中显示),这个问题帮忙看下好么
class form1
{
Form form2;
Form form3;
Form1(){form2 = new Form2();form3 =new Form3(); form2.Form3Box = form3.richTextBox;}private void button2_Click(object sender, EventArgs e)
{form2.Show();
form2.richTextBoxAppendText("咳咳,测试测试~喂喂~");
}private void button3_Click(object sender, EventArgs e)
{form3.Show();
form3.richTextBoxAppendText("咳咳,测试测试~喂喂~");
}}
class Form2
{
private void button2_Click(object sender, EventArgs e)
Form3Box.AppendText("咳咳,测试测试~喂喂~");
{
}}
form2.Form3Box Form3Box是什么?参数?方法? 我按照这个写了就报错了
form1的代码报错:form2不包含“Form3Box”的定义
form2的代码报错:上下文不存在Form3Box
class Form2 : Form
{
public RichTextBox Form3Box{get;set;}
}
-
我写了一个完整的示例演示了你上面的问题
示例如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace X.WinForms.Tutorials.Q2013011701 { /// <summary> /// 所有示列的父窗体 /// </summary> public partial class BaseForm : Form { public RichTextBox ContentControl { get; set; } public Panel ButtonContainer { get; set; } public virtual void AppendText(string fValue) { if (this.ContentControl == null) return; this.ContentControl.AppendText(fValue); } public BaseForm() { this.ButtonContainer = new Panel { Dock = DockStyle.Bottom, Height = 40, Padding = new Padding(7) }; this.ContentControl = new RichTextBox { Dock = DockStyle.Fill }; this.Controls.Add(ButtonContainer); this.Controls.Add(ContentControl); } private void InitializeComponent() { this.SuspendLayout(); // // BaseForm // this.ClientSize = new System.Drawing.Size(284, 261); this.Name = "BaseForm"; this.ResumeLayout(false); } } /// <summary> /// 主窗体 /// </summary> public partial class MainForm : BaseForm { public List<ChildForm> Children { get; set; } public MainForm() { this.Children = new List<ChildForm>(); InitTutorials(); } void InitTutorials() { var btnCallAllChildren = new Button { Dock = DockStyle.Left, Text = "向所有子窗体添加一个新的 GUID", Width = 200, }; var btnCallA = new Button { Dock = DockStyle.Left, Text = "向 A 窗体添加一个新的 GUID", Width = 200, }; var btnCallB = new Button { Dock = DockStyle.Left, Text = "向 B 窗体添加一个新的 GUID", Width = 200, }; btnCallAllChildren.Click += (o, e) => this.Children.ForEach(r => r.AppendText(Guid.NewGuid().ToString())); btnCallA.Click += (o, e) => { var form = this.Children.FirstOrDefault(r => r.Text == "A"); if (form == null) { form = new ChildForm(this) { Text = "A" }; this.Children.Add(form); } if (!form.Visible) form.Visible = true; form.AppendText(Guid.NewGuid().ToString()); }; btnCallB.Click += (o, e) => { var form = this.Children.FirstOrDefault(r => r.Text == "B"); if (form == null) { form = new ChildForm(this) { Text = "B", Visible = true }; this.Children.Add(form); } if (!form.Visible) form.Visible = true; form.AppendText(Guid.NewGuid().ToString()); }; this.ButtonContainer.Controls.Add(btnCallAllChildren); this.ButtonContainer.Controls.Add(btnCallA); this.ButtonContainer.Controls.Add(btnCallB); } } /// <summary> /// 子窗体 /// </summary> public partial class ChildForm : BaseForm { public MainForm Parent { get; set; } public ChildForm(MainForm fParent) { this.Parent = fParent; InitTutorials(); } void InitTutorials() { var btnCallParent = new Button { Dock = DockStyle.Left, Text = "向父窗体添加一个新的 GUID", Width = 200, }; btnCallParent.Click += (o, e) => this.Parent.AppendText(Guid.NewGuid().ToString()); this.ButtonContainer.Controls.Add(btnCallParent); } } }
知识改变命运,奋斗成就人生!
谢谢你的回答,我好好研究一下。 -
参考上面的回答,我也查了一些实例,找到一些方法,这里发出来希望能给遇到跟我相同问题的人一些帮助。
一:两个子窗口之间相互发送:
在主窗口中,定义静态对象,public static Form2 f2 = new Form2();在按钮中f2.Show();
这样在form3中直接:Form1.f2.richTextBox1.AppendText("测试测试");就可以了。(子窗口之间都一样的)
还有一种方法:(直接贴代码了)
private partial class Form1:Form
{
public static List<Form2> frm2 {set;get;}
public static List<Form3> frm3 {set;get;}
public Form1()
{
InitializeComponent();
frm2 = new List<Form2>();
frm3 = new List<Form3>();
}
private void button1_Click(Object sender,EventArgs e)//这只写form2部分,form3一样的
{
var form2 = frm2.FirstOrDefault(r=>r.Text = "Form2");
if(form2 == null)
{
form2 = new Form2() {Text = "Form2"}
frm2.Add(form2);
form2.Owner = this;//注意这句代码就是子窗口向父窗口发送的关键
if(!form2.Visible)
{
form2.Visible = true;
}
}
}
在子窗口中:(只写form3向form2发送了,俩子窗口都一样的)
public partial class Form3:Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(Object sender,EventArgs e)
{
var form = Form1.frm2.FirstOrDefault(r=>r.Text = "Form2");//这里就查找在Form1中增加的Form2
form.richTextBox1.AppendText("测试测试");
}
}
这洋酒可以子窗口之间互相发送了;
父窗口向子窗口发送就简答了,就不详细发了(在父窗口中直接用实例调用子窗口控件就可以);
子窗口向父窗口发送需要用到前文标记的代码:form2.Owner = this;有了这句之后在子窗口Form2中:
private void button1_Click(Object sender,EventArgs e)
{
Form1 F1 = this.Owner as Form1;//这里是实例了Form2的Owner而不是从新new了一个Form1。
F1.richTextBox1.AppentText("测试测试GOTOForm1~~~");
}
在第一种方法中,在f2.Show();前面加上2.Owner = this;就可以了。
我把我的方法发出来了,我是新手有很多也不是很明白,我是参考大家的方法写的,有不好不对不准确等的地方,请指正!~~~