Is there a way to put the same object into 2 forms?

And to organize access, for example, by a static link that will be created in a separate class, or something like that?

  • Yes, and you just described it. - Igor
  • @Igor if I write stupidly Form2.Controls.Add(this.textBox1); it will disappear from the main form and appear in the second one. - Verm ww
  • 2
    You see, your question has already begun to improve (just need to remove the word "stupid"). - Igor
  • 3
    The graphic component ( Control ) can only be on one form at a time. You are not on the wrong side. You need to create a model that contains some value and bind it (data binding) to two different controls on the forms. - Alexander Petrov
  • What kind of object? - D .Stark

1 answer 1

The graphic component ( Control ) can only be on one form at a time.

You are not on the wrong side. You need to create a model that contains some value and bind it (data binding) to two different controls on the forms.

Suppose there is the following model:

 class Foo { public string Bar { get; set; } } 

There are two forms. Each has its own TextBox . Let's call them, respectively, textBoxOnFormOne and textBoxOnFormTwo

On the first form, we create an object of our model class:

 foo = new Foo(); 

And we tie it to the textbox:

 textBoxOnFormOne.DataBindings.Add("Text", foo, "Bar", false, DataSourceUpdateMode.OnPropertyChanged); 

In one way or another, we pass the link to the object to the second form. And we also make a similar binding in it:

 textBoxOnFormTwo.DataBindings.Add("Text", foo, "Bar", false, DataSourceUpdateMode.OnPropertyChanged); 

Now, if you enter / change the value in the textbox on one form, it will automatically change to another.