Files
602_FD/LargeSquareOne/FrmChannelCfg.cs
T

137 lines
4.3 KiB
C#
Raw Normal View History

2026-08-10 16:05:06 +08:00
using JinYuan.Models;
using JinYuan.VirtualDataLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LargeSquareOne
{
public partial class FrmChannelCfg : Form
{
private ChannelConfig _config;
private Channel _currentChannel;
public FrmChannelCfg()
{
InitializeComponent();
LoadData();
}
private void LoadData()
{
_config = CommonMethods.LoadFromJson(CommonMethods.ChannelPath);
listBoxChannels.DataSource = _config.Channels;
listBoxChannels.DisplayMember = "AllContent";
}
// 选中通道时更新右侧详情
private void listBoxChannels_SelectedIndexChanged(object sender, EventArgs e)
{
_currentChannel = (Channel)listBoxChannels.SelectedItem;
if (_currentChannel != null)
{
numChannelId.Value = _currentChannel.ChannelId;
numDefaultValue.Value = _currentChannel.DefaultValue;
UpdateGearsList();
}
}
private void UpdateGearsList()
{
listBoxGears.DataSource = null;
listBoxGears.DataSource = _currentChannel.Gears;
}
private void UpdateChannelList()
{
listBoxChannels.DataSource = null;
listBoxChannels.DataSource = _config.Channels;
listBoxChannels.DisplayMember = "AllContent";
}
// 添加档位
private void btnAddGear_Click(object sender, EventArgs e)
{
var gearName = txtNewGear.Text.Trim();
if (!string.IsNullOrEmpty(gearName))
{
_currentChannel.Gears.Add(gearName);
txtNewGear.Text = "";
UpdateGearsList();
}
}
// 删除档位
private void btnRemoveGear_Click(object sender, EventArgs e)
{
if (listBoxGears.SelectedItem != null)
{
var gear = listBoxGears.SelectedItem.ToString();
_currentChannel.Gears.Remove(gear);
UpdateGearsList();
}
}
// 保存
private void btnSave_Click(object sender, EventArgs e)
{
2026-08-11 18:57:20 +08:00
var errList = _config.Channels.Where(x => x.AllContent.Contains("NG") && x.DefaultValue != 98).ToList();
if (errList.Count > 0)
2026-08-10 16:05:06 +08:00
{
2026-08-11 18:57:20 +08:00
var errArr = errList.Select(it => it.DefaultValue).ToArray();
MessageBox.Show($"NG写给PLC的值必须为98,不能为{string.Join(",", errArr)}");
return;
2026-08-10 16:05:06 +08:00
}
2026-08-11 18:57:20 +08:00
2026-08-10 16:05:06 +08:00
CommonMethods.SaveToJson(CommonMethods.ChannelPath, _config);
MessageBox.Show("保存成功!");
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void listBoxGears_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = listBoxGears.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
string oldValue = listBoxGears.Items[index].ToString();
FrmInput frm = new FrmInput(oldValue);
frm.ShowDialog();
string newValue = frm.CurGear;
if (!string.IsNullOrEmpty(newValue))
{
var gearIndex = _currentChannel.Gears.IndexOf(oldValue);
_currentChannel.Gears[gearIndex] = newValue; // 直接修改 Items
UpdateChannelList();
}
}
}
private void numDefaultValue_ValueChanged(object sender, EventArgs e)
{
_currentChannel.DefaultValue = (int)numDefaultValue.Value;
UpdateChannelList();
}
private void numDefaultValue_Validating(object sender, CancelEventArgs e)
{
2026-08-11 18:57:20 +08:00
//if ((int)numDefaultValue.Value == 98)
//{
// MessageBox.Show("98: 扫码NG(不允许在界面配置)");
// e.Cancel = true;
// return;
//}
2026-08-10 16:05:06 +08:00
}
}
}