How to control a group of controls in C # (Windwos Forms)?

0

I would like to know how I can control each group of colors (R, G, B) by changing the value of a control such as ScrollBar and changing the value of Slider and DecimalUpDown. I have done it but it requires writing many lines of code and I would like to know if there is any way to optimize it.

Thanks!

    private void scR_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        slR.Value = scR.Value;
    }
    private void slR_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        scR.Value = slR.Value;
    }
    private void dcR_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {

    }

    #endregion
    #region GREEN
    private void dcG_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {

    }
    private void scG_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        slG.Value = scG.Value;
    }

    private void slG_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        scG.Value = slG.Value;
    }
    #endregion
    #region BLUE
    private void slB_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        scB.Value = slB.Value;
    }
    private void scB_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        slB.Value = scB.Value;
    }
    private void dcB_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {

    }
    #endregion
    
asked by Moha Mohito 09.11.2017 в 20:21
source

1 answer

0

The shortest way I know is to use the MVVM (Model-View-ViewModel) pattern, otherwise, at least in winforms, you will not be able to get rid of the lines of code.

Here is a small example, in case you are interested in implementing it:

You create a class that will contain the 4 values, it must implement the INotifyPropertyChanged interface:

public class ColorData : INotifyPropertyChanged
{
    private int a;
    public int A
    {
        get { return a; }
        set
        {
            a = value;
            PropertyChanged(new PropertyChangedEventArgs("A"));
        }
    }

    private int r;
    public int R
    {
        get { return r; }
        set
        {
            r = value;
            PropertyChanged(new PropertyChangedEventArgs("R"));
        }
    }

    private int g;
    public int G
    {
        get { return g; }
        set
        {
            g = value;
            PropertyChanged(new PropertyChangedEventArgs("G"));
        }
    }

    private int b;
    public int B
    {
        get { return b; }
        set
        {
            b = value;
            PropertyChanged(new PropertyChangedEventArgs("B"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void PropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChanged?.Invoke(this, e);
    }

}

And in the constructor of your form:     // You initialize a variable that will contain the data     ColorData colorData = new ColorData ();     // and to each control you make a binding of the property that you want to change automatically with the value that you have in the class     // for example, "Value" is the Value property of the slR, slG and slB controls,     slR.DataBindings.Add ("Value", colorData, "R");     slG.DataBindings.Add ("Value", colorData, "G");     slB.DataBindings.Add ("Value", colorData, "B");

// si quieres inicializar valores:  
colorData.A = 0; 
colorData.R = 10;
colorData.G = 10;
colorData.B = 10;

In WPF it is easier because there is a binding between controls (you assign the value of another control to a control and the changes are automatically reflected), if your application is not very large, equal and it is better for you to change to WPF.

    
answered by 11.11.2017 в 21:11