views:

426

answers:

3

How to Create a UserControl which

for example 3 winforms and 1 UserControl,

adding UserControl to all forms but if user by click in one of the forms changed UserControl content, all forms display the same changes.

Edited

I think it working now. the dark area is the usercontrol with some content alt text

alt text

+2  A: 

keep them pointing to 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();
  }
Asad Butt
Sorry but I dont get it yet. keep what pointing to the same source. any working same sample well be great. thank you
Power-Mosfet
A: 

Each form will host a unique instance of the UserControl. You will have to use events / data binding to have changes in one form cause updates in the others.

Jake
+2  A: 

... too long for a comment ... may add a code example to this if the OP requests ...

If 'WinForms, please add a "WinForms" tag to your tags.

You might want to clarify : are you looking for a solution here that will "scale up" to cover a case where you might have many controls on the UserControl (tens ? hundreds ?) you are re-using on many Forms, all of which need to be synchronized :

If you are (looking for scaling-up), then I think you will need to explore 'DataSource as Asad suggests (note that only certain WinForms controls [like ComboBox, for example] have a 'DataSource property), and 'DataBinding as Jake suggests; both those answers are pointing you to the need for a "higher-level" approach to synchronizing state.

For a "smaller scale" solution, you might consider adding a "Settings" file, and using that as a repository for current values.

In any case, you'll still need to raise an event ... with every change in the current UserControl that has focus ... to trigger updating of the controls in the other UserControl instances.

Or, depending on your design and requirements, if you can accept that each UserControl is not updated immediately : you can defer triggering update until the user switches to another Form : then the previously active Form will trigger its 'Deactivate Event : in that Event you can detect whether settings have changed (using some kind of "dirty bit" in the UserControl ?), and then update the other UserControls as needed : my guess is you do want immediate updating, however.

Another good thing, imho, to clarify in order to get the best responses here might be : exactly how are these multiple Forms, each containing an instance of the same UserControl, created : is there one "Main Form" which is creating all the other Form instances : or is this an "SDI" model where each Form is created independently. Do any of the Forms have their Parent property set to a non-null value (i.e., do any of the Forms have another Form as Parent) ?

If you want a working code example of using a public static class for a "small scale" solution to your question "as asked" : just ask here, and I'll post one : it won't be "elegant," but it will work :)

BillW
Im no longer sure of which I want. perhaps I should example may situation a bit more. I have a main form(+ UserControls) which is hidden for a large part, user can access its content only for notify spot. so there for I need an second form which display whats happing inside the main form and if needed cancel/restart the progress. "small scale" sounds like an solution to me. it well be more than great if your post your sample.
Power-Mosfet
@Power-Mosfet It would help me (and others reading this, I think) if you describe exactly what you want to see. How many UserControls are visible to the user at any one time ? It would also help to know what type of Controls are in the UserControl : if they are Controls which have a 'DataSource property, then, as Asad, suggested above, you have built-in data-binding functionality. If the Controls have no 'DataSource, like a TextBox, or a CheckBox, for example, then you are in a real different place with trying to databind, and that's where code like what I'll post can come in handy.
BillW