public ref class Form : System::Windows::Forms::ContainerControl
public class Form : System.Windows.Forms.ContainerControl
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
public class Form : System.Windows.Forms.ContainerControl
type Form = class
inherit ContainerControl
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Form = class
inherit ContainerControl
Public Class Form
Inherits ContainerControl
以下示例创建 的新实例
Form
,并调用
ShowDialog
方法将窗体显示为对话框。 该示例设置
FormBorderStyle
、
AcceptButton
、
CancelButton
、
MinimizeBox
、
MaximizeBox
和
StartPosition
属性,以将窗体的外观和功能更改为对话框。 该示例还使用
Add
窗体集合的
Controls
方法添加两个
Button
控件。 该示例使用
HelpButton
属性在对话框的标题栏中显示帮助按钮。
public:
void CreateMyForm()
// Create a new instance of the form.
Form^ form1 = gcnew Form;
// Create two buttons to use as the accept and cancel buttons.
Button^ button1 = gcnew Button;
Button^ button2 = gcnew Button;
// Set the text of button1 to "OK".
button1->Text = "OK";
// Set the position of the button on the form.
button1->Location = Point(10,10);
// Set the text of button2 to "Cancel".
button2->Text = "Cancel";
// Set the position of the button based on the location of button1.
button2->Location =
Point( button1->Left, button1->Height + button1->Top + 10 );
// Set the caption bar text of the form.
form1->Text = "My Dialog Box";
// Display a help button on the form.
form1->HelpButton = true;
// Define the border style of the form to a dialog box.
form1->FormBorderStyle = ::FormBorderStyle::FixedDialog;
// Set the MaximizeBox to false to remove the maximize box.
form1->MaximizeBox = false;
// Set the MinimizeBox to false to remove the minimize box.
form1->MinimizeBox = false;
// Set the accept button of the form to button1.
form1->AcceptButton = button1;
// Set the cancel button of the form to button2.
form1->CancelButton = button2;
// Set the start position of the form to the center of the screen.
form1->StartPosition = FormStartPosition::CenterScreen;
// Add button1 to the form.
form1->Controls->Add( button1 );
// Add button2 to the form.
form1->Controls->Add( button2 );
// Display the form as a modal dialog box.
form1->ShowDialog();
public void CreateMyForm()
// Create a new instance of the form.
Form form1 = new Form();
// Create two buttons to use as the accept and cancel buttons.
Button button1 = new Button ();
Button button2 = new Button ();
// Set the text of button1 to "OK".
button1.Text = "OK";
// Set the position of the button on the form.
button1.Location = new Point (10, 10);
// Set the text of button2 to "Cancel".
button2.Text = "Cancel";
// Set the position of the button based on the location of button1.
button2.Location
= new Point (button1.Left, button1.Height + button1.Top + 10);
// Set the caption bar text of the form.
form1.Text = "My Dialog Box";
// Display a help button on the form.
form1.HelpButton = true;
// Define the border style of the form to a dialog box.
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
// Set the MaximizeBox to false to remove the maximize box.
form1.MaximizeBox = false;
// Set the MinimizeBox to false to remove the minimize box.
form1.MinimizeBox = false;
// Set the accept button of the form to button1.
form1.AcceptButton = button1;
// Set the cancel button of the form to button2.
form1.CancelButton = button2;
// Set the start position of the form to the center of the screen.
form1.StartPosition = FormStartPosition.CenterScreen;
// Add button1 to the form.
form1.Controls.Add(button1);
// Add button2 to the form.
form1.Controls.Add(button2);
// Display the form as a modal dialog box.
form1.ShowDialog();
Public Sub CreateMyForm()
' Create a new instance of the form.
Dim form1 As New Form()
' Create two buttons to use as the accept and cancel buttons.
Dim button1 As New Button()
Dim button2 As New Button()
' Set the text of button1 to "OK".
button1.Text = "OK"
' Set the position of the button on the form.
button1.Location = New Point(10, 10)
' Set the text of button2 to "Cancel".
button2.Text = "Cancel"
' Set the position of the button based on the location of button1.
button2.Location = _
New Point(button1.Left, button1.Height + button1.Top + 10)
' Set the caption bar text of the form.
form1.Text = "My Dialog Box"
' Display a help button on the form.
form1.HelpButton = True
' Define the border style of the form to a dialog box.
form1.FormBorderStyle = FormBorderStyle.FixedDialog
' Set the MaximizeBox to false to remove the maximize box.
form1.MaximizeBox = False
' Set the MinimizeBox to false to remove the minimize box.
form1.MinimizeBox = False
' Set the accept button of the form to button1.
form1.AcceptButton = button1
' Set the cancel button of the form to button2.
form1.CancelButton = button2
' Set the start position of the form to the center of the screen.
form1.StartPosition = FormStartPosition.CenterScreen
' Add button1 to the form.
form1.Controls.Add(button1)
' Add button2 to the form.
form1.Controls.Add(button2)
' Display the form as a modal dialog box.
form1.ShowDialog()
End Sub
是
Form
应用程序中显示的任何窗口的表示形式。 类
Form
可用于创建标准窗口、工具窗口、无边框窗口和浮动窗口。 还可以使用
Form
类创建模式窗口,例如对话框。 MDI) 窗体 (多文档界面是一种特殊窗体,可以包含称为 MDI 子窗体的其他窗体。 通过将 属性设置为
IsMdiContainer
true
来创建 MDI 窗体。 通过将 属性设置为
MdiParent
将包含子窗体的 MDI 父窗体来创建 MDI 子窗体。
使用 类中
Form
可用的属性,可以确定要创建的窗口或对话框的外观、大小、颜色和窗口管理功能。 属性
Text
允许你在标题栏中指定窗口的标题。 和
Size
DesktopLocation
属性允许在显示窗口时定义窗口的大小和位置。 可以使用
ForeColor
color 属性更改放置在窗体上的所有控件的默认前景色。 使用
FormBorderStyle
、
MinimizeBox
和
MaximizeBox
属性可以控制窗体在运行时是最小化、最大化还是调整其大小。
除了属性,还可以使用 类的 方法来操作窗体。 例如,可以使用
ShowDialog
方法将窗体显示为模式对话框。 可以使用
SetDesktopLocation
方法将窗体放置在桌面上。
类的事件
Form
允许您响应对窗体上执行的操作。 可以使用
Activated
事件执行一些操作,例如在激活窗体时更新窗体控件中显示的数据。
通过在 类中放置一个名为
Main
的方法,可以将窗体用作应用程序中的起始类。
Main
在 方法中添加代码以创建和显示窗体。 还需要将 属性添加到
STAThread
Main
方法,以便窗体运行。 当起始窗体关闭时,应用程序也会关闭。
例如
Enabled
,如果在 Visual Studio 设计器 () Microsoft显示 之前将 属性
Form
false
设置为
Enabled
,则最小化、最大化、关闭和系统按钮将保持启用状态。 如果在 (可见之后
Form
设置为
Enabled
false
,例如,当加载事件) 发生时,按钮将被禁用。