Form 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示组成应用程序的用户界面的窗口或对话框。
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窗体的外观和StartPosition功能更改为对话框的 、AcceptButtonCancelButton、、MinimizeBox和MaximizeBox属性。 该示例还使用 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
注解
A Form 是应用程序中显示的任何窗口的表示形式。 该 Form 类可用于创建标准、工具、无边框和浮动窗口。 还可以使用该 Form 类创建模式窗口,例如对话框。 一种特殊的窗体,多文档界面 (MDI) 窗体,可以包含称为 MDI 子窗体的其他窗体。 通过将属性true
设置为 IsMdiContainer .. 创建 MDI 窗体。 通过将属性设置为 MdiParent 包含子窗体的 MDI 父窗体来创建 MDI 子窗体。
使用类中 Form 可用的属性,可以确定要创建的窗口或对话框的外观、大小、颜色和窗口管理功能。 该 Text 属性允许你在标题栏中指定窗口的标题。 通过 Size 和 DesktopLocation 属性,可以在窗口显示时定义窗口的大小和位置。 可以使用 ForeColor 颜色属性更改窗体上放置的所有控件的默认前景颜色。 MinimizeBox属性FormBorderStyle和MaximizeBox属性允许你控制窗体是可以在运行时最小化、最大化还是调整其大小。
除了属性,还可以使用类的方法操作窗体。 例如,可以使用 ShowDialog 该方法将窗体显示为模式对话框。 可以使用该方法 SetDesktopLocation 将窗体放置在桌面上。
类的事件 Form 允许你响应窗体上执行的操作。 可以使用 Activated 该事件执行操作,例如在激活窗体时更新窗体控件中显示的数据。
可以通过在类中放置调用 Main
的方法,将窗体用作应用程序中的起始类。 在 Main
方法中添加用于创建和显示窗体的代码。 还需要将 STAThread
属性添加到 Main
方法中,以便窗体运行。 关闭起始窗体时,应用程序也会关闭。
例如,如果在可见 (之前Form将Enabled属性设置为 false
false,Enabled则 Microsoft Visual Studio 设计器) 中设置为 false,则保持启用最小化、最大化、关闭和系统按钮。 例如,如果设置为Enabledfalse
“加载”事件) 后Form可见 (,则禁用按钮。
构造函数
Form() |
初始化 Form 类的新实例。 |
字段
ScrollStateAutoScrolling |
确定 AutoScroll 属性的值。 (继承自 ScrollableControl) |
ScrollStateFullDrag |
确定用户是否启用了全窗口拖动。 (继承自 ScrollableControl) |
ScrollStateHScrollVisible |
确定 HScroll 属性的值是否设置为 |
ScrollStateUserHasScrolled |
确定用户是否滚动了 ScrollableControl 控件。 (继承自 ScrollableControl) |
ScrollStateVScrollVisible |
确定 VScroll 属性的值是否设置为 |
属性
AcceptButton |
获取或设置当用户按 Enter 键时所单击的窗体上的按钮。 |
AccessibilityObject |
获取分配给该控件的 AccessibleObject。 (继承自 Control) |
AccessibleDefaultActionDescription |
获取或设置控件的默认操作说明以供具有辅助功能的客户端应用程序使用。 (继承自 Control) |
AccessibleDescription |
获取或设置辅助功能客户端应用程序使用的控件说明。 (继承自 Control) |
AccessibleName |
获取或设置辅助功能客户端应用程序所使用的控件名称。 (继承自 Control) |
AccessibleRole |
获取或设置控件的辅助性角色。 (继承自 Control) |
ActiveControl |
获取或设置容器控件上的活动控件。 (继承自 ContainerControl) |
ActiveForm |
获取此应用程序的当前活动窗体。 |
ActiveMdiChild |
获取当前活动的多文档界面 (MDI) 子窗口。 |
AllowDrop |
获取或设置一个值,该值指示控件是否可以接受用户拖放到它上面的数据。 (继承自 Control) |
AllowTransparency |
获取或设置一个值,该值指示能否调整窗体的不透明度。 |
Anchor |
获取或设置控件绑定到的容器的边缘并确定控件如何随其父级一起调整大小。 (继承自 Control) |
AutoScale |
已过时。
已过时。
获取或设置一个值,该值指示窗体是否调整其大小以适合该窗体上使用的字体高度,以及是否缩放其控件。 |
AutoScaleBaseSize |
获取或设置用于自动缩放窗体的基大小。 |
AutoScaleDimensions |
获取或设置控件的设计尺寸。 (继承自 ContainerControl) |
AutoScaleFactor |
获取当前和设计时自动缩放尺寸之间的缩放因子。 (继承自 ContainerControl) |
AutoScaleMode |
获取或设置控件的自动缩放模式。 (继承自 ContainerControl) |
AutoScroll |
获取或设置一个值,该值指示窗体是否启用自动滚动。 |
AutoScrollMargin |
获取或设置自动滚动边距的大小。 (继承自 ScrollableControl) |
AutoScrollMinSize |
获取或设置自动滚动的最小尺寸。 (继承自 ScrollableControl) |
AutoScrollOffset |
获取或设置一个值,该值指示在 ScrollControlIntoView(Control) 中将控件滚动到何处。 (继承自 Control) |
AutoScrollPosition |
获取或设置自动滚动定位的位置。 (继承自 ScrollableControl) |
AutoSize |
根据 AutoSizeMode 的设置调整窗体的大小。 |
AutoSize |
此属性与此类无关。 (继承自 Control) |
AutoSizeMode |
获取或设置窗体自动调整自身大小的模式。 |
AutoValidate |
获取或设置一个值,该值指示当焦点更改时是否自动验证此容器内的控件。 |
AutoValidate |
获取或设置一个值,该值指示当焦点更改时是否自动验证此容器内的控件。 (继承自 ContainerControl) |
BackColor |
获取或设置控件的背景色。 |
BackgroundImage |
获取或设置在控件中显示的背景图像。 (继承自 Control) |
BackgroundImageLayout |
获取或设置在 ImageLayout 枚举中定义的背景图像布局。 (继承自 Control) |
BindingContext |
获取或设置控件的 BindingContext。 (继承自 ContainerControl) |
Bottom |
获取控件上边缘与其容器工作区的上边缘之间的距离(以像素为单位)。 (继承自 Control) |
Bounds |
获取或设置控件(包括其非工作区元素)相对于其父控件的大小和位置(以像素为单位)。 (继承自 Control) |
CancelButton |
获取或设置当用户按 Esc 键时单击的按钮控件。 |
CanEnableIme |
获取一个用以指示是否可以将 ImeMode 属性设置为活动值的值,以启用 IME 支持。 (继承自 ContainerControl) |
CanFocus |
获取一个值,该值指示控件是否可以接收焦点。 (继承自 Control) |
CanRaiseEvents |
确定是否可以在控件上引发事件。 (继承自 Control) |
CanSelect |
获取一个值,该值指示是否可以选中控件。 (继承自 Control) |
Capture |
获取或设置一个值,该值指示控件是否已捕获鼠标。 (继承自 Control) |
CausesValidation |
获取或设置一个值,该值指示控件是否会引起在任何需要在接收焦点时执行验证的控件上执行验证。 (继承自 Control) |
ClientRectangle |
获取表示控件的工作区的矩形。 (继承自 Control) |
ClientSize |
获取或设置窗体工作区的大小。 |
CompanyName |
获取包含控件的应用程序的公司名称或创建者。 (继承自 Control) |
Container |
获取包含 IContainer 的 Component。 (继承自 Component) |
ContainsFocus |
获取一个值,该值指示控件或它的一个子控件当前是否有输入焦点。 (继承自 Control) |
ContextMenu |
获取或设置与控件关联的快捷菜单。 (继承自 Control) |
ContextMenuStrip |
获取或设置与此控件关联的 ContextMenuStrip。 (继承自 Control) |
ControlBox |
获取或设置一个值,该值指示在该窗体的标题栏中是否显示控件框。 |
Controls |
获取包含在控件内的控件的集合。 (继承自 Control) |
Created |
获取一个值,该值指示控件是否已经创建。 (继承自 Control) |
CreateParams |
获取创建控件句柄时所需要的创建参数。 |
CurrentAutoScaleDimensions |
获取屏幕的当前运行时尺寸。 (继承自 ContainerControl) |
Cursor |
获取或设置当鼠标指针位于控件上时显示的光标。 (继承自 Control) |
DataBindings |
为该控件获取数据绑定。 (继承自 Control) |
DefaultCursor |
获取或设置控件的默认光标。 (继承自 Control) |
DefaultImeMode |
获取控件支持的默认输入法编辑器 (IME) 模式。 |
DefaultMargin |
获取控件之间默认指定的间距(以像素为单位)。 (继承自 Control) |
DefaultMaximumSize |
获取以像素为单位的长度和高度,此长度和高度被指定为控件的默认最大大小。 (继承自 Control) |
DefaultMinimumSize |
获取以像素为单位的长度和高度,此长度和高度被指定为控件的默认最小大小。 (继承自 Control) |
DefaultPadding |
获取控件内容的内部间距(以像素为单位)。 (继承自 Control) |
DefaultSize |
获取控件的默认大小。 |
DesignMode |
获取一个值,用以指示 Component 当前是否处于设计模式。 (继承自 Component) |
DesktopBounds |
获取或设置 Windows 桌面上窗体的大小和位置。 |
DesktopLocation |
获取或设置 Windows 桌面上窗体的位置。 |
DeviceDpi |
获取显示当前控件的显示设备的 DPI 值。 (继承自 Control) |
DialogResult |
获取或设置窗体的对话框结果。 |
DisplayRectangle |
获取表示控件的虚拟显示区域的矩形。 (继承自 ScrollableControl) |
Disposing |
获取一个值,该值指示 Control 基类是否在释放进程中。 (继承自 Control) |
Dock |
获取或设置哪些控件边框停靠到其父控件并确定控件如何随其父级一起调整大小。 (继承自 Control) |
DockPadding |
获取控件的所有边缘的停靠边距设置。 (继承自 ScrollableControl) |
DoubleBuffered |
获取或设置一个值,该值指示此控件是否应使用辅助缓冲区重绘其图面,以减少或避免闪烁。 (继承自 Control) |
Enabled |
获取或设置一个值,该值指示控件是否可以对用户交互作出响应。 (继承自 Control) |
Events |
获取附加到此 Component 的事件处理程序的列表。 (继承自 Component) |
Focused |
获取一个值,该值指示控件是否有输入焦点。 (继承自 Control) |
Font |
获取或设置控件显示的文字的字体。 (继承自 Control) |
FontHeight |
获取或设置控件的字体的高度。 (继承自 Control) |
ForeColor |
获取或设置控件的前景色。 (继承自 Control) |
FormBorderStyle |
获取或设置窗体的边框样式。 |
Handle |
获取控件绑定到的窗口句柄。 (继承自 Control) |
HasChildren |
获取一个值,该值指示控件是否包含一个或多个子控件。 (继承自 Control) |
Height |
获取或设置控件的高度。 (继承自 Control) |
HelpButton |
获取或设置一个值,该值指示是否应在窗体的标题框中显示“帮助”按钮。 |
HorizontalScroll |
获取与水平滚动条关联的特征。 (继承自 ScrollableControl) |
HScroll |
获取或设置一个值,该值指示水平滚动条是否可见。 (继承自 ScrollableControl) |
Icon |
获取或设置窗体的图标。 |
ImeMode |
获取或设置控件的输入法编辑器 (IME) 模式。 (继承自 Control) |
ImeModeBase |
获取或设置控件的 IME 模式。 (继承自 Control) |
InvokeRequired |
获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用 Invoke 方法,因为调用方位于创建控件所在的线程以外的线程中。 (继承自 Control) |
IsAccessible |
获取或设置一个值,该值指示控件对辅助功能应用程序是否可见。 (继承自 Control) |
IsAncestorSiteInDesignMode |
指示此控件的上级位置之一是否位于 DesignMode 中。 此属性为只读。 (继承自 Control) |
IsDisposed |
获取一个值,该值指示控件是否已经被释放。 (继承自 Control) |
IsHandleCreated |
获取一个值,该值指示控件是否有与它关联的句柄。 (继承自 Control) |
IsMdiChild |
获取一个值,该值指示窗体是否为多文档界面 (MDI) 子窗体。 |
IsMdiContainer |
获取或设置一个值,该值指示窗体是否为多文档界面 (MDI) 子窗体的容器。 |
IsMirrored |
获取一个值,该值指示此控件是否为镜像控件。 (继承自 Control) |
IsRestrictedWindow |
获取一个值,该值指示窗体是否可以不受限制地使用所有窗口和用户输入事件。 |
KeyPreview |
获取或设置一个值,该值指示在将键事件传递到具有焦点的控件前,窗体是否将接收此键事件。 |
LayoutEngine |
获取控件的布局引擎的缓存实例。 (继承自 Control) |
Left |
获取或设置控件左边缘与其容器的工作区左边缘之间的距离(以像素为单位)。 (继承自 Control) |
Location | |
Location |
获取或设置该控件的左上角相对于其容器的左上角的坐标。 (继承自 Control) |
MainMenuStrip |
获取或设置窗体的主菜单容器。 |
Margin |
获取或设置控件之间的空间。 |
Margin |
获取或设置控件之间的空间。 (继承自 Control) |
MaximizeBox |
获取或设置一个值,该值指示是否在窗体的标题栏中显示“最大化”按钮。 |
MaximizedBounds |
获取或设置窗体最大化后的大小。 |
MaximumSize |
获取窗体可调整到的最大大小。 |
MdiChildren |
获取窗体的数组,这些窗体表示以此窗体作为父级的多文档界面 (MDI) 子窗体。 |
MdiChildrenMinimizedAnchorBottom |
获取或设置最小化 MDI 子级的定位。 |
MdiParent |
获取或设置此窗体的当前多文档界面 (MDI) 父窗体。 |
Menu |
获取或设置在窗体中显示的 MainMenu。 |
MergedMenu |
获取窗体的合并菜单。 |
MinimizeBox |
获取或设置一个值,该值指示是否在窗体的标题栏中显示“最小化”按钮。 |
MinimumSize |
获取或设置窗体可调整到的最小大小。 |
Modal |
获取一个值,该值指示是否有模式地显示此窗体。 |
Name |
获取或设置控件的名称。 (继承自 Control) |
Opacity |
获取或设置窗体的不透明度级别。 |
OwnedForms |
获取 Form 对象的数组,这些对象表示此窗体拥有的所有窗体。 |
Owner |
获取或设置拥有此窗体的窗体。 |
Padding |
获取或设置控件内的空白。 (继承自 Control) |
Parent |
获取或设置控件的父容器。 (继承自 Control) |
ParentForm |
获取将容器控件分配给的窗体。 (继承自 ContainerControl) |
PreferredSize |
获取可以容纳控件的矩形区域的大小。 (继承自 Control) |
ProductName |
获取包含控件的程序集的产品名称。 (继承自 Control) |
ProductVersion |
获取包含控件的程序集的版本。 (继承自 Control) |
RecreatingHandle |
获取一个值,该值指示控件当前是否在重新创建其句柄。 (继承自 Control) |
Region |
获取或设置与控件关联的窗口区域。 (继承自 Control) |
RenderRightToLeft |
已过时。
已过时。
此属性现已过时。 (继承自 Control) |
ResizeRedraw |
获取或设置一个值,该值指示控件在调整大小时是否重绘自己。 (继承自 Control) |
RestoreBounds |
获取窗体在其正常窗口状态下的位置和大小。 |
Right |
获取控件右边缘与其容器的工作区左边缘之间的距离(以像素为单位)。 (继承自 Control) |
RightToLeft |
获取或设置一个值,该值指示是否将控件的元素对齐以支持使用从右向左的字体的区域设置。 (继承自 Control) |
RightToLeftLayout |
获取或设置一个值,该值指示是否打开从右向左的镜像放置。 |
ScaleChildren |
获取一个值,该值确定子控件的缩放。 (继承自 Control) |
ShowFocusCues |
获取一个值,该值指示控件是否应显示聚焦框。 (继承自 Control) |
ShowIcon |
获取或设置一个值,该值指示是否在窗体的标题栏中显示图标。 |
ShowInTaskbar |
获取或设置一个值,该值指示是否在 Windows 任务栏中显示窗体。 |
ShowKeyboardCues |
获取一个值,该值指示用户界面是否处于适当的状态以显示或隐藏键盘快捷键。 (继承自 Control) |
ShowWithoutActivation |
获取一个值,该值指示显示窗口时是否激活它。 |
Site |
获取或设置控件的站点。 (继承自 Control) |
Size |
获取或设置窗体的大小。 |
SizeGripStyle |
获取或设置在窗体右下角显示的大小调整手柄的样式。 |
StartPosition |
获取或设置运行时窗体的起始位置。 |
TabIndex |
获取或设置控件在其容器内的 Tab 键顺序。 |
TabStop |
获取或设置一个值,该值指示用户能否使用 Tab 键将焦点放到该控件上。 |
TabStop |
获取或设置一个值,该值指示用户能否使用 Tab 键将焦点放到该控件上。 (继承自 Control) |
Tag |
获取或设置包含有关控件的数据的对象。 (继承自 Control) |
Text |
获取或设置与此控件关联的文本。 |
Text |
获取或设置与此控件关联的文本。 (继承自 Control) |
Top |
获取或设置控件上边缘与其容器的工作区上边缘之间的距离(以像素为单位)。 (继承自 Control) |
TopLevel |
获取或设置一个值,该值指示是否将窗体显示为顶层窗口。 |
TopLevelControl |
获取没有另一个 Windows 窗体控件作为其父级的父控件。 通常,这是控件所在的最外面的 Form。 (继承自 Control) |
TopMost |
获取或设置一个值,指示该窗体是否应显示为最顶层窗体。 |
TransparencyKey |
获取或设置将表示窗体透明区域的颜色。 |
UseWaitCursor |
获取或设置一个值,该值指示是否将等待光标用于当前控件以及所有子控件。 (继承自 Control) |
VerticalScroll |
获取与垂直滚动条相关联的特性。 (继承自 ScrollableControl) |
Visible |
获取或设置一个值,该值指示是否显示该控件及其所有子控件。 (继承自 Control) |
VScroll |
获取或设置一个值,该值指示垂直滚动条是否可见。 (继承自 ScrollableControl) |
Width |
获取或设置控件的宽度。 (继承自 Control) |
WindowState |
获取或设置一个值,该值指示窗体是最小化、最大化还是正常。 |
WindowTarget |
此属性与此类无关。 (继承自 Control) |
方法
事件
显式接口实现
IContainerControl.ActivateControl(Control) |
激活指定的控件。 (继承自 ContainerControl) |
IDropTarget.OnDragDrop(DragEventArgs) |
引发 DragDrop 事件。 (继承自 Control) |
IDropTarget.OnDragEnter(DragEventArgs) |
引发 DragEnter 事件。 (继承自 Control) |
IDropTarget.OnDragLeave(EventArgs) |
引发 DragLeave 事件。 (继承自 Control) |
IDropTarget.OnDragOver(DragEventArgs) |
引发 DragOver 事件。 (继承自 Control) |