0

Is het possible to have one instance of a usercontrol running on two different forms?


I did the following and it did not work (the controls only shows up at the last form).

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var control = new UserControl1();

            var form2 = new Form2();
            form2.UserControl1 = control;
            form2.Show();

            var form1 = new Form1();
            form1.UserControl1 = control;

            Application.Run(form1);
        }
4

1 に答える 1

1

私はノーと言わなければならないでしょう。少なくとも通常の.Net操作では。テキストボックスを作成してform1に追加し、次に同じテキストボックスインスタンスをform2に追加すると、form1のテキストボックスは文字通りform2に移動します。それが機能したとしても、場所、親フォームなどのプロパティは、2つのフォームの間で中断されます。

共通のインスタンスデータが必要な場合は、クラスシングルトンが適している可能性があります。

于 2012-12-15T20:17:56.717 に答える