C#CANoe功能测试一键操作设计方案

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace CXF.CanUISharp.OperatorInterface.CANoeMsgOnekeyV01
{
    public partial class MsgOneKeyUI : UserControl
    {
        // 声明设计器控件
        private ListView listViewVariables;
        private Button btnAdd;
        private Button btnDelete;
        private Button btn_DoSignal;
        private Button bt_Revoke;
        private Button bt_Redo;
        private Button btn_TestGetSysvar;
        private CheckBox checkBoxEnable;
        private CheckBox checkBoxEvent;
        private TextBox textBoxTitle;
        private TextBox textBoxSysvar;
        private ColumnHeader columnType;
        private ColumnHeader columnDescription;
        private ColumnHeader columnNamespace;
        private ColumnHeader columnName;
        private ColumnHeader columnSetValue;
        private ColumnHeader columnGetValue;
        private ColumnHeader columnMin;
        private ColumnHeader columnMax;
        private ColumnHeader columnResult;

        private readonly Model_Onekey _model;
        private readonly ListViewOneKeyDAL _dal;
        private bool _isUpdating = false;

        public MsgOneKeyUI(Model_Onekey model)
        {
            _model = model ?? new Model_Onekey();
            _dal = new ListViewOneKeyDAL(_model);
            
            InitializeComponent();
            InitializeUI();
            SetupDataBinding();
        }

        private void InitializeComponent()
        {
            // 创建控件并设置属性
            this.listViewVariables = new ListView();
            this.columnType = new ColumnHeader();
            this.columnDescription = new ColumnHeader();
            this.columnNamespace = new ColumnHeader();
            this.columnName = new ColumnHeader();
            this.columnSetValue = new ColumnHeader();
            this.columnGetValue = new ColumnHeader();
            this.columnMin = new ColumnHeader();
            this.columnMax = new ColumnHeader();
            this.columnResult = new ColumnHeader();
            this.btnAdd = new Button();
            this.btnDelete = new Button();
            this.btn_DoSignal = new Button();
            this.bt_Revoke = new Button();
            this.bt_Redo = new Button();
            this.btn_TestGetSysvar = new Button();
            this.checkBoxEnable = new CheckBox();
            this.checkBoxEvent = new CheckBox();
            this.textBoxTitle = new TextBox();
            this.textBoxSysvar = new TextBox();
            this.SuspendLayout();
            
            // listViewVariables
            this.listViewVariables.Columns.AddRange(new ColumnHeader[] {
                this.columnType,
                this.columnDescription,
                this.columnNamespace,
                this.columnName,
                this.columnSetValue,
                this.columnGetValue,
                this.columnMin,
                this.columnMax,
                this.columnResult
            });
            this.listViewVariables.FullRowSelect = true;
            this.listViewVariables.GridLines = true;
            this.listViewVariables.Location = new Point(10, 40);
            this.listViewVariables.Size = new Size(780, 200);
            this.listViewVariables.View = View.Details;
            
            // 列宽设置
            this.columnType.Width = 100;
            this.columnDescription.Width = 150;
            this.columnNamespace.Width = 150;
            this.columnName.Width = 150;
            this.columnSetValue.Width = 100;
            this.columnGetValue.Width = 100;
            this.columnMin.Width = 80;
            this.columnMax.Width = 80;
            this.columnResult.Width = 100;
            
            // 按钮
            this.btnAdd.Text = "Add";
            this.btnAdd.Location = new Point(10, 250);
            this.btnAdd.Size = new Size(75, 25);
            this.btnAdd.Click += new EventHandler(btnAdd_Click);
            
            this.btnDelete.Text = "Delete";
            this.btnDelete.Location = new Point(90, 250);
            this.btnDelete.Size = new Size(75, 25);
            this.btnDelete.Click += new EventHandler(btnDelete_Click);
            
            this.btn_DoSignal.Text = "Execute";
            this.btn_DoSignal.Location = new Point(10, 280);
            this.btn_DoSignal.Size = new Size(75, 25);
            this.btn_DoSignal.Click += new EventHandler(btn_DoSignal_Click);
            
            this.bt_Revoke.Text = "Undo";
            this.bt_Revoke.Location = new Point(90, 280);
            this.bt_Revoke.Size = new Size(75, 25);
            this.bt_Revoke.Click += new EventHandler(bt_Revoke_Click);
            
            this.bt_Redo.Text = "Redo";
            this.bt_Redo.Location = new Point(170, 280);
            this.bt_Redo.Size = new Size(75, 25);
            this.bt_Redo.Click += new EventHandler(bt_Redo_Click);
            
            this.btn_TestGetSysvar.Text = "Test";
            this.btn_TestGetSysvar.Location = new Point(250, 280);
            this.btn_TestGetSysvar.Size = new Size(75, 25);
            this.btn_TestGetSysvar.Click += new EventHandler(btn_TestGetSysvar_Click);
            
            // 复选框
            this.checkBoxEnable.Text = "Enable";
            this.checkBoxEnable.Location = new Point(10, 10);
            this.checkBoxEnable.Size = new Size(80, 20);
            this.checkBoxEnable.CheckedChanged += new EventHandler(checkBoxEnable_CheckedChanged);
            
            this.checkBoxEvent.Text = "Enable Events";
            this.checkBoxEvent.Location = new Point(100, 10);
            this.checkBoxEvent.Size = new Size(100, 20);
            
            // 文本框
            this.textBoxTitle.Location = new Point(210, 10);
            this.textBoxTitle.Size = new Size(200, 20);
            this.textBoxTitle.PlaceholderText = "Title";
            
            this.textBoxSysvar.Location = new Point(420, 10);
            this.textBoxSysvar.Size = new Size(200, 20);
            this.textBoxSysvar.PlaceholderText = "System Variable";
            this.textBoxSysvar.TextChanged += new EventHandler(textBoxSysvar_TextChanged);
            
            // 用户控件
            this.Size = new Size(800, 320);
            this.Controls.Add(listViewVariables);
            this.Controls.Add(btnAdd);
            this.Controls.Add(btnDelete);
            this.Controls.Add(btn_DoSignal);
            this.Controls.Add(bt_Revoke);
            this.Controls.Add(bt_Redo);
            this.Controls.Add(btn_TestGetSysvar);
            this.Controls.Add(checkBoxEnable);
            this.Controls.Add(checkBoxEvent);
            this.Controls.Add(textBoxTitle);
            this.Controls.Add(textBoxSysvar);
            this.ResumeLayout(true);
        }

        private void InitializeUI()
        {
            // 设置列标题
            columnType.Text = "Type";
            columnDescription.Text = "Description";
            columnNamespace.Text = "Namespace";
            columnName.Text = "Name";
            columnSetValue.Text = "Set Value";
            columnGetValue.Text = "Get Value";
            columnMin.Text = "Min";
            columnMax.Text = "Max";
            columnResult.Text = "Result";
            
            // 初始化按钮状态
            UpdateUndoRedoButtons();
        }

        private void SetupDataBinding()
        {
            // 绑定基础属性
            checkBoxEnable.DataBindings.Add("Checked", _model, "IsEnabled", false, DataSourceUpdateMode.OnPropertyChanged);
            checkBoxEvent.DataBindings.Add("Checked", _model, "IsCheckedEvent", false, DataSourceUpdateMode.OnPropertyChanged);
            textBoxTitle.DataBindings.Add("Text", _model, "Title", false, DataSourceUpdateMode.OnPropertyChanged);
            textBoxSysvar.DataBindings.Add("Text", _model, "SystemVariableName", false, DataSourceUpdateMode.OnPropertyChanged);
            
            // 绑定ListView
            _dal.VariableParameters.ListChanged += (s, e) => RefreshListView();
            RefreshListView();
        }

        private void RefreshListView()
        {
            listViewVariables.Items.Clear();
            foreach (var param in _dal.VariableParameters)
            {
                var item = new ListViewItem(param.variableParameterType.ToString());
                item.SubItems.Add(param.Description ?? "");
                item.SubItems.Add(param.Namespace ?? "");
                item.SubItems.Add(param.Name ?? "");
                item.SubItems.Add(param.SetValue?.ToString() ?? "");
                item.SubItems.Add(param.GetValue?.ToString() ?? "");
                item.SubItems.Add(param.Min?.ToString() ?? "");
                item.SubItems.Add(param.Max?.ToString() ?? "");
                item.SubItems.Add(param.Result?.ToString() ?? "");
                listViewVariables.Items.Add(item);
            }
        }

        // 单步执行按钮
        private void btn_DoSignal_Click(object sender, EventArgs e)
        {
            // 这里应该调用CANoe API执行操作
            MessageBox.Show("Signal execution started", "Information", 
                          MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // 撤销按钮
        private void bt_Revoke_Click(object sender, EventArgs e)
        {
            _dal.Undo();
            UpdateUndoRedoButtons();
            RefreshListView();
        }

        // 重做按钮
        private void bt_Redo_Click(object sender, EventArgs e)
        {
            _dal.Redo();
            UpdateUndoRedoButtons();
            RefreshListView();
        }

        private void UpdateUndoRedoButtons()
        {
            bt_Revoke.Enabled = _dal.CanUndo;
            bt_Redo.Enabled = _dal.CanRedo;
        }

        // 添加变量按钮
        private void btnAdd_Click(object sender, EventArgs e)
        {
            _dal.AddNewVariable();
        }

        // 删除变量按钮
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listViewVariables.SelectedIndices.Count > 0)
            {
                _dal.RemoveVariable(listViewVariables.SelectedIndices[0]);
            }
            else
            {
                MessageBox.Show("Please select a row to delete", "Information", 
                              MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        // 是否启用复选框
        private void checkBoxEnable_CheckedChanged(object sender, EventArgs e)
        {
            // 启用/禁用所有控件
            bool enabled = checkBoxEnable.Checked;
            listViewVariables.Enabled = enabled;
            btnAdd.Enabled = enabled;
            btnDelete.Enabled = enabled;
            btn_DoSignal.Enabled = enabled;
            checkBoxEvent.Enabled = enabled;
            textBoxTitle.Enabled = enabled;
            textBoxSysvar.Enabled = enabled;
            btn_TestGetSysvar.Enabled = enabled;
        }

        // 测试系统变量按钮
        private void btn_TestGetSysvar_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_model.SystemVariableName))
            {
                MessageBox.Show("System variable name is empty", "Error", 
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
            try
            {
                // 这里应该调用CANoe API测试变量
                // 模拟测试结果
                var random = new Random();
                bool success = random.Next(0, 100) > 30; // 70%成功率
                
                MessageBox.Show($"Testing system variable: {_model.SystemVariableName}\n" +
                               $"Result: {(success ? "SUCCESS" : "FAILURE")}", 
                               "Test Result", 
                               MessageBoxButtons.OK, 
                               success ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error testing system variable: {ex.Message}", "Error", 
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 系统变量名称改变事件
        private void textBoxSysvar_TextChanged(object sender, EventArgs e)
        {
            if (_isUpdating) return;
            
            if (!_dal.IsSystemVariableUnique(textBoxSysvar.Text))
            {
                MessageBox.Show("System variable name must be unique", "Validation Error", 
                               MessageBoxButtons.OK, MessageBoxIcon.Warning);
                _isUpdating = true;
                textBoxSysvar.Text = _model.SystemVariableName; // 恢复原值
                _isUpdating = false;
            }
        }

        // 添加类型编辑功能
        private void listViewVariables_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var hitTest = listViewVariables.HitTest(e.Location);
            if (hitTest.Item != null && hitTest.SubItem == hitTest.Item.SubItems[0])
            {
                // 创建下拉框编辑类型
                var comboBox = new ComboBox
                {
                    DropDownStyle = ComboBoxStyle.DropDownList,
                    DataSource = Enum.GetValues(typeof(VariableParameterType)),
                    Bounds = hitTest.SubItem.Bounds
                };
                
                comboBox.SelectedItem = _dal.VariableParameters[hitTest.Item.Index].variableParameterType;
                comboBox.SelectedIndexChanged += (s, args) =>
                {
                    _dal.VariableParameters[hitTest.Item.Index].variableParameterType = 
                        (VariableParameterType)comboBox.SelectedItem;
                    listViewVariables.Controls.Remove(comboBox);
                    RefreshListView();
                };
                
                comboBox.LostFocus += (s, args) => listViewVariables.Controls.Remove(comboBox);
                
                listViewVariables.Controls.Add(comboBox);
                comboBox.Focus();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

99乘法口诀万物皆可变

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值