在解决方案下就好办了,你在Form1所在项目添加引用Form2所在的项目,使Form2依赖于Form1,给你写个Demo吧,如Form1所在项目为WindowsFormsApplication1,其中有Form1,Form1上有个Textbox1,Form2所在项目为WindowsFormsApplication2,其中有Form1,Form1上有个label,timer,label显示时间timer1.Interval=1000;timer1.Enabled=false;
WindowsFormsApplication2.Form1:
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public bool TimerEnabled
{
set { timer1.Enabled = value; }
}
int i=1;
private void timer1_Tick(object sender, EventArgs e)
{
i++;
label1.Text = i.ToString();
}
}
}
WindowsFormsApplication1--引用--右键--添加引用--项目,选择WindowsFormsApplication2--确定
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (int.Parse(textBox1.Text) == 10)
{
WindowsFormsApplication1.Form1 frm = new WindowsFormsApplication1.Form1();
frm.TimerEnabled = true; //启动Timer并打开窗体
frm.ShowDialog();
}
}
}
}
将WindowsFormsApplication1设为启动项目并运行
你在打开一个窗体时候加上
窗体.Owner = this;
Form1中,当Textbox1值为10时
Form1 frm1 = (Form1)this.Owner;
((Timer)frm1.Controls["panel1"].Controls["timer1"]).start();
给你一种方法 用C++的函数
看下这个http://blog.csdn.net/bdstjk/article/details/7000676。我这个例子是一个项目里面,你改下到两个项目
如果是两个项目的话,就要用反射了。。。因为是应用程序域不同,在不同的进程。
很简单啊。在form1中触发一个事件,伪代码如下:
public event TextChanged;
if(textbox1.Text=="10")
{
if(TextChanged!=null)
TextChanged(this,null);
}
在form2中定义一个form1的实例变量_form1
private form1 _form1;
public form2()
{
_form1=new form1();
_form1.TextChanged+=new EventHandler(Form1TextChanged);
}
private void Form1TextChanged(object sender,EventArgs e)
{
this.timer2.Start();
}