最简单的方法就是,给被调用窗体添加一个公共静态字段,类型就是要引用的窗体名。 例:class Form2: Form { public static Form F2;
public Form2() { F2 = this; } }class Form1: Form { // 这里这样用: Form2.F2.控件名.属性 } 如果想引用Form2内的其他成员,最简单的是把要引用的成员都改成public类型。不过这样破坏了Form2的封装性。最好的方法是用公共属性来封装这些引用。例如 class Form2: Form { public static Form F2; private System.Windows.Forms.TextBox textBox1;
public string TextBox { get {return this.textBox1.Text}; set {this.textBox1.Text = value}; } public Form2() { F2 = this; } }class Form1: Form { // 这里这样用: Form2.F2.TextBox = "欢迎楼主"; }
怎么都喜欢把简单事情搞这么复杂的, Form F = Application.OpenForms["Form2"]; // 爱咋整咋整 TextBox TB = (TextBox)F.Controls.Find("textBox1", true)[0]; F.Show(); F.Name = "a"; F.Paint += new PaintEventHandler(F_Paint);
注册事件:
button1.Click += new System.EventHandler(this.button1_Click);
private void Form1_Load(object sender, EventArgs e)
{
frm2.button1.Click += new EventHandler(frm2.button1_Click);//促发Form2中的button1_Click
frm2.button1.Click += new EventHandler(this.button1_Click);//促发Form1中的button1_Click }
从Form1生成Form3,
只要在Form3的构造函数中添加参数,
就可以将Form1中的参数传递给Form3了。运用静态变量。
在公共变量之前加入static修饰,
就可以在不同Form之间相互传递参数,
运用delegate。
参考
{
this.Cursor = Cursors.WaitCursor;string path=AssemblyName;
string name=strName; Form fm=(Form)Assembly.Load(path).CreateInstance(name);
fm.MdiParent=this.ParentForm;
fm.Show();
fm.Dock=DockStyle.Fill;
this.Cursor = Cursors.Default;
}
通过反射调用窗体
private void Form1_Load(object sender, EventArgs e)
{
frm2.button1.Click += new EventHandler(frm2.button1_Click);//促发Form2中的button1_Click
frm2.button1.Click += new EventHandler(this.button1_Click);//促发Form1中的button1_Click }使用这个方法 完全没反应 还是触发 不了第二个面板的事件
把事件写在接口里,比较好管理
1.接口里写一委托
using System;
using System.Collections.Generic;
using System.Text;namespace SeaskyFI.UI.Main
{
public delegate void ReturnVoucherID(int VchID);
public interface IReturnVoucherID
{
event ReturnVoucherID OnReturnVoucherID;
}
}2.在form2中使用此接口
form2:IReturnVoucherID
public event ReturnVoucherID OnReturnVoucherID; private void OnReturnVchID(object sender, EventArgs e)
{
VchInfo vchInfo = new VchInfo();
vchInfo = inputControl.Holder;
if (OnReturnVoucherID != null)
{
OnReturnVoucherID(inputControl.Holder.VCHID);
this.DialogResult = DialogResult.OK;
}
}3.在form1中注册此事件form1 frm1 = new form1 ();
frm1 .CreateAssetVoucher(vchInfo);
frm1 .OnReturnVoucherID += new ReturnVoucherID(frm1 _OnReturnVoucherID);
frm1 .ShowDialog();private void frm1 _OnReturnVoucherID(int VchID)
{
try
{
}
catch (Exception ex)
{
ShowMessage(ex);
}
}
private void btnClose_Click(object sender, EventArgs e)
{
// this.Close(); frmCallWeb myForm = new frmCallWeb();
myForm.btnClose_Click(myForm.btnClose,new EventArgs()); myForm.Show();
}Form2:
btnClose 属性设为Public
public void btnClose_Click(object sender, EventArgs e)//把Private改为public
{
label2.Text = "Hello!";
}
这种情况下你想要触发frm2.button1_Click就必须把frm2.button1_Click声明为public...
private void btnClose_Click(object sender, EventArgs e)
{
// this.Close(); frmCallWeb myForm = new frmCallWeb();
myForm.btnClose_Click(myForm.btnClose,new EventArgs()); myForm.Show();
}Form2:
btnClose 属性设为Public
public void btnClose_Click(object sender, EventArgs e)//把Private改为public
{
label2.Text = "Hello!";
}
这个方法实现了 但是 如果我不获取label2.Text的值 我又想不让 form2面板弹出来 而从 form2中获取 dataGridView 中的数据。怎么办?
例:class Form2: Form
{
public static Form F2;
public Form2()
{
F2 = this;
}
}class Form1: Form
{
// 这里这样用:
Form2.F2.控件名.属性
}
如果想引用Form2内的其他成员,最简单的是把要引用的成员都改成public类型。不过这样破坏了Form2的封装性。最好的方法是用公共属性来封装这些引用。例如
class Form2: Form
{
public static Form F2;
private System.Windows.Forms.TextBox textBox1;
public string TextBox
{
get {return this.textBox1.Text};
set {this.textBox1.Text = value};
} public Form2()
{
F2 = this;
}
}class Form1: Form
{
// 这里这样用:
Form2.F2.TextBox = "欢迎楼主";
}
Form F = Application.OpenForms["Form2"];
// 爱咋整咋整
TextBox TB = (TextBox)F.Controls.Find("textBox1", true)[0];
F.Show();
F.Name = "a";
F.Paint += new PaintEventHandler(F_Paint);
用28楼的方法, 在VS中运行代码分析,同样会弹出警告框.认为这是不合理的构造.
在form1里面实例化form2 Form2 form2 = new Form2()
在form1_load事件里面直接 form2. 就可以点出form2的属性方法和时间。
http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx
MainFrm.cs:public partial class MainFrm: Form
{
private void MainFrm _Load(object sender, EventArgs e)
{
Middle.sendEvent += new Middle.SendMessage(this.DoMethod);
}
public void DoMethod(string getstr)
{
listBox1.Items.Add(getstr);
}
}ChildFrm.cs:public partial class ChildFrm: Form
{
public ChildFrm ()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Middle.DoSendMessage(this.textBox1.Text);
textBox1.Text = "";
textBox1.Focus();
}
}Middle.cs:public static class Middle
{
public delegate void SendMessage(string str);
public static event SendMessage sendEvent;
public static void DoSendMessage(string str)
{
sendEvent(str);
}
}
//
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new Form1());
Form1 mainFrm = new Form1();
childFrm secondFrm = new childFrm();
secondFrm.Show();
Application.Run(mainFrm);
}
}
这时候在任意位置调用 globle就可以了不要忘了给分