Если задавать прогрессию с помощью ValueChanged
Цитата(sulyco @ 18.4.2014, 10:41 ) | | При наличии одного контрола на форме вполне терпимо. |
Тогда можете попробовать такой вариант
| Код | public Form1() { InitializeComponent();
//После инициализации компонентов назначаем событию ValueChanged всех NumericUpDown на форме один и тот же обработчик ProgressiveValueChanged, а так же исполняем этот обработчик для начальной установки необходимых параметров foreach (Control control in this.Controls) if (control is NumericUpDown) { (control as NumericUpDown).ValueChanged += ProgressiveValueChanged; ProgressiveValueChanged(control, null); } }
private void ProgressiveValueChanged(object sender, EventArgs e) { (sender as NumericUpDown).Maximum = 1000; (sender as NumericUpDown).Minimum = 1; if ((sender as NumericUpDown).Value < 2) { (sender as NumericUpDown).Increment = 0.01M; (sender as NumericUpDown).DecimalPlaces = 2; } if ((sender as NumericUpDown).Value >= 2 && (sender as NumericUpDown).Value < 3) { (sender as NumericUpDown).Increment = 0.02M; (sender as NumericUpDown).DecimalPlaces = 2; } if ((sender as NumericUpDown).Value >= 3 && (sender as NumericUpDown).Value < 4) { (sender as NumericUpDown).Increment = 0.05M; (sender as NumericUpDown).DecimalPlaces = 2; } if ((sender as NumericUpDown).Value >= 4 && (sender as NumericUpDown).Value < 6) { (sender as NumericUpDown).Increment = 0.1M; (sender as NumericUpDown).DecimalPlaces = 1; } if ((sender as NumericUpDown).Value >= 6 && (sender as NumericUpDown).Value < 10) { (sender as NumericUpDown).Increment = 0.2M; (sender as NumericUpDown).DecimalPlaces = 1; } if ((sender as NumericUpDown).Value >= 10 && (sender as NumericUpDown).Value < 20) { (sender as NumericUpDown).Increment = 0.5M; (sender as NumericUpDown).DecimalPlaces = 1; } if ((sender as NumericUpDown).Value >= 20 && (sender as NumericUpDown).Value < 30) { (sender as NumericUpDown).Increment = 1; (sender as NumericUpDown).DecimalPlaces = 0; } if ((sender as NumericUpDown).Value >= 30 && (sender as NumericUpDown).Value < 50) { (sender as NumericUpDown).Increment = 2; (sender as NumericUpDown).DecimalPlaces = 0; } if ((sender as NumericUpDown).Value >= 50 && (sender as NumericUpDown).Value < 100) { (sender as NumericUpDown).Increment = 5; (sender as NumericUpDown).DecimalPlaces = 0; } }
|
Если все же
Цитата(sulyco @ 18.4.2014, 10:41 ) | | Решил создать пользовательский |
Тогда можно сделать приблизительно такого вида UserControl
| Код | public partial class ProgressiveNumericUpDown : UserControl { //Пользовательский ProgressiveNumericUpDown будет базироваться на этом контроле private NumericUpDown numericUpDown1;
public ProgressiveNumericUpDown() { InitializeComponent(); this.Size = new Size(120, 20);
//Создаем базовый компонент NumericUpDown, добавляем его в наш контрол и растягиваем его там numericUpDown1 = new NumericUpDown(); this.Controls.Add(numericUpDown1); numericUpDown1.Dock = DockStyle.Fill; //Присваиваем событию ValueChanged базового контрола наш механизм прогрессии и сразу же производим начальные установки numericUpDown1.ValueChanged += ProgressiveValueChanged; ProgressiveValueChanged(null, null); }
private void ProgressiveValueChanged(object sender, EventArgs e) { numericUpDown1.Maximum = 1000; numericUpDown1.Minimum = 1; if (numericUpDown1.Value < 2) { numericUpDown1.Increment = 0.01M; numericUpDown1.DecimalPlaces = 2; } if (numericUpDown1.Value >= 2 && numericUpDown1.Value < 3) { numericUpDown1.Increment = 0.02M; numericUpDown1.DecimalPlaces = 2; } if (numericUpDown1.Value >= 3 && numericUpDown1.Value < 4) { numericUpDown1.Increment = 0.05M; numericUpDown1.DecimalPlaces = 2; } if (numericUpDown1.Value >= 4 && numericUpDown1.Value < 6) { numericUpDown1.Increment = 0.1M; numericUpDown1.DecimalPlaces = 1; } if (numericUpDown1.Value >= 6 && numericUpDown1.Value < 10) { numericUpDown1.Increment = 0.2M; numericUpDown1.DecimalPlaces = 1; } if (numericUpDown1.Value >= 10 && numericUpDown1.Value < 20) { numericUpDown1.Increment = 0.5M; numericUpDown1.DecimalPlaces = 1; } if (numericUpDown1.Value >= 20 && numericUpDown1.Value < 30) { numericUpDown1.Increment = 1; numericUpDown1.DecimalPlaces = 0; } if (numericUpDown1.Value >= 30 && numericUpDown1.Value < 50) { numericUpDown1.Increment = 2; numericUpDown1.DecimalPlaces = 0; } if (numericUpDown1.Value >= 50 && numericUpDown1.Value < 100) { numericUpDown1.Increment = 5; numericUpDown1.DecimalPlaces = 0; } //Если пользователь назначил на событие ProgressiveNumericUpDown.ValueChanged свой обработчик, то самое время вызвать его if (ValueChanged != null) ValueChanged(null, null); }
//Расшариваем свойство Minimum базового контрола public decimal Minimum { get { return numericUpDown1.Minimum; } set { numericUpDown1.Minimum = value; } }
//Расшариваем свойство Maximum базового контрола public decimal Maximum { get { return numericUpDown1.Maximum; } set { numericUpDown1.Maximum = value; } }
//Расшариваем свойство Increment базового контрола public decimal Increment { get { return numericUpDown1.Increment; } set { numericUpDown1.Increment = value; } }
//Расшариваем свойство Value базового контрола public decimal Value { get { return numericUpDown1.Value; } set { numericUpDown1.Value = value; } }
//Объявляем событие ValueChanged для компонента ProgressiveNumericUpDown, которое будет происходить при изменении свойства Value базового контрола после того, как будут применены наши правила прогрессии public event EventHandler ValueChanged; }
|
|