C#上位机编程之数据范围设置页面

页面效果

image.png

变量类型定义

页面显示名称 变量名称 类型
类别 _type byte
量程上限 _RnMax float
量程下限 _RnMin float
原值上限 _rawMax float
原值下限 _rawMin float

类定义

 public class Scaling:IComparable
    {
        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);}
    }

控件逻辑

  • 定义一个List,List包含所有的tagdata类型的变量;
  • 通过ID在list中寻找需要编辑的变量是哪个;
    -加载时进行初始化控件值
  • 编辑变量
  • 退出时保存
//加载时初始化控件值
private void ScaleParams_Load(object sender, EventArgs e)
        {
            //查询数组 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;
            }
        }

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

 private void save()
        {
                   //判断是否进行scale操作
            //条件1: tagData.Scale == true
            //条件2: rbNone 没有选择
            //条件3: 值设置合理?? Max>Min
            _data.HasSclae = (rbNone.Checked == false) && (nm_RawMax.Value > nm_RawMin.Value) && (nm_EuMax.Value > nm_EuMin.Value);

            //不编辑范围时,移除并返回
            if (_data.HasSclae == false)
            {
                if (_scaling != null) _list.Remove(_scaling);
                return;
            }
            //需要编辑时,如果没有新增一个,有的话更新数据
            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;

        }

重点:

在新增的时候,需要添加新的Scaling,但是List采用的是二分法进行的搜索,所以需要对List进行重新排序,排序的方法为:

 list.Sort((Scaling a,Scaling b) => { return a.ID > b.ID ? 1 : -1; });

你可能感兴趣的:(C#上位机编程之数据范围设置页面)