private Control CreateControl(SettingModule module, PropertyInfo item) { var type = item.PropertyType; if (type == typeof(bool)) { return(RenderBool(module, item)); } if (type.IsEnum) { return(RenderEnum(module, item)); } if (type == typeof(int)) { return(RenderNumber(module, item, 'i')); } if (type == typeof(long)) { return(RenderNumber(module, item, 'l')); } return(RenderInput(module, item)); }
private Control RenderInput(SettingModule module, PropertyInfo item) { var attr = item.GetCustomAttribute <SettingItemAttribute>(); var name = attr.Name; var itemContainer = new Panel { AutoSize = true, Margin = new Padding(0, 10, 0, 10) }; var label = new Label { Text = name, Left = 0, Top = 0, AutoSize = true }; if (attr.RestartRequired) { label.ForeColor = Color.Red; } itemContainer.Controls.Add(label); var textbox = new TextBox { Text = item.GetValue(module).ToString(), Left = 0, Top = 16, Width = 400 }; itemContainer.Controls.Add(textbox); // 确定按钮 var btn = new Button { Text = "确定", Left = 405, Top = 16, Width = 40, FlatStyle = FlatStyle.Flat }; btn.Click += (sender, e) => { item.SetValue(module, textbox.Text); }; itemContainer.Controls.Add(btn); return(itemContainer); }
private Control RenderNumber(SettingModule module, PropertyInfo item, char type) { var attr = item.GetCustomAttribute <SettingItemAttribute>(); var name = attr.Name; var itemContainer = new Panel { AutoSize = true, Margin = new Padding(0, 10, 0, 10) }; var label = new Label { Text = name, Left = 0, Top = 0, AutoSize = true }; if (attr.RestartRequired) { label.ForeColor = Color.Red; } itemContainer.Controls.Add(label); var textbox = new NumericUpDown { Left = 0, Top = 16, Width = 400, Minimum = 0, Maximum = type == 'i' ? int.MaxValue : long.MaxValue }; textbox.Value = long.Parse(item.GetValue(module).ToString()); itemContainer.Controls.Add(textbox); textbox.ValueChanged += (sender, e) => { if (type == 'i') { item.SetValue(module, Convert.ToInt32(textbox.Value)); } else { item.SetValue(module, Convert.ToInt64(textbox.Value)); } }; return(itemContainer); }
private Control RenderBool(SettingModule module, PropertyInfo item) { var attr = item.GetCustomAttribute <SettingItemAttribute>(); var name = attr.Name; var checkbox = new CheckBox { Text = name, Checked = (bool)item.GetValue(module), TextAlign = ContentAlignment.MiddleCenter, AutoSize = true, FlatStyle = FlatStyle.Flat, Margin = new Padding(0, 5, 0, 5) }; if (attr.RestartRequired) { checkbox.ForeColor = Color.Red; } checkbox.CheckedChanged += (sender, e) => { item.SetValue(module, checkbox.Checked); }; return(checkbox); }
private Control RenderEnum(SettingModule module, PropertyInfo item) { var attr = item.GetCustomAttribute <SettingItemAttribute>(); var name = attr.Name; var itemContainer = new Panel { AutoSize = true, Margin = new Padding(0, 10, 0, 10) }; var label = new Label { Text = name, Left = 0, Top = 0, AutoSize = true }; if (attr.RestartRequired) { label.ForeColor = Color.Red; } itemContainer.Controls.Add(label); var enumItems = item.PropertyType.GetFields() .Where(field => field.Name != "value__") .Select(field => { var desc = field.GetCustomAttribute <EnumDescriptionAttribute>(); desc.Value = field.GetValue(item).ToString(); return(desc); }).ToArray(); var dropdown = new ComboBox { Left = 0, Top = 16, MinimumSize = new Size(400, 0), DropDownStyle = ComboBoxStyle.DropDownList, ValueMember = "Value", DisplayMember = "Description" }; dropdown.Items.AddRange(enumItems); var val = item.GetValue(module).ToString(); dropdown.SelectedItem = enumItems.First(i => i.Value == val); // 鼠标离开时,使其推动焦点,以避免选项被意外选择 dropdown.MouseLeave += (sender, e) => { dropdown.Parent.Focus(); }; itemContainer.Controls.Add(dropdown); dropdown.SelectedIndexChanged += (sender, e) => { item.SetValue(module, Enum.Parse(item.PropertyType, ((EnumDescriptionAttribute)dropdown.SelectedItem).Value)); }; return(itemContainer); }