C#上位機(jī)編程之?dāng)?shù)據(jù)范圍設(shè)置頁(yè)面

頁(yè)面效果

image.png

變量類型定義

頁(yè)面顯示名稱 變量名稱 類型
類別 _type byte
量程上限 _RnMax float
量程下限 _RnMin float
原值上限 _rawMax float
原值下限 _rawMin float

類定義

 public class Scaling:IComparable<Scaling>
    {
        short _id;
        byte _type;
        float _euMax;
        float _euMin;
        float _rawMax;
        float _rawMin;

        public Scaling(short ID, byte type, float euMax, float euMin, float rawMax, float rawMin)
        {
            _id = ID;
            _type = type;
            _euMax = euMax;
            _euMin = euMin;
            _rawMax = rawMax;
            _rawMin = rawMin;
        }

        public short ID { get { return _id; } set { _id = value; } }
        public byte Type { get { return _type; } set { _type = value; } }
        public float EuMax { get { return _euMax; } set { _euMax = value; } }
        public float EuMin { get { return _euMin; } set { _euMin = value; } }
        public float RawMax { get { return _rawMax; } set { _rawMax = value; } }
        public float RawMin { get { return _rawMin; } set { _rawMin = value; } 
        public int CompareTo(Scaling other){return _id.CompareTo(other._id);}
    }

控件邏輯

  • 定義一個(gè)List,List包含所有的tagdata類型的變量;
  • 通過(guò)ID在list中尋找需要編輯的變量是哪個(gè);
    -加載時(shí)進(jìn)行初始化控件值
  • 編輯變量
  • 退出時(shí)保存
//加載時(shí)初始化控件值
private void ScaleParams_Load(object sender, EventArgs e)
        {
            //查詢數(shù)組 BinarySearch
             int index =  _list.BinarySearch(new   Scaling(_data.ID, 0, 0, 0, 0, 0));
            if (index >= 0)
            {
                _scaling = _list[index];
                rbNone.Checked = (_scaling.Type == 0);
                rbLine.Checked = (_scaling.Type == 1);
                rbSquare.Checked = (_scaling.Type == 2);

                nm_RawMax.Value = (decimal)_scaling.RawMax;
                nm_RawMin.Value= (decimal)_scaling.RawMin  ;
                nm_EuMax.Value= (decimal)_scaling.EuMax  ;
                nm_EuMin.Value = (decimal)_scaling.EuMin;
            }
        }

//退出時(shí)保存
 private void ScaleParams_FormClosing(object sender, FormClosingEventArgs e)
        {
            save();
        }

 private void save()
        {
                   //判斷是否進(jìn)行scale操作
            //條件1: tagData.Scale == true
            //條件2: rbNone 沒(méi)有選擇
            //條件3: 值設(shè)置合理?? Max>Min
            _data.HasSclae = (rbNone.Checked == false) && (nm_RawMax.Value > nm_RawMin.Value) && (nm_EuMax.Value > nm_EuMin.Value);

            //不編輯范圍時(shí),移除并返回
            if (_data.HasSclae == false)
            {
                if (_scaling != null) _list.Remove(_scaling);
                return;
            }
            //需要編輯時(shí),如果沒(méi)有新增一個(gè),有的話更新數(shù)據(jù)
            if (_scaling == null)
            {
                _list.Add(new Scaling(_data.ID, (byte)(rbLine.Checked ? 1 : 2), (float)nm_EuMax.Value, (float)nm_EuMin.Value, (float)nm_RawMax.Value, (float)nm_RawMin.Value));
            }
            else
            {
                _scaling.Type = (byte)(rbLine.Checked ? 1 : 2);
                _scaling.EuMax = (float)nm_EuMax.Value;
                _scaling.EuMin = (float)nm_EuMin.Value;
                _scaling.RawMax = (float)nm_RawMax.Value;
                _scaling.RawMin = (float)nm_RawMin.Value;
            }
            _data.HasSclae = true;

        }

重點(diǎn):

在新增的時(shí)候,需要添加新的Scaling,但是List采用的是二分法進(jìn)行的搜索,所以需要對(duì)List進(jìn)行重新排序,排序的方法為:

 list.Sort((Scaling a,Scaling b) => { return a.ID > b.ID ? 1 : -1; });
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容