1. 程式人生 > 其它 >C#--運動控制05--實時報警及顯示方案

C#--運動控制05--實時報警及顯示方案

以下是學習筆記:

思路:

正常時候顯示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);
        }