在一个子窗口中将定时器数据以文本框显示,当窗口关闭后,定时器仍在读取数据。现在需要重新打开子窗口后,能继续在文本框显示后来的定时器数据,现在的问题是关闭了窗口后,就不能重新打开窗口了,代码如下:在主窗口中
public partial class Form1 : Form
{
private static WindowsFormsApplication4.Form2 form2;
int i = 0; public Form1()
{
InitializeComponent();
form2 = new Form2(this);
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);//当指定的计时器间隔已过去而且计时器处于启用状态时发生。
timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
i++;
form2.TextBoxDisplay(i.ToString());
} private void aaToolStripMenuItem_Click(object sender, EventArgs e)
{
form2.Show();
}
}
在文本框显示子窗口中
public partial class Form2 : Form
{
public Form2(WindowsFormsApplication4.Form1 p)
{
InitializeComponent();
this.MdiParent = p;
} public void TextBoxDisplay(string text)
{
textBox1.Text = text;
} }
public partial class Form1 : Form
{
private static WindowsFormsApplication4.Form2 form2;
int i = 0; public Form1()
{
InitializeComponent();
form2 = new Form2(this);
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);//当指定的计时器间隔已过去而且计时器处于启用状态时发生。
timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
i++;
form2.TextBoxDisplay(i.ToString());
} private void aaToolStripMenuItem_Click(object sender, EventArgs e)
{
form2.Show();
}
}
在文本框显示子窗口中
public partial class Form2 : Form
{
public Form2(WindowsFormsApplication4.Form1 p)
{
InitializeComponent();
this.MdiParent = p;
} public void TextBoxDisplay(string text)
{
textBox1.Text = text;
} }
private void timer1_Tick(object sender, EventArgs e)
{
i++;
form2.TextBoxDisplay(i.ToString());
}
这里岂不出错?
别关form2,隐藏行么?或者别在计数器里写form2的东西,累加一个form1的static int
1.每次都new一个新的窗体
form2.Show();
改为
new Form2().Show();2.在form2中,关闭改为隐藏,例如
public class Form2 : Form
{
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
base.OnClosing(e);
this.Hide();
}
}
{
Form2 form2=new Form2();
form2.Show(this);
}这样试试 ?