頁(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; });