展示

image.pngimage.png
image.png
form1
image.png
ModalDialog
image.png
DialogData
image.png

窗体控件属性要更改的有:

ModalDialog:
窗体属性中的FormBorderStyle 改为FixedDialog 不显示标题框
MaximizeBox 改为 False 不显示最大化按钮
MinimizeBox 改为 False 没显示最小化按钮
ControlBox 改为 False 不显示关闭按钮
ShowInTask 改为 False 窗体不显示到任务栏中
button1的dialogResult 属性改为 OK 或 Yes
button2的dialogResualt 属性改为 cancel 或 No

附上代码

Form1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace _10_3
  11. {
  12. public partial class Form1 : Form
  13. {
  14. /// <summary>
  15. /// 父窗体
  16. /// </summary>
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. int[] diaocha = new int[3];
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. DialogData dd = new DialogData();
  25. ModalDialog md = new ModalDialog()
  26. {
  27. DataDialog = dd // 赋值
  28. };
  29. md.ShowDialog();
  30. if (md.DialogResult == DialogResult.OK)
  31. {
  32. diaocha[0] += md.DataDialog.DataH;
  33. this.textBox1.Text = diaocha[0].ToString();
  34. diaocha[1] += md.DataDialog.DataY;
  35. this.textBox2.Text = diaocha[1].ToString();
  36. diaocha[2] += md.DataDialog.DataB;
  37. this.textBox3.Text = diaocha[2].ToString();
  38. int count = 0;
  39. foreach (int i in diaocha)
  40. count += i;
  41. this.textBox4.Text = count.ToString();
  42. }
  43. }
  44. }
  45. }

ModalDialog

注意:单选按钮绑定的是CheckedChanged事件

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace _10_3
  11. {
  12. public partial class ModalDialog : Form
  13. {
  14. /// <summary>
  15. /// 模态对话框窗体
  16. /// </summary>
  17. public ModalDialog()
  18. {
  19. InitializeComponent();
  20. }
  21. private int henrenke;
  22. private int yibanrenke;
  23. private int burenke;
  24. private void radioButton1_CheckedChanged(object sender, EventArgs e)
  25. {
  26. if (radioButton1.Checked)
  27. henrenke += 1;
  28. }
  29. private void radioButton2_CheckedChanged(object sender, EventArgs e)
  30. {
  31. if (radioButton2.Checked)
  32. yibanrenke += 1;
  33. }
  34. private void radioButton3_CheckedChanged(object sender, EventArgs e)
  35. {
  36. if (radioButton3.Checked)
  37. burenke += 1;
  38. }
  39. public DialogData DataDialog
  40. {
  41. get
  42. {
  43. DialogData dd = new DialogData()
  44. {
  45. // 赋值
  46. DataH = henrenke,
  47. //类似参数,用','隔开
  48. DataY = yibanrenke,
  49. DataB = burenke
  50. };
  51. return dd;
  52. }
  53. set
  54. {
  55. henrenke = value.DataH;
  56. yibanrenke = value.DataY;
  57. burenke = value.DataB;
  58. }
  59. }
  60. }
  61. }

DialogData
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _10_3
  7. {
  8. /// <summary>
  9. /// 数据存储类
  10. /// </summary>
  11. public class DialogData
  12. {
  13. private int _henrenke;
  14. private int _yibanrenke;
  15. private int _burenke;
  16. public int DataH
  17. {
  18. get
  19. {
  20. return _henrenke;
  21. }
  22. set
  23. {
  24. _henrenke = value;
  25. }
  26. }
  27. public int DataY
  28. {
  29. get
  30. {
  31. return _yibanrenke;
  32. }
  33. set
  34. {
  35. _yibanrenke = value;
  36. }
  37. }
  38. public int DataB
  39. {
  40. get
  41. {
  42. return _burenke;
  43. }
  44. set
  45. {
  46. _burenke = value;
  47. }
  48. }
  49. }
  50. }