以下是学习笔记:
思路:
正常时候显示Label标签,有报警时候显示滚动条控件。
Label标签和滚动条标签大小一样,重叠在一起,通过是否报警,来显示或隐藏控件,达到切换正常和报警的状态。
1,安装NuGet滚动条控件:SeeSharpToolsJY.GUI
2,控件布局
【2.1】
【2.2】
ScrollingText的Visalbe设置为False;
【2.3】
3,代码实现
【3.1】
//【报警显示步骤1】在主窗体添加报警委托 /// <summary> /// 参数1:报警信息。参数2:这个报警是触发还是消除 /// </summary> private Action<string, bool> AddAlarm;
【3.2】
//【报警显示步骤2】在监控窗体添加报警委托的原型 public void AddAlarm(string info, bool isAck) { if (isAck) { //如果 if (!AlarmInfoList.Contains(info)) { AlarmInfoList.Add(info); } } else { if (AlarmInfoList.Contains(info)) { AlarmInfoList.Remove(info); } } //刷新界面 RefreshAlarm(); } /// <summary> /// 刷新报警控件界面显示 /// </summary> private void RefreshAlarm() { this.Invoke(new Action(() => { if (AlarmInfoList.Count == 0) { this.led_state.Value = true; this.lbl_info.Visible = true; this.lbl_scrollInfo.Visible = false; this.lbl_info.Text = "系统运行正常"; } else if (AlarmInfoList.Count == 1) { this.led_state.Value = false; this.lbl_info.Visible = true; this.lbl_scrollInfo.Visible = false; this.lbl_info.Text = AlarmInfoList[0]; } else { this.led_state.Value = false; this.lbl_info.Visible = false; this.lbl_scrollInfo.Visible = true; this.lbl_scrollInfo.Text = string.Join(" / ", AlarmInfoList).Trim(); } })); } /// <summary> /// 报警信息列表 /// </summary> private List<string> AlarmInfoList=new List<string>();
【3.2】
frm=new FrmMonitor(); //打开监控窗体的时候,主窗体的AddLog委托绑定监控窗体的AddLog方法 this.AddLog = ((FrmMonitor) frm).AddLog; //【报警步骤3】打开监控窗体的时候,主窗体的AddAlarm委托绑定监控窗体的AddAlarm方法 this.AddAlarm= ((FrmMonitor)frm).AddAlarm;
4,测试
private void button1_Click(object sender, EventArgs e) { AddAlarm(this.textBox1.Text, this.checkBox1.Checked); }