How to reduce code that changes the properties of numericUpDown?

private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked) { numericUpDownHPM1.Maximum = 1000; numericUpDownHPM1.Minimum = 1; numericUpDownHPM1.DecimalPlaces = 0; numericUpDownHPM1.Increment = 1; numericUpDownHPM1.Value = 100; label101.Visible = true; numericUpDownHPP1.Maximum = 1000; label102.Visible = true; numericUpDownMPM1.Maximum = 1000; numericUpDownMPM1.Minimum = 1; numericUpDownMPM1.DecimalPlaces = 0; numericUpDownMPM1.Increment = 1; numericUpDownMPM1.Value = 100; label103.Visible = true; numericUpDownMPP1.Maximum = 1000; label104.Visible = true; numericUpDownStrM1.Maximum = 1000; numericUpDownStrM1.Minimum = 1; numericUpDownStrM1.DecimalPlaces = 0; numericUpDownStrM1.Increment = 1; numericUpDownStrM1.Value = 100; label111.Visible = true; numericUpDownStrP1.Maximum = 1000; label112.Visible = true; numericUpDownDefM1.Maximum = 1000; numericUpDownDefM1.Minimum = 1; numericUpDownDefM1.DecimalPlaces = 0; numericUpDownDefM1.Increment = 1; numericUpDownDefM1.Value = 100; label113.Visible = true; numericUpDownDefP1.Maximum = 1000; label114.Visible = true; } else { numericUpDownHPM1.Maximum = 10; numericUpDownHPM1.Minimum = new decimal(new int[] { 1, 0, 0, 65536 }); numericUpDownHPM1.DecimalPlaces = 1; numericUpDownHPM1.Increment = new decimal(new int[] { 1, 0, 0, 65536}); numericUpDownHPM1.Value = 1; label101.Visible = false; numericUpDownHPP1.Maximum = 99999; label102.Visible = false; numericUpDownMPM1.Maximum = 10; numericUpDownMPM1.Minimum = new decimal(new int[] { 1, 0, 0, 65536 }); numericUpDownMPM1.DecimalPlaces = 1; numericUpDownMPM1.Increment = new decimal(new int[] { 1, 0, 0, 65536 }); numericUpDownMPM1.Value = 1; label103.Visible = false; numericUpDownMPP1.Maximum = 65535; label104.Visible = false; numericUpDownStrM1.Maximum = 10; numericUpDownStrM1.Minimum = new decimal(new int[] { 1, 0, 0, 65536 }); numericUpDownStrM1.DecimalPlaces = 1; numericUpDownStrM1.Increment = new decimal(new int[] { 1, 0, 0, 65536 }); numericUpDownStrM1.Value = 1; label111.Visible = false; numericUpDownStrP1.Maximum = 255; label112.Visible = false; numericUpDownDefM1.Maximum = 10; numericUpDownDefM1.Minimum = new decimal(new int[] { 1, 0, 0, 65536 }); numericUpDownDefM1.DecimalPlaces = 1; numericUpDownDefM1.Increment = new decimal(new int[] { 1, 0, 0, 65536 }); numericUpDownDefM1.Value = 1; label113.Visible = false; numericUpDownDefP1.Maximum = 255; label114.Visible = false; } } 

This is only part of the code. The code is 10 times larger, takes up a lot of space.

    2 answers 2

    At a minimum, you can put the setting in the method:

     public void SetUpElement(NumericUpDown item, int max, int min, int decPlaces, int increment, int value) { item.Maximum = max; item.Minimum = min; item.DecimalPlaces = decPlaces; item.Increment = increment; item.Value = value; } 

    Now instead of 6 lines for setting up each element there will be 1 method call. For example, for numericUpDownHPM1 :

     SetUpElement(numericUpDownHPM1, 1000, 1, 0, 1, 100); 

      Most correctly in this case to describe the model (in the case of MVC ) and bind the properties of the controls to it. You create a model once for each set of elements and initialize with all the necessary dependencies (n / a, the minimum and maximum values ​​for each of the options). When switching the Checked / Unchecked mode, you set the corresponding flag in the model and it starts to give the control new values .