/// <summary> /// 添加快捷发送数据 /// </summary> /// <param name="serialWriteConfig"></param> private void AddWriteConfig(SerialWriteConfig serialWriteConfig) { List <SerialWriteConfig> listSerialWriteConfigs = FormMain.arrSerialWriteConfig.ToList(); listSerialWriteConfigs.Add(serialWriteConfig); FormMain.arrSerialWriteConfig = listSerialWriteConfigs.ToArray(); }
/// <summary> /// 编辑快捷发送数据 /// </summary> /// <param name="serialWriteConfig"></param> /// <param name="index"></param> private void EditWriteConfig(SerialWriteConfig serialWriteConfig, int index) { FormMain.arrSerialWriteConfig[index].Name = serialWriteConfig.Name; FormMain.arrSerialWriteConfig[index].Data = serialWriteConfig.Data; FormMain.arrSerialWriteConfig[index].Format = serialWriteConfig.Format; FormMain.arrSerialWriteConfig[index].IsTimer = serialWriteConfig.IsTimer; FormMain.arrSerialWriteConfig[index].Timer = serialWriteConfig.Timer; }
/// <summary> /// 加载快捷发送数据UI /// </summary> /// <param name="serialWriteConfig"></param> private void LoadSerialWriteUI(SerialWriteConfig serialWriteConfig) { txtWriteConfigName.Text = serialWriteConfig.Name; txtWriteConfigData.Text = serialWriteConfig.Data; frmMain.SerialFormatToCombo(cbWriteConfigFormat, serialWriteConfig.Format); chkWriteConfigTimer.Checked = serialWriteConfig.IsTimer; numWriteConfigTimer.Value = serialWriteConfig.Timer; btnWriteConfigSave.Text = "修改"; }
/// <summary> /// 保存按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnWriteConfigSave_Click(object sender, EventArgs e) { Button btn = (Button)sender; ListView listView = listViewWriteConfig; SerialWriteConfig serialWriteConfig = new SerialWriteConfig(); if (txtWriteConfigName.Text.Trim() == "") { MessageBox.Show("名称不能为空,请重新输入。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else if (txtWriteConfigData.Text.Trim() == "") { MessageBox.Show("数据不能为空,请重新输入。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } serialWriteConfig.Name = txtWriteConfigName.Text; serialWriteConfig.Data = txtWriteConfigData.Text; serialWriteConfig.Format = frmMain.ComboToSerialFormat(cbWriteConfigFormat); serialWriteConfig.IsTimer = chkWriteConfigTimer.Checked; serialWriteConfig.Timer = Convert.ToInt32(numWriteConfigTimer.Value); if (listView.SelectedIndices.Count > 0) { int selectIndex = listView.SelectedIndices[0]; int getIndex = SerialWriteConfig.GetIndex(FormMain.arrSerialWriteConfig, txtWriteConfigName.Text.Trim()); if (selectIndex != getIndex && getIndex >= 0) { MessageBox.Show("名称已经存在,请重新输入。", "冲突", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //修改数据 EditWriteConfig(serialWriteConfig, selectIndex); } else { if (SerialWriteConfig.GetIndex(FormMain.arrSerialWriteConfig, txtWriteConfigName.Text.Trim()) >= 0) { MessageBox.Show("名称已经存在,请重新输入。", "冲突", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //添加数据 AddWriteConfig(serialWriteConfig); } Json.WriteFile(SerialWriteConfig.Path, FormMain.arrSerialWriteConfig); InitListViewWriteConfig(listViewWriteConfig); InitSerialWriteUI(); FormMain.InitSerialWriteConfig(); }
/// <summary> /// 移动快捷发送数据 /// </summary> /// <param name="listView"></param> /// <param name="isUp"></param> private void MoveWriteConfig(ListView listView, Boolean isUp = true) { if (listView.SelectedIndices.Count == 1) { int index = listView.SelectedIndices[0]; if (index >= (isUp ? 1 : 0) && index < (isUp ? FormMain.arrSerialWriteConfig.Length : FormMain.arrSerialWriteConfig.Length - 1)) { List <SerialWriteConfig> listSerialWriteConfigs = FormMain.arrSerialWriteConfig.ToList(); SerialWriteConfig tmp = listSerialWriteConfigs[index]; listSerialWriteConfigs[index] = listSerialWriteConfigs[index - (isUp ? 1 : -1)]; listSerialWriteConfigs[index - (isUp ? 1 : -1)] = tmp; FormMain.arrSerialWriteConfig = listSerialWriteConfigs.ToArray(); Json.WriteFile(SerialWriteConfig.Path, FormMain.arrSerialWriteConfig); InitSerialWriteUI(); InitListViewWriteConfig(listView); FormMain.InitSerialWriteConfig(); } } }
/// <summary> /// 选中快捷数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cbSerialWrite_SelectionChangeCommitted(object sender, EventArgs e) { ComboBox cbSerialWrite = (ComboBox)sender; TextBox txtSerialWrite = (cbSerialWrite.Tag.ToString() == "1") ? txtSerialWrite1 : txtSerialWrite2; ComboBox cbWriteFormat = (cbSerialWrite.Tag.ToString() == "1") ? cbWriteFormat1 : cbWriteFormat2; CheckBox chkSerialWriteLoop = (cbSerialWrite.Tag.ToString() == "1") ? chkSerialWriteLoop1 : chkSerialWriteLoop2; NumericUpDown numSerialWriteInterval = (cbSerialWrite.Tag.ToString() == "1") ? numSerialWriteInterval1 : numSerialWriteInterval2; if (cbSerialWrite.SelectedIndex > 0) { SerialWriteConfig serialWriteConfig = arrSerialWriteConfig[cbSerialWrite.SelectedIndex - 1]; txtSerialWrite.Text = serialWriteConfig.Data; SerialFormatToCombo(cbWriteFormat, serialWriteConfig.Format); numSerialWriteInterval.Value = Convert.ToDecimal(serialWriteConfig.Timer); chkSerialWriteLoop.Checked = serialWriteConfig.IsTimer; } else { chkSerialWriteLoop.Checked = false; } }