How to put one WinForms user control on three forms and update all three at the same time?

How to create a usercontrol that

e.g. 3 winforms and 1 UserControl,

adding UserControl to all forms, but if the user clicked on one of the forms, changed the content of UserControl, all forms display the same changes.

Edited

I think it works now. the dark area is a usercontrol with some content alt text http://img28.imageshack.us/img28/6169/naamloostl.png

alt text http://img36.imageshack.us/img36/7362/naamloos1bt.png

+1
source share
4 answers

... too long for a comment ... might add sample code to this if OP requests ...

If "WinForms", add the "WinForms" tag to your tags.

You might want to clarify: are you looking for a solution that will "scale" to cover the case where you may have many UserControl controls (tens? Hundreds?) That you reuse in many Forms, all of which need to be synchronized :

If you are (looking for scaling), then I think you need to study "DataSource as Asad offers" (note that only certain WinForms controls, like ComboBox, have the DataSource property) DataBinding, as Jake suggests; both of these responses indicate the need to use a "higher level" for state synchronization.

For a “smaller scale” solution, you can add a Settings file and use it as a repository for current values.

In any case, you still need to raise an event ... each time you change the current UserControl that has focus ... to start updating controls in other instances of UserControl.

Or, depending on your design and requirements, if you can agree that each UserControl is not updated immediately: you can delay the launch of the update until the user switches to another form: then the previously active form will launch it "Deactivate the event : in this event, you can determine if the settings have changed (using some kind of dirty bit in UserControl?), and then update other UserControls as necessary: ​​suppose you really want an immediate update.

Another good thing, imho, to clarify in order to get the best answers here may be: exactly how these multiple Forms, each of which contains an instance of the same UserControl, are created: is there one “Main Form” that creates all the other instances of the Form : or this is the "SDI" model, where each form is created independently. In any of the Forms, the parent property is set to a non-zero value (i.e., Does any of the forms have a different form as a parent)?

If you need an example of working code for using an open static class to solve the "small scale" to your question "as asked": just ask here and I will send it: it will not be "elegant," but it will work :)

+2
source

save them by specifying the same DataSource

 public void UpdateControls() { List<string> filesDataSource = GetFiles(); // replace with your Datasource Control1.DataSource = filesDataSource ; Control2.DataSource = filesDataSource ; Control3.DataSource = filesDataSource ; Control1.DataBind(); Control2.DataBind(); Control3.DataBind(); } Control1_SelectedIndexChanged(.............) // Replace with your evnents { // // UpdateControls(); } 
+2
source

The best solution is a static class with an event handler and a public method.

 public static class Signal { public static EventHandler OnSignal {get;set} public static void SendSignal() { if (OnSignal !=null) OnSignal(this,new EventArgs()); } } 

In your winform:

 protected void Form1_load(object sender, EventArgs e) { Signal.OnSignal += new EventHandler(IGotSignal) } protected void IGotSignal(object sender, EventArgs e) { MessageBox.Show("i got a signal") } protected void button1_Click(object sender,EventArgs e) { Signal.SendSignal(); } 
+1
source

Each form will contain a unique instance of UserControl. You will have to use event / data binding so that changes in one form lead to changes in others.

0
source

Source: https://habr.com/ru/post/1303595/


All Articles