1.窗体事件的发生顺序如下:
· 构造函数:在对象实例化过程中执行;
· Load:在对象实例化之后,窗体可见之前发生,此时窗体已存在,但不可见;
· Activated:在窗体处于可见状态并处于当前状态时发生;
· Closing:在窗体关闭时发生;
· Closed:在窗体关闭后发生;
· Deactivate:在窗体关闭后发生,不能执行防止窗体被正常垃圾收集的操作;
2.Show()和ShowDialog()方法的区别:
· 调用Show()方法后,后面的代码会立即执行;
· 调用ShowDialog()方法后,直到ShowDialog()方法的窗体关闭后才继续执行之后的代码。
TestFormShow:
Form1.cs:主窗体
01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Data;
05.using System.Drawing;
06.using System.Text;
07.using System.Windows.Forms;
08.
09.namespace TestFormShow
10.{
11. public partial class Form1 : Form
12. {
13. public Form1()
14. {
15. InitializeComponent();
16. }
17.
18. private void button1_Click(object sender, EventArgs e)
19. {
20. //创建对话框
21. Phone frm = new Phone();
22. while (true)
23. {
24. //显示对话框
25. frm.ShowDialog();
26.
27. //按下确认按钮
28. if (frm.DialogResult == DialogResult.OK)
29. {
30. if (frm.PhoneNumber.Length == 7 || frm.PhoneNumber.Length == 8)
31. {
32. //格式正确,显示结果,并退出循环
33. label1.Text = "Phone Number is : " + frm.PhoneNumber;
34. break;
35. }
36. else
37. {
38. //格式不正确,提示错误信息,继续循环
39. MessageBox.Show("Phone number must be 7 or 8 digit.");
40. }
41. }
42. //按下取消按钮
43. else if (frm.DialogResult == DialogResult.Cancel)
44. {
45. label1.Text = "Form was Canceled!";
46. break;
47. }
48.
49. }
50. //关闭对话框
51. frm.Close();
52. }
53. }
54.}
Phone.cs:对话框
view source
print ? 01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Data;
05.using System.Drawing;
06.using System.Text;
07.using System.Windows.Forms;
08.
09.namespace TestFormShow
10.{
11. public partial class Phone : Form
12. {
13. public Phone()
14. {
15. InitializeComponent();
16.
17. button1.DialogResult = DialogResult.OK;
18. button2.DialogResult = DialogResult.Cancel;
19.
20. }
21.
22. public string PhoneNumber
23. {
24. get
25. {
26. return textBox1.Text;
27. }
28. set
29. {
30. textBox1.Text = value;
31. }
32. }
33. }
· 构造函数:在对象实例化过程中执行;
· Load:在对象实例化之后,窗体可见之前发生,此时窗体已存在,但不可见;
· Activated:在窗体处于可见状态并处于当前状态时发生;
· Closing:在窗体关闭时发生;
· Closed:在窗体关闭后发生;
· Deactivate:在窗体关闭后发生,不能执行防止窗体被正常垃圾收集的操作;
2.Show()和ShowDialog()方法的区别:
· 调用Show()方法后,后面的代码会立即执行;
· 调用ShowDialog()方法后,直到ShowDialog()方法的窗体关闭后才继续执行之后的代码。
TestFormShow:
Form1.cs:主窗体
01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Data;
05.using System.Drawing;
06.using System.Text;
07.using System.Windows.Forms;
08.
09.namespace TestFormShow
10.{
11. public partial class Form1 : Form
12. {
13. public Form1()
14. {
15. InitializeComponent();
16. }
17.
18. private void button1_Click(object sender, EventArgs e)
19. {
20. //创建对话框
21. Phone frm = new Phone();
22. while (true)
23. {
24. //显示对话框
25. frm.ShowDialog();
26.
27. //按下确认按钮
28. if (frm.DialogResult == DialogResult.OK)
29. {
30. if (frm.PhoneNumber.Length == 7 || frm.PhoneNumber.Length == 8)
31. {
32. //格式正确,显示结果,并退出循环
33. label1.Text = "Phone Number is : " + frm.PhoneNumber;
34. break;
35. }
36. else
37. {
38. //格式不正确,提示错误信息,继续循环
39. MessageBox.Show("Phone number must be 7 or 8 digit.");
40. }
41. }
42. //按下取消按钮
43. else if (frm.DialogResult == DialogResult.Cancel)
44. {
45. label1.Text = "Form was Canceled!";
46. break;
47. }
48.
49. }
50. //关闭对话框
51. frm.Close();
52. }
53. }
54.}
Phone.cs:对话框
view source
print ? 01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Data;
05.using System.Drawing;
06.using System.Text;
07.using System.Windows.Forms;
08.
09.namespace TestFormShow
10.{
11. public partial class Phone : Form
12. {
13. public Phone()
14. {
15. InitializeComponent();
16.
17. button1.DialogResult = DialogResult.OK;
18. button2.DialogResult = DialogResult.Cancel;
19.
20. }
21.
22. public string PhoneNumber
23. {
24. get
25. {
26. return textBox1.Text;
27. }
28. set
29. {
30. textBox1.Text = value;
31. }
32. }
33. }
34.}
本文转自My_King1 51CTO博客,原文链接:http://blog.51cto.com/apprentice/1360723,如需转载请自行联系原作者