After the mode form (ShowDialog) is opened, no operation can be performed on the following main form.
In this blog, we want to implement non-modal forms (show) to achieve some effects of the mode form (the main form is closed, and the sub form is also closed. Only one subform can be opened .)
At the same time, some features of the non-modal form are retained (both the main form and sub-form can be operated, such as text writing ).
Practical Use:
1. For example, if you want to write existing content in the main form, use the subform to display the existing content (generally there are many information, and you cannot use a space in the main form to display the content completely) for reference.
2. Click the information in the subform and display it in the main form.
And so on.
The Code is as follows:
FormA ---- main form
FormB ---- subform
View plaincopy to clipboardprint?
··· · 50 ······· · 90 ····· · 140 · 150
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Namespace BasicWindowsApplication
{
Public partial class FormA: Form
{
FormB formB = null;
Public FormA ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
If (formB = null)
{
FormB = new FormB (this); // this step is important
FormB. CloseFrm + = new EventHandler (frmA_CloseFrm );
FormB. Show ();
}
Else
{
FormB. TopMost = true;
}
}
/// <Summary>
/// Form close event
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Public void frmA_CloseFrm (object sender, EventArgs e)
{
FormB = null;
}
/// <Summary>
/// Form close event
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void FrmA_FormClosed (object sender, FormClosedEventArgs e)
{
If (formB! = Null)
{
FormB. Dispose ();
}
}
}
}
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Namespace BasicWindowsApplication
{
Public partial class FormA: Form
{
FormB formB = null;
Public FormA ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
If (formB = null)
{
FormB = new FormB (this); // this step is important
FormB. CloseFrm + = new EventHandler (frmA_CloseFrm );
FormB. Show ();
}
Else
{
FormB. TopMost = true;
}
& Nb