2006/5/13
考试考得差不多了,看了一些书,参考了一下MSDN,有很多收获: Form.DialogResult属性 (表示当窗体用作对话框时该窗体的结果。)
窗体的对话框结果是当窗体显示为模式对话框时,从该窗体返回的值。如果该窗体显示为对话框,用来自 DialogResult 枚举的值设置此属性可以为该窗体设置对话框结果的值、隐藏该模式对话框并将控件返回到调用窗体。此属性通常由窗体上 Button 控件的 DialogResult 属性设置。当用户单击 Button 控件时,分配给 Button 的 DialogResult 属性的值将分配给该窗体的 DialogResult 属性。
当窗体显示为模式对话框时,单击“关闭”按钮(窗体右上角带 X 的按钮)将使窗体隐藏,并使 DialogResult 属性设置为 DialogResult.Cancel。当用户单击对话框的“关闭”按钮或设置 DialogResult 属性的该值时,不自动调用 Close 方法。而是隐藏该窗体并可重新显示该窗体,而不用创建该对话框的新实例。因为此行为,所以当应用程序不再需要该窗体时,必须调用该窗体的 Dispose 方法。
可以使用此属性确定对话框是如何关闭的,以便正确处理在该对话框中执行的操作。
注意 通过在窗体的 Closing 事件的事件处理程序中设置 DialogResult 属性,可以重写用户单击“关闭”按钮时分配给 DialogResult 属性的值。
注意 如果 Form 显示为无模式窗口,则 DialogResult 属性返回的值可能不返回分配给该窗体的值,因为关闭该窗体时将自动释放该窗体的资源。
[C#]
1
public void CreateMyForm()2
...{3
// Create a new instance of the form.4
Form form1 = new Form();5
// Create two buttons to use as the accept and cancel buttons.6
Button button1 = new Button ();7
Button button2 = new Button ();8
9
// Set the text of button1 to "OK".10
button1.Text = "OK";11
// Set the position of the button on the form.12
button1.Location = new Point (10, 10);13
// Set the text of button2 to "Cancel".14
button2.Text = "Cancel";15
// Set the position of the button based on the location of button1.16
button2.Location17
= new Point (button1.Left, button1.Height + button1.Top + 10);18
// Make button1's dialog result OK.19
button1.DialogResult = DialogResult.OK;20
// Make button2's dialog result Cancel.21
button2.DialogResult = DialogResult.Cancel;22
// Set the caption bar text of the form.23
form1.Text = "My Dialog Box";24
25
// Define the border style of the form to a dialog box.26
form1.FormBorderStyle = FormBorderStyle.FixedDialog;27
// Set the accept button of the form to button1.28
form1.AcceptButton = button1;29
// Set the cancel button of the form to button2.30
form1.CancelButton = button2;31
// Set the start position of the form to the center of the screen.32
form1.StartPosition = FormStartPosition.CenterScreen;33
34
// Add button1 to the form.35
form1.Controls.Add(button1);36
// Add button2 to the form.37
form1.Controls.Add(button2);38
39
// Display the form as a modal dialog box.40
form1.ShowDialog();41
42
// Determine if the OK button was clicked on the dialog box.43
if (form1.DialogResult == DialogResult.OK)44
...{45
// Display a message box indicating that the OK button was clicked.46
MessageBox.Show("The OK button on the form was clicked.");47
// Optional: Call the Dispose method when you are finished with the dialog box.48
form1.Dispose();49
}50
else51
...{52
// Display a message box indicating that the Cancel button was clicked.53
MessageBox.Show("The Cancel button on the form was clicked.");54
// Optional: Call the Dispose method when you are finished with the dialog box.55
form1.Dispose();56
}57
}1
[Visual Basic]2
Public Sub CreateMyForm()Sub CreateMyForm()3
' Create a new instance of the form.4
Dim form1 As New Form()5
' Create two buttons to use as the accept and cancel buttons.6
Dim button1 As New Button()7
Dim button2 As New Button()8
9
' Set the text of button1 to "OK".10
button1.Text = "OK"11
' Set the position of the button on the form.12
button1.Location = New Point(10, 10)13
' Set the text of button2 to "Cancel".14
button2.Text = "Cancel"15
' Set the position of the button based on the location of button1.16
button2.Location = New Point(button1.Left, button1.Height + button1.Top + 10)17
' Make button1's dialog result OK.18
button1.DialogResult = DialogResult.OK19
' Make button2's dialog result Cancel.20
button2.DialogResult = DialogResult.Cancel21
' Set the caption bar text of the form.22
form1.Text = "My Dialog Box"23
24
' Define the border style of the form to a dialog box.25
form1.FormBorderStyle = FormBorderStyle.FixedDialog26
' Set the accept button of the form to button1.27
form1.AcceptButton = button128
' Set the cancel button of the form to button2.29
form1.CancelButton = button230
' Set the start position of the form to the center of the screen.31
form1.StartPosition = FormStartPosition.CenterScreen32
33
' Add button1 to the form.34
form1.Controls.Add(button1)35
' Add button2 to the form.36
form1.Controls.Add(button2)37
38
' Display the form as a modal dialog box.39
form1.ShowDialog()40
41
' Determine if the OK button was clicked on the dialog box.42
If form1.DialogResult = DialogResult.OK Then43
' Display a message box indicating that the OK button was clicked.44
MessageBox.Show("The OK button on the form was clicked.")45
' Optional: Call the Dispose method when you are finished with the dialog box.46
form1.Dispose47
' Display a message box indicating that the Cancel button was clicked.48
Else49
MessageBox.Show("The Cancel button on the form was clicked.")50
' Optional: Call the Dispose method when you are finished with the dialog box.51
form1.Dispose52
End If53
End Sub 'CreateMyFormListView.Activation属性
Activation 属性允许您指定用户激活 ListView 控件中的项的方式。激活 ListView 中的项与只选择项是不同的。当激活某个项时,通常会在 ItemActivate 事件的事件处理程序中执行一个操作。例如,当激活某个项时,您可能会打开一个文件或显示一个允许用户对项进行编辑的对话框。通常,项的激活是通过用户对它进行双击来实现的。如果 Activation 属性设置为 ItemActivation.OneClick,那么单击该项一次即可将其激活。将 Activation 属性设置为 ItemActivation.TwoClick 与标准的双击不同,这是因为两次单击之间的时间间隔可以是任意的。
注意 如果将 Activation 属性设置为 ItemActivation.OneClick 或 ItemActivation.TwoClick,则不管 LabelEdit 属性的值是什么,都不允许进行标签编辑。