Home > Net >  For help on how to realize the global control
For help on how to realize the global control

Time:03-14


The code shown in the following two forms
 
//From1
Public static TextBox textbox1=new TextBox ();

Private void From1_Load (object sender, EventArgs e)
{
Textbox1. Text="aaa";
PnlRight. Controls. The Add (textbox1);
}

//From2
Private void From2_Load (object sender, EventArgs e)
{
Textbox textbox2=From1. Textbox1;
//textbox2. Text="BBB"
//messagebox. Show (textbox2. Text + "\ r \ n" + From1 textbox1. The text). This pop-up is BBB
PnlRight. Controls. The Add (textbox2);
}

//Form2 return to Form1, textbox1 be closed because From2, was released,
Private void From2_FormClosing (object sender, FormClosingEventArgs e)
{
The Form FM1=Application. OpenForms (" From1 ");
If (FM1!=null)
FM1. The Show ();
}


Form2 return to Form1, textbox1 because From2 closed, and was released,
Lead to Form1, click on the button to open the Form2 direct error
Solution of how to solve

CodePudding user response:

What the hell is this code

CodePudding user response:

TextBox textbox2=Form1. Textbox1;

Although it can compile, but there is no sense in, after all, a TextBox is visual controls,
If you think is the purpose of the textbox1 assigned to textbox2, that is about to use OOP concept of object replication, overloading=operator, or reload the Assign () method, namely:
TextBox textbox2=new TextBox ();
Textbox2. Assign (textbox1);

Like this,

CodePudding user response:

Can also be overloaded constructor directly:
The class MyTextBox: TextBox
{
Public MyTextBox (TextBox)
{
.
}
}

MyTextBox textbox2=new MyTextBox (textbox1);

All in all, object instances are not simply use=to assignment,
  •  Tags:  
  • C#
  • Related