The first method of using delegation:
Method of the parent form:
[Csharp]
Private void constraintEnd_Click (object sender, EventArgs e)
{
// If (MessageBox. Show ("the current furnace data will be forcibly archived! "," Warning ", MessageBoxButtons. OKCancel) = DialogResult. OK) // 2012.4.11 modify
//{
CCMEnd ccmend = new CCMEnd (curHeatInfo. Rows [0] ["heatid"]. ToString (), label337.Text. ToString (). Trim ());
Ccmend. resultEvent + = new CCMEnd. CalculateDelegate (Endccm );
Ccmend. Show (this );
}
Public void Endccm (string result)
{
CheckProcStatus ();
If (result. Equals ("1 "))
{
// MessageBox. Show ("ARCHIVE successful! "," Warning "); // 2012.4.11 modify
MessageBoxTimeout. Show ("ARCHIVE successful! "," Warning ", 4000 );
}
Else
{
// MessageBox. Show (result, "warning ");
MessageBoxTimeout. Show (result, "alarm", 4000 );
}
//}
}
Subform method:
[Csharp]
Public delegate void CalculateDelegate (string result );
Public event CalculateDelegate resultEvent;
Private void button#click (object sender, EventArgs e)
{
If (! String. IsNullOrEmpty (textBox4.Text )&&! String. IsNullOrEmpty (textBox5.Text ))
{
QT_Service.QT_Service qtService = new UI. QT_Service.QT_Service ();
Int cut_head = Int16.Parse (textBox4.Text );
Int cut_trimmed = Int16.Parse (textBox5.Text );
Result = qtService. EndEAFHeatID (4, string. Format ("{0: yyyyMMddHHmmss}", DateTime. Now), cut_head, cut_trimmed );
// CalculateDelegate calculate = new CalculateDelegate (CCMMonitor) this. Owner). Endccm );
// Calculate (result );
This. Close ();
ResultEvent (result );
}
Else
{
MessageBox. Show ("incorrect input. Please try again! "," Warning ");
}
}
Method 2:
Parent window:
[Csharp]
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
Form2 frm = new Form2 (this, textBox1.Text); // construct Form2 with parameters (this method requires redundant construction of two parameters)
If (frm. ShowDialog () = DialogResult. OK)
{
This. textBox1.Text + = frm. ReturnText; // obtain the return value through the Form2 attribute
}
Frm = null;
}
Subwindow:
[Csharp]
Public partial class Form2: Form
{
Private Form1 pfrm;
Private string frm1txt;
Public Form2 (Form1 parentfrm, string frm1text)
{
InitializeComponent ();
Pfrm = parentfrm;
Frm1txt = frm1text;
}
Public string ReturnText // attributes of the return value
{
Get {return this. frm1txt ;}
}
Private void button2_Click (object sender, EventArgs e) // assign a value to the attribute and disable the mode form Form2
{
Frm1txt = this. textBox2.Text;
This. DialogResult = DialogResult. OK; // * after the required mode form is opened, the operation can continue only after the DialogResult is initialized. Otherwise, the value cannot be returned.
This. Close (); // mode when the form is closed. close () is equivalent to hiding the form and closing it completely. Dispose () is required ()
}
Private void button3_Click (object sender, EventArgs e) // close
{
This. DialogResult = DialogResult. Cancel; // same *
This. Close ();
}
[Csharp]
The third method is to pass the object in the parent window as a parameter to the Child Window.