C3204
2025-12-18 d85cbf0ccd61d95b96695756e0c90db8b7679545
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace LB_SmartVision.Forms.Pages.SettingPage
{
    // 最优方案实现
    public class ControlStateManager
    {
        private readonly Dictionary<string, Control> _controlMap = new Dictionary<string, Control>();
        private readonly TableLayoutPanel _tablePanel;
 
        public ControlStateManager(TableLayoutPanel tablePanel)
        {
            _tablePanel = tablePanel;
            BuildControlMap();
        }
 
        // 初始化时构建控件字典(只需一次)
        private void BuildControlMap(Control parent = null)
        {
            parent = parent ?? _tablePanel;
 
            foreach (Control control in parent.Controls)
            {
                if (!string.IsNullOrEmpty(control.Name))
                {
                    _controlMap[control.Name] = control;
                }
 
                if (control.HasChildren)
                {
                    BuildControlMap(control);
                }
            }
        }
 
        // O(1) 快速访问
        public T GetControl<T>(string name) where T : Control
        {
            return _controlMap.TryGetValue(name, out Control control) ? control as T : null;
        }
 
        // 获取TextBox值
        public string GetTextBoxValue(string name)
        {
            return GetControl<TextBox>(name)?.Text;
        }
 
        // 获取CheckBox状态
        public bool? GetCheckBoxState(string name)
        {
            return GetControl<CheckBox>(name)?.Checked;
        }
 
        /// <summary>
        /// 获取 ComboBox 的选中项文本
        /// </summary>
        public string GetComboBoxText(string comboBoxName)
        {
            var comboBox = GetControl<ComboBox>(comboBoxName);
            return comboBox?.SelectedItem?.ToString() ?? string.Empty;
        }
 
        /// <summary>
        /// 获取 ComboBox 的选中索引
        /// </summary>
        public int GetComboBoxSelectedIndex(string comboBoxName)
        {
            var comboBox = GetControl<ComboBox>(comboBoxName);
            return comboBox?.SelectedIndex ?? -1;
        }
 
        /// <summary>
        /// 获取 ComboBox 的所有项
        /// </summary>
        public List<object> GetComboBoxItems(string comboBoxName)
        {
            var comboBox = GetControl<ComboBox>(comboBoxName);
            return comboBox?.Items.Cast<object>().ToList() ?? new List<object>();
        }
 
        /// <summary>
        /// 获取 ComboBox 的完整状态
        /// </summary>
        public ComboBoxState GetComboBoxState(string comboBoxName)
        {
            var comboBox = GetControl<ComboBox>(comboBoxName);
            if (comboBox == null) return null;
 
            return new ComboBoxState
            {
                Items = comboBox.Items.Cast<object>().ToList(),
                SelectedIndex = comboBox.SelectedIndex,
                SelectedItem = comboBox.SelectedItem,
                Text = comboBox.Text
            };
        }
 
        // 动态更新控件映射(当添加/删除控件时调用)
        public void RefreshControlMap()
        {
            _controlMap.Clear();
            BuildControlMap();
        }
    }
 
    // ComboBox 状态数据结构
    public class ComboBoxState
    {
        public List<object> Items { get; set; }
        public int SelectedIndex { get; set; }
        public object SelectedItem { get; set; }
        public string Text { get; set; }
    }
}