0

As a part of my main project, I need to display a simple on/off switch on multiple displays while the status of switches must be synced and I can click either one to switch.

Referred to this questions, it displays windows on "slave" screens by taking a snapshot of the "master" window. It is not an option for me as I won't able to operate on "slave" windows.

Referred to this question suggests to create multiple instances of forms across screens, it again will not able to sync the status of the button.

It seems the second example would be more applicable to my requirement, as the only thing I need to do is to synchronize the status of the on/off switch. If I am correct, I, however, don't know how to pass the parameter between instances or how to raise events and add the event listener?

A final note is that I need it working with at least 3 displays, so would the event need to be something like multi-cast?

Update: All forms are within the same application. More detail: I have one MainForm on one of the displays that shows most information and need other forms on all displays to operate as a switch. The status of switch will be send back to the MainForm and synchronised on all SwitchForm.

Thanks!

Tide Gu
  • 815
  • 1
  • 7
  • 21
  • Are these forms part of the same application? – Zalomon May 23 '17 at 10:15
  • @Zalomon Yes. More detail: I have one `MainForm` on one of the displays that shows most information and need other forms on all displays to operate as a switch. The status of switch will be send back to the `MainForm` and synchronised on all `SwitchForm`s. – Tide Gu May 23 '17 at 10:22

2 Answers2

1

I'll have a controlling class (could be MainForm but I'd advice against it) to instantiate and capture the events on SwitchForm; you can create specific events to pass all the information you need (such as new values for every control) and create public methods on SwitchForm and MainForm to set the new value.

Zalomon
  • 553
  • 4
  • 14
  • Thanks a lot. Further questions: 1) Why against? SwitchForm will be created by MainForm anyway, if it's not against the design pattern. 2) How to register events, raise and handle them? Would you please give an example or some links? Thanks! – Tide Gu May 23 '17 at 10:36
  • 1
    1) I'd advice against using MainForm because that logic should be handled by an external class, not a big deal but as rule of thumb I like my forms as stupid as possible. 2) https://stackoverflow.com/questions/977326/propagating-events-from-one-form-to-another-form-in-c-sharp – Zalomon May 23 '17 at 12:10
  • Thanks. It's very helpful. Now I need to find a way to make bi-directional event handler :) – Tide Gu May 23 '17 at 13:20
0

Thanks to @Zalomon's suggestion. I finally found a solution:

MainForm:

public partial class FormMain : Form {
    public FormMain() {
        InitializeComponent();
    }

    private void SwitchEventHandler(object sender, FormSwitch.SwitchEventArgs e) {
        Debug.Print($"Event {((Form)sender).Bounds}");
    }

    private void button1_Click(object sender, EventArgs e) {
        FormSwitch sw = new FormSwitch("1");
        sw.Show();
        FormSwitch sw2 = new FormSwitch("2");
        sw2.Show();
        sw.SwitchEvent += SwitchEventHandler;
        sw.SwitchEvent += sw.SwitchEventHandler;
        sw.SwitchEvent += sw2.SwitchEventHandler;
        sw2.SwitchEvent += SwitchEventHandler;
        sw2.SwitchEvent += sw.SwitchEventHandler;
        sw2.SwitchEvent += sw2.SwitchEventHandler;
    }
}

SwitchForm:

public partial class FormSwitch : Form {

    public class SwitchEventArgs : EventArgs {
        public bool Status { get; set; }
        public SwitchEventArgs(bool status) { Status = status; }
    }

    public event EventHandler<SwitchEventArgs> SwitchEvent;

    private bool _status;
    private string _name;

    public FormSwitch(string name) {
        _name = name;
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
        SwitchEvent(this, new SwitchEventArgs(!_status));
    }

    public void SwitchEventHandler(object sender, SwitchEventArgs e) {
        _status = e.Status;
        Debug.Print($"{_name}: {Bounds} {((Form)sender).Bounds} {_status}");
    }
}

Please let me know how do you think - It works but if there's anything I did wrong?

I will accept my own answer if it is publically acceptable.

Credits goes to @Zalomon and thanks again!

Tide Gu
  • 815
  • 1
  • 7
  • 21