You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WinForms托盘空白通知弹窗问题求助:无会议时隐藏通知

问题:用户无会议时仍弹出空白系统托盘通知

我正在编写一个创建系统托盘弹出消息的程序,逻辑是先验证是否为有效用户,有效则显示会议通知,否则不显示。但现在遇到一个问题:当指定用户(UserCode=2)没有会议时,仍然会弹出空白的通知,我希望这种情况下直接隐藏该通知,恳请帮忙排查问题。

我的代码

using (SqlConnection CNN = new SqlConnection(AppManager.GetConnectionString())) {
 CNN.Open(); 
 //getting all records from Meeting
 using (SqlCommand scmd = new SqlCommand()) {
 using (DataTable dtMeeting = new DataTable()) {
 scmd.CommandText = "SELECT MeetingNumber from Meeting";
 scmd.Connection = CNN;
 SqlDataAdapter sdaMeeting = new SqlDataAdapter(scmd);
 sdaMeeting.Fill(dtMeeting);
 foreach (DataRow drMeeting in dtMeeting.Rows) {
 using (DataTable dtAttendies = new DataTable()) {
 scmd.CommandText = "SELECT MEETING.MeetingNumber, MEETING.MeetingType," + 
 " MEETINGTYPEMASTER.MeetingTypeName, MEETING.MeetingName,MEETING.Location," + 
 " MEETING.StartTime, MEETING.EndTime, MEETINGATTENDEES.AttendeeType," + 
 " MEETING.Prepared, MEETING.PreparedBy, MEETING.PreparedDateTime," + 
 " MEETING.BranchCode" + 
 " FROM MEETING, MEETINGTYPEMASTER, EMPLOYEEMASTER ,MEETINGATTENDEES," + 
 " DESIGNATIONMASTER, DEPARTMENTMASTER,USERMASTER" + 
 " Where MEETING.MeetingType=MEETINGTYPEMASTER.MeetingTypeCode" + 
 " And EMPLOYEEMASTER.EmployeeCode=MEETINGATTENDEES.EmployeeCode" + 
 " And EMPLOYEEMASTER.DepartmentCode=DEPARTMENTMASTER.DepartmentCode" + 
 " And EMPLOYEEMASTER.DesignationCode=DESIGNATIONMASTER.DesignationCode" + 
 " And MEETING.BranchCode=xxx" + 
 " And MEETING.MeetingNumber=MEETINGATTENDEES.MeetingNumber" + 
 " And MEETING.MeetingNumber= " + drMeeting["MeetingNumber"] + "" + 
 //This meeting number coming from outer loop
 " And EMPLOYEEMASTER.EmployeeCode=USERMASTER.EmployeeCode" + 
 " And USERMASTER.UserCode=2";//User code
 scmd.Connection = CNN;
 SqlDataAdapter sdaAttendies = new SqlDataAdapter(scmd);
 sdaAttendies.Fill(dtAttendies);
 if (dtAttendies.Rows.Count != 0) {
 foreach (DataRow drAttendies in dtAttendies.Rows) {
 lblMeetingType.Text = dtAttendies.Rows[0]["MeetingTypeName"].ToString();
 lblMeetingName.Text = dtAttendies.Rows[0]["MeetingName"].ToString();
 }
 } else {
 // if the user code has no meeting still the blank popup is coming
 //Here no code is given but still the notification popup is coming
 }
 }
 }
 }
 }
 this.Close();
} catch (Exception ex) {
 Console.WriteLine(ex.ToString());
}
}
private void Form1_Resize(object sender, EventArgs e) {
 if (WindowState == FormWindowState.Minimized) {
 ShowInTaskbar = false;
 notifyIcon1.Visible = true;
 notifyIcon1.ShowBalloonTip(1000);
 }
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
 ShowInTaskbar = true;
 notifyIcon1.Visible = false;
 WindowState = FormWindowState.Normal;
}
public Form1() {
 InitializeComponent();
}
static class Program {
 [STAThread]
 static void Main() {
 Application.EnableVisualStyles();
 Application.SetCompatibleTextRenderingDefault(false);
 Application.Run(new Form1());
 }
}

我找不到错误所在,请不要协助我实现显示通知的功能,谢谢。


解决方案

嘿,问题其实很明显——你的托盘通知触发逻辑和会议查询逻辑完全没关联!

看你的Form1_Resize方法:只要窗口最小化,不管用户有没有会议,你都会无条件调用notifyIcon1.ShowBalloonTip(1000);,这就是无会议时弹出空白通知的核心原因。

具体修复步骤

  1. 添加一个状态标记:在Form1类里声明一个私有布尔变量,用来记录当前用户是否有会议:
private bool hasUserMeeting = false;
  1. 在查询逻辑中更新标记:当查询到用户有参会记录时,把这个变量设为true:
if (dtAttendies.Rows.Count != 0) {
    hasUserMeeting = true; // 标记用户存在有效会议
    foreach (DataRow drAttendies in dtAttendies.Rows) {
        lblMeetingType.Text = dtAttendies.Rows[0]["MeetingTypeName"].ToString();
        lblMeetingName.Text = dtAttendies.Rows[0]["MeetingName"].ToString();
    }
}
  1. 修改Resize事件的触发条件:只有当用户有会议时,才弹出托盘通知:
private void Form1_Resize(object sender, EventArgs e) {
    if (WindowState == FormWindowState.Minimized) {
        ShowInTaskbar = false;
        notifyIcon1.Visible = true;
        // 仅当用户有会议时才显示通知
        if (hasUserMeeting) {
            notifyIcon1.ShowBalloonTip(1000);
        }
    }
}

这样调整后,当用户没有会议时,hasUserMeeting会保持false,窗口最小化时就不会触发空白通知了。另外提一句,你当前的会议查询逻辑效率偏低(循环查询每个Meeting),不过既然你没提这个,我就不多展开,先解决你当前的核心问题~

内容的提问来源于stack exchange,提问作者Ramachandran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:51:22