F
F
FAwafawf12122021-06-22 11:44:21
C++ / C#
FAwafawf1212, 2021-06-22 11:44:21

How to change checkbox value from another window?

How can I change the value of a checkbox from another window? there are 2 windows, in one checkbox with a method in the code where the checkbox value can change, and in the other window a method that causes the method to fire in order to change the value. I do not understand how you can call a method from another window?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Bereznikov, 2021-06-22
@FAwafawf1212

As always, there is a simple and correct way to solve the problem. The
simple method is to simply call the method of this window. You display it somewhere, right? Get a field with the type of your window with the method, where you show the window - save it there. Change the checkbox - access the field, call the method. Something like this (the code did not run, somewhere it may be necessary to write a little differently):

public class MainWindow : Window
{
    private WindowWithMetod _windowWithMetod;
    
    // ........................................................................................

    private void DisplaySecondWindow()
    {
        _windowWithMethod = new WindowWithMethod;
        _windowWithMethod.Closed += (s, e) => _windowWithMethod = null;
        _windowWithMethod.Show();
    }

    private void HandleCheckboxValueChanged(object sender, EventArgs args)
    {
        _windowWithMethod?.Method();
    }
}

The correct method is to think before you write something. Look, you have some state of something expressed by a checkbox, which means it makes sense to keep that something separate from both forms. We need:
  1. Set and store some boolean value
  2. Be able to respond to change

Let's define the interfaces with which it would be convenient for us to deal (it is better to give names that are more meaningful):
public interface IBooleanState
{
    public bool State { get; set; }
}

public interface IBooleanStateEvents
{
    public event EventArgs<bool> StateChanged;
}

If you don't have a generic version of EventArgs, get this one:
public sealed class EventArgs<T> : EventArgs
{
    public EventArgs(T value)
    {
        Value = value;
    }

    public T Value { get; }
}

Next, we implement our interfaces, it will be more convenient to do this in one class:
public sealed class BooleanState : IBooleanState, IBooleanStateEvents
{
    private bool _state;

    public event EventArgs<bool> StateChanged;

    public bool State
    {
        get => _state;
        set
        {
            if (value == _state)
            {
                return;
            }
            
            _state = value;

           StateChanged?.Invoke(this, new EventArgs(value));
        }
    }
}

Next, we need to somehow access all this goodness from our forms. Change constructors and add fields:
public class MainWindow : Window
{
    private readonly IBooleanState _state;
    // ..................
    public MainWindow(IBooleanState state)
    {
        _state = state;
    }
}

public class WindowWithMethod : Window
{
    private readonly _stateEvents;

    public WindowWithMethod(IBooleanStateEvents stateEvents)
    {
        _stateEvents = stateEvents;
    }
}

When we create windows, we pass an instance of our BooleanState there:
// Наверное где-то в Program.cs 
private readonly BooleanState _state = new BooleanState();
//................................................
Application.Run(new MainWindow(_state));
//................................................

For the second window, it all depends on where you create it, if you also pass it from Program, then you pass it in the same way, if from MainWindow, you have to cast:
new WindowWithMethod((IBooleanStateEvents)_state);
Now the matter remains small, you need to change the value from MainWindow, and react to it in WindowWithMethod:
public class MainWindow : Window
{
    private void HandleCheckboxValueChanged(object sender, EventArgs args)
    {
        _state.State = _checkBox.Checked;
    }
}

public class WindowWithMethod : Window
{
    // .................
    public WindowWithMethod(IBooleanStateEvents stateEvents)
    {
        // ........................
        _stateEvents = stateEvents;
        _stateEvents.StateChanged += HandleStateChanged;
       // .........................
    }

    private void HandleStateChanged(object sender, EventArgs<bool> args)
    {
        Method(args.State);
    }
}

This way you don't have to stick around with the public method from the second window, and there's a lot more room for further changes. If it's lazy, interfaces can be omitted and only the class can be used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question