예제 #1
0
        public static Enum GetEnumValue(this ComboBox cbo, Type enumType)
        {
            foreach (Enum value in Enum.GetValues(enumType))
            {
                if (ResourceHelper.GetEnumText(value) == cbo.SelectedItem.ToString())
                {
                    return(value);
                }
            }

            return(null);
        }
예제 #2
0
        public static T GetEnumValue <T>(this ComboBox cbo)
        {
            foreach (Enum value in Enum.GetValues(typeof(T)))
            {
                if (ResourceHelper.GetEnumText(value) == cbo.SelectedItem.ToString())
                {
                    return((T)(object)value);
                }
            }

            return(default(T));
        }
예제 #3
0
        public static void InitializeComboBox(ComboBox combo, Type enumType, Enum[] hiddenValues = null)
        {
            Enum selectedValue = combo.GetEnumValue(enumType);

            combo.DropDownStyle = ComboBoxStyle.DropDownList;
            combo.Items.Clear();
            foreach (Enum value in Enum.GetValues(enumType))
            {
                if (hiddenValues == null || Array.IndexOf(hiddenValues, value) < 0)
                {
                    combo.Items.Add(ResourceHelper.GetEnumText(value));
                }
            }

            if (selectedValue != null)
            {
                combo.SetEnumValue(selectedValue);
            }
        }
예제 #4
0
        public void UpdateUI()
        {
            this.Updating = true;

            foreach (KeyValuePair <string, Control> kvp in _bindings)
            {
                if (!_fieldInfo.ContainsKey(kvp.Key))
                {
                    throw new Exception("Invalid binding key");
                }
                else
                {
                    FieldInfoWrapper field  = _fieldInfo[kvp.Key];
                    eNumberFormat    format = _fieldFormat[kvp.Key];
                    object           value  = field.GetValue(this.Entity);
                    if (kvp.Value is TextBox)
                    {
                        if (value is IFormattable)
                        {
                            kvp.Value.Text = ((IFormattable)value).ToString(format == eNumberFormat.Decimal ? "" : "X", System.Globalization.CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            kvp.Value.Text = value == null ? "" : ((string)value).Replace(Environment.NewLine, "\n").Replace("\n", Environment.NewLine);
                        }
                    }
                    else if (kvp.Value is ctrlPathSelection)
                    {
                        kvp.Value.Text = (string)value;
                    }
                    else if (kvp.Value is CheckBox)
                    {
                        ((CheckBox)kvp.Value).Checked = Convert.ToBoolean(value);
                    }
                    else if (kvp.Value is ctrlRiskyOption)
                    {
                        ((ctrlRiskyOption)kvp.Value).Checked = Convert.ToBoolean(value);
                    }
                    else if (kvp.Value is RadioButton)
                    {
                        ((RadioButton)kvp.Value).Checked = (bool)value;
                    }
                    else if (kvp.Value is Panel)
                    {
                        RadioButton radio = kvp.Value.Controls.OfType <RadioButton>().FirstOrDefault(r => r.Tag.Equals(value));
                        if (radio != null)
                        {
                            radio.Checked = true;
                        }
                        else
                        {
                            throw new Exception("No radio button matching value found");
                        }
                    }
                    else if (kvp.Value is ctrlTrackbar)
                    {
                        if (field.FieldType == typeof(Int32))
                        {
                            ((ctrlTrackbar)kvp.Value).Value = (int)value;
                        }
                        else
                        {
                            ((ctrlTrackbar)kvp.Value).Value = (int)(uint)value;
                        }
                    }
                    else if (kvp.Value is ctrlHorizontalTrackbar)
                    {
                        ((ctrlHorizontalTrackbar)kvp.Value).Value = (int)value;
                    }
                    else if (kvp.Value is TrackBar)
                    {
                        if (field.FieldType == typeof(Int32))
                        {
                            ((TrackBar)kvp.Value).Value = (int)value;
                        }
                        else
                        {
                            ((TrackBar)kvp.Value).Value = (int)(uint)value;
                        }
                    }
                    else if (kvp.Value is MesenNumericUpDown)
                    {
                        MesenNumericUpDown nud = kvp.Value as MesenNumericUpDown;
                        decimal            val;
                        if (field.FieldType == typeof(UInt32))
                        {
                            val = (UInt32)value;
                        }
                        else if (field.FieldType == typeof(Int32))
                        {
                            val = (Int32)value;
                        }
                        else
                        {
                            val = (decimal)(double)value;
                        }
                        val       = Math.Min(Math.Max(val, nud.Minimum), nud.Maximum);
                        nud.Value = val;
                    }
                    else if (kvp.Value is ComboBox)
                    {
                        ComboBox combo = kvp.Value as ComboBox;
                        if (value is Enum)
                        {
                            combo.SelectedItem = ResourceHelper.GetEnumText((Enum)value);
                        }
                        else if (field.FieldType == typeof(UInt32))
                        {
                            for (int i = 0, len = combo.Items.Count; i < len; i++)
                            {
                                UInt32 numericValue;
                                string item = Regex.Replace(combo.Items[i].ToString(), "[^0-9]", "");
                                if (UInt32.TryParse(item, out numericValue))
                                {
                                    if (numericValue == (UInt32)value)
                                    {
                                        combo.SelectedIndex = i;
                                        break;
                                    }
                                }
                            }
                        }
                        else if (field.FieldType == typeof(string))
                        {
                            combo.SelectedItem = value;
                            if (combo.SelectedIndex < 0 && combo.Items.Count > 0)
                            {
                                combo.SelectedIndex = 0;
                            }
                        }
                    }
                }
            }

            this.Updating = false;
        }
예제 #5
0
        protected void UpdateUI()
        {
            this.Updating = true;

            foreach (KeyValuePair <string, Control> kvp in _bindings)
            {
                if (!_fieldInfo.ContainsKey(kvp.Key))
                {
                    throw new Exception("Invalid binding key");
                }
                else
                {
                    FieldInfo field = _fieldInfo[kvp.Key];
                    object    value = field.GetValue(this.Entity);
                    if (kvp.Value is TextBox)
                    {
                        if (field.FieldType == typeof(UInt32))
                        {
                            kvp.Value.Text = ((UInt32)value).ToString("X");
                        }
                        else if (field.FieldType == typeof(Byte))
                        {
                            kvp.Value.Text = ((Byte)value).ToString("X");
                        }
                        else
                        {
                            kvp.Value.Text = (string)value;
                        }
                    }
                    else if (kvp.Value is CheckBox)
                    {
                        ((CheckBox)kvp.Value).Checked = (bool)value;
                    }
                    else if (kvp.Value is RadioButton)
                    {
                        ((RadioButton)kvp.Value).Checked = (bool)value;
                    }
                    else if (kvp.Value is Panel)
                    {
                        RadioButton radio = kvp.Value.Controls.OfType <RadioButton>().FirstOrDefault(r => r.Tag.Equals(value));
                        if (radio != null)
                        {
                            radio.Checked = true;
                        }
                        else
                        {
                            throw new Exception("No radio button matching value found");
                        }
                    }
                    else if (kvp.Value is ctrlTrackbar)
                    {
                        ((ctrlTrackbar)kvp.Value).Value = (int)(uint)value;
                    }
                    else if (kvp.Value is ctrlHorizontalTrackbar)
                    {
                        ((ctrlHorizontalTrackbar)kvp.Value).Value = (int)value;
                    }
                    else if (kvp.Value is TrackBar)
                    {
                        if (field.FieldType == typeof(Int32))
                        {
                            ((TrackBar)kvp.Value).Value = (int)value;
                        }
                        else
                        {
                            ((TrackBar)kvp.Value).Value = (int)(uint)value;
                        }
                    }
                    else if (kvp.Value is NumericUpDown)
                    {
                        NumericUpDown nud = kvp.Value as NumericUpDown;
                        decimal       val;
                        if (field.FieldType == typeof(UInt32))
                        {
                            val = (UInt32)value;
                        }
                        else if (field.FieldType == typeof(Int32))
                        {
                            val = (Int32)value;
                        }
                        else
                        {
                            val = (decimal)(double)value;
                        }
                        val       = Math.Min(Math.Max(val, nud.Minimum), nud.Maximum);
                        nud.Value = val;
                    }
                    else if (kvp.Value is ComboBox)
                    {
                        ComboBox combo = kvp.Value as ComboBox;
                        if (value is Enum)
                        {
                            combo.SelectedItem = ResourceHelper.GetEnumText((Enum)value);
                        }
                        else if (field.FieldType == typeof(UInt32))
                        {
                            for (int i = 0, len = combo.Items.Count; i < len; i++)
                            {
                                UInt32 numericValue;
                                string item = Regex.Replace(combo.Items[i].ToString(), "[^0-9]", "");
                                if (UInt32.TryParse(item, out numericValue))
                                {
                                    if (numericValue == (UInt32)value)
                                    {
                                        combo.SelectedIndex = i;
                                        break;
                                    }
                                }
                            }
                        }
                        else if (field.FieldType == typeof(string))
                        {
                            combo.SelectedItem = value;
                            if (combo.SelectedIndex < 0 && combo.Items.Count > 0)
                            {
                                combo.SelectedIndex = 0;
                            }
                        }
                    }
                }
            }

            this.Updating = false;

            this.AfterUpdateUI();
        }