窗口问题,大家解惑啊!
要实现 Form1中用一个定时器每五分钟调用一次Form2对象传一个string参数,Form2中有一个lable,让它的Text为传过来的string的内容,并用Show方法显示。但是每次Show出来的效果希望是如果前面的Form2窗口如果没有被用户关闭则刷新lable.Text的内容,如果已经被用户关闭则重新弹出新的Form2窗口显示string内容。该怎么实现呢?大家帮帮忙。
------解决方案--------------------if(form2 != null)
{
form2.label.Text = " ";
form2.Refresh();
}
else
{
form2 = new form2();
form2.label.Text = " ";
form2.Refresh();
}
------解决方案--------------------public partial class Form1 : Form
{
private Timer mtimer;
Form2 mform2;
public Form1()
{
InitializeComponent();
mtimer = new Timer();
mtimer.Interval = 5* 1000;
mtimer.Tick += new EventHandler(mtimer_Tick);
mtimer.Enabled = false;
}
void mtimer_Tick(object sender, EventArgs e)
{
if (mform2 == null)
mform2 = new Form2();
Random rd = new Random();
mform2.ShowMessage(String.Format( "Form1 Send:{0} ",rd.Next().ToString()));
}
private void button1_Click(object sender, EventArgs e)
{
if (mtimer.Enabled)
return;
if (mform2 == null)
mform2 = new Form2();
mform2.Show();
mform2.ShowMessage( "Start ");
mtimer.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
if (!mtimer.Enabled)
return;
mtimer.Enabled = false;
mform2.Dispose();
mform2 = null;
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void ShowMessage(string _strmsg)
{
label1.Text = _strmsg;
this.Show();
this.BringToFront();
this.Update();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}
}
------解决方案--------------------Form f;
//timer1的Enabled为true,Interval值我就不说了
private void timer1_Tick(object sender, EventArgs e)
{
if (f == null)
{
f = new Form2();
f.Show();
}
f.lable1.Text = DateTime.Now.ToLongTimeString();
//lable1设为public的,后边的值相当于传过去的值
}
------解决方案--------------------简单点写,可以这样试下
Form1中
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
Form2 f2 = null;
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Interval = 300000;
string fStr = textBox1.Text;
if (f2 == null || f2.IsDisposed)
{
f2 = new Form2();
f2.label1.Text = fStr;
f2.Show();
}
else
{