C#中两个form窗体之间如何访问相互属性控件,如下:

一个mainform主窗体含有两个子窗体,form1窗体和form2窗体,form1和form2之间没有联系。form1中有个控件combobox1想加载additem(form2中的textbox.text)。应该怎么实现。form2并不在form1中,他俩是平级的。。。求大神帮忙啊
补充1:如果通过构造函数法,是不是就意味着form2在form1中了。补充2:如果通过在另一个类class1中添加个静态属性例如static form for2;再从form2中加载函数中加上class1.for2=this;那这个for2和mainform中的form2有什么联系和区别。

方法一、利用属性传值
BackGround:①点击 Button按钮,将主窗体Form1中textBox1 中的值传到 Form2中的textBox2中。② 点击Form2中的按钮,将Form2中textBox的值传给主窗体的文本框。
1、 在Form2中定义一个字段,封装成属性:
private string flag;
/// <summary>
/// 接收传过来的值
/// </summary>
public string Flag
{
get { return flag; }
set { flag = value; }
}
2、 在Form1 Button按钮事件中,实例化一个Form2 窗体对象,并将textBox1中的值赋给 Form2中的Flag,这样在窗体Form2的登录事件中就可以获取到窗体Form1传过来的值。
窗体:Form1中的代码:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Flag = textBox1.Text;
//关键地方 ↓
if (f2.ShowDialog() == DialogResult.OK)
{
textBox1.Text = f2.Flag;
}
}
窗体:Form2的Load()事件
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this.flag;
}
3、 子窗体传值给父窗体(回传) 点击Form2中的button按钮将Form2中textBox的值传给父窗体Form1.
窗体:Form2中的代码
private void button1_Click(object sender, EventArgs e)
{
flag = this.textBox1.Text;
//关键地方 ↓
this.DialogResult = DialogResult.OK;
}
方法二、利用子窗体中的构造函数 (实现了父窗体给子窗体的传值,但是子窗体的值回传暂未实现)
1、 重载窗体Form2中的 构造函数
string str = String.Empty;//接收传过来的值
public Form2(string textValue)
{
InitializeComponent();
this.str = textValue;
}
2、 主窗体调用子窗体时候传参数:主窗体Form1的Button事件
Form2 f2 = new Form2(textBox1.Text);
f2.ShowDialog();
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-07-12
将form1中这个控件的Modifiers属性修改为Public或Internal,其实这个方法并不是最好的,最好的办法是自己在form1中定义一个Public类型的属性,这个属性内部可以去操作这个控件,然后在form2中去调用。
如:
Public string TextValue {
get { return this.txtInput.Text; }
set { this.txtInput.Text=value; }
}
实际上form1中txtInput的Text属性在form2中是不可见的,定义了这个属性后,就可以在form2中来实现赋值了,这种方法既保护了原有的方法又实现了想要的功能。
建议采用这种办法。追问

你好,我还是有点不明白,怎么在form2中访问自己在form1中定义的TextValue属性啊

本回答被网友采纳
第2个回答  2012-07-12
利用个全局变量,,或者在form1,form2设置个参数,,构造个桥梁,让它们可以互相沟通。
第3个回答  推荐于2017-10-14
具体不明白你的意思,,,不晓得 是不是 在form2中的textbox中添加值,然后form1中的combox1中的下拉选型就自动添加对对应的值,我尝试做了个demo,发给你作参考.....
直接贴代码了:
mainForm
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 WindowsFormsApplication
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.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 WindowsFormsApplication
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
Init();
}
private void Init()
{
Form3 f3 = new Form3();
f3.Show();
f3.handler += new MyEventHandler(f3_handler);
}
void f3_handler(object sender, MyEventArgs e)
{
this.comboBox1.Items.Add(e.Text.ToString());
}
}
}

form3:
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 WindowsFormsApplication
{
public delegate void MyEventHandler(object sender,MyEventArgs e);
public partial class Form3 : Form
{
public event MyEventHandler handler;
public Form3()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
MyEventArgs myEvent = new MyEventArgs();
myEvent.Text = this.textBox1.Text;
if (handler != null)
{
handler(this, myEvent);
}
}
}
}

在定义一个事件类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication
{
public class MyEventArgs:EventArgs
{
public string Text
{
get;
set;
}
}
}追问

你好,辛苦了,看你的代码好像是用委托来传递参数的,如果form3和form2都是form1的成员呢,你的form3好像是在form2中初始化的,要是平级的呢就是没有父子关系怎么做啊。另外你可以帮我解答下我的追问里的疑问么,谢谢了,我是初学者,详细点哈~~~谢谢啊

本回答被提问者采纳
相似回答