我的Windows窗体应用程序(C#)有一个奇怪的问题。我在我的项目中有一个带有NotifyIcon的表单(Form1)和另一个表单(Form2)。 Form1有一些代码基本上做了一些XML解析,并在Form2上添加了一堆LinkLabel。这一切工作正常。Windows窗体:关闭,但不关闭
在NotifyIcon上,我有一个带有EventHandler的ContextMenu MenuItem(称为“Refresh”),基本上可以通过XML解析命令运行。我还在NotifyIcon上有一个MouseDoubleClick命令,当系统托盘图标被双击时,它打开Form2(使用ShowDialog())显示LinkLabels的链接。这也很好。
我有Form2上的一个按钮控件(称为 “关闭”),正在使用此事件处理代码:
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
关闭窗体2。这也很好。
但是,根据Form1的NotifyIcon上的“Refresh”ContextMenu MenuItem命中的次数,这是在Form2关闭之前必须按下Form2上的“关闭”按钮控件的次数。例如,如果用户三次在Form1上点击“Refresh”MenuItem,他们将不得不在Form2上点击“关闭”按钮控件三次。这几乎就像Form2明显关闭,但代码似乎没有认识到它已关闭。
我是新来的Windows窗体的发展,所以也许这是我不知道的生命周期的事情。任何帮助,将不胜感激。
编辑。我会尝试发布相应的代码:
//NotifyIcon Form (Form1)
//ico is the ID of the NotifyIcon
public partial class TrayIcon : Form
{
Message msg = new Message(); //Form2
ContextMenu contextMenu = new ContextMenu();
public TrayIcon()
{
InitializeComponent();
}
private void TrayIcon_Load(object sender, EventArgs e)
{
contextMenu.MenuItems.Clear();
contextMenu.MenuItems.Add(0, new MenuItem("Refresh", new System.EventHandler(ico_Refresh)));
ico.DoubleClick += new MouseEventHandler(ico_ShowMsg);
DoXmlParsing();
}
private void ico_Refresh(object Sender, EventArgs e)
{
TrayIcon_Load(null, null);
}
private void ico_ShowMsg(object Sender, MouseEventHandler e)
{
if (msg.Visible == false)
msg.ShowDialog();
}
}
在窗体2(消息),我有这个点击处理代码按钮:
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
//I have also tried:
if (this.Visible)
this.Hide();
}
您可以发布Refresh MenuItem的代码和用于打开Form2的代码吗? – 2009-08-27 21:39:23
Is Fo RM2是一种无模式的形式?你有没有在btnClose_Click中放置一个断点来确保它每次都被调用? – 2009-08-27 21:39:48