当前位置:   article > 正文

C# 通过子窗体刷新父窗体:窗体控制_c#子窗口 刷新父窗体

c#子窗口 刷新父窗体

目录

1、实现方法 

2.SqlCommand类

3.实例的主窗体Frm_Main: 

(1)Frm_Main.Designer.cs

(2)Frm_Main.cs

4.实例的子窗体Frm_Child: 

(1) Frm_Child.Designer.cs

(2)Frm_Child.cs

5.生成效果

(1)生成

(2)添加

(3)添加后

(4)清空


        在一些软件,比如,进销存管理系统中添加销售单信息时,每个销售单都可能对应多种商品,而且在向销售单中添加商品时,一般都是在新弹出的窗体中选择商品,这时就涉及通过子窗体刷新父窗体的问题。

1、实现方法 

        实现通过子窗体刷新父窗体时,主要通过在自定义事件中执行数据绑定来对主窗体进行刷新,即当子窗体产生更新操作时,通过子窗体的一个方法触发主窗体中对应的事件,这个过程主要用到了EventHandler事件。

        EventHandler事件主要是通过EventHandler委托来实现的,该委托表示处理不包含事件数据的事件的方法。语法格式如下:

  1. public delegate void EventHandler(Object sender,EventArgs e)
  2. 参数说明
  3. Sender:事件源。
  4. e:不包含任何事件数据的EventArgs事件数据。

        例如,通过使用EventHandler事件为子窗体添加一个事件处理程序,以便能够刷新父窗体中的数据。

        在子窗体里:

  1. /// <summary>
  2. /// 某一个事件,比如,添加
  3. /// </summary>
  4. private void Button1_Click(object? sender, EventArgs e)
  5. {
  6. GlobalFlag = true; //设定标识的值为true
  7. if (!(comboBox1.Items.Equals(idContent))) //当Combobox控件中不存在将添加的信息时
  8. {
  9. comboBox1.Items.Add(idContent!); //在Combobox控件中添加一条记录
  10. }
  11. UpdateData();
  12. }
  13. // 其它
  14. /// <summary>
  15. /// 更新DataGridView控件中的内容
  16. /// </summary>
  17. protected void UpdateData()
  18. {
  19. UpdateDataGridView?.Invoke(this, EventArgs.Empty);
  20. }

         在主窗体里:

  1. // 添加子窗体的事件
  2. public void CreateFrmChild()
  3. {
  4. Frm_Child BabyWindow = new()
  5. {
  6. MdiParent = this,
  7. };
  8. dataGridView1.Controls.Add(BabyWindow);
  9. BabyWindow.UpdateDataGridView += new EventHandler(BabyWindow_UpdateDataGridView!);
  10. BabyWindow.Show();
  11. }
  12. // 其它
  13. /// <summary>
  14. /// 实现事件委托
  15. /// </summary>
  16. void BabyWindow_UpdateDataGridView(object sender, EventArgs e)
  17. {
  18. // 其它
  19. }

2.SqlCommand类

        本实例中使用了SQL,主窗体的数据读取自SQL,在子窗体里更新,然后回写SQL,其结果再次更新到主窗体。

        在C#中执行SQL语句时,可以使用SqlCommand类,该类主要用于向SQL Server数据库发送SQL语句。

        本实例的数据库连接字符串:

  1. //数据库连接字符串
  2. readonly string? ConnString = "Data Source=DESKTOP-3LV13FS;DataBase=db_TomeOne;integrated security=SSPI;TrustServerCertificate=true;";

        还有,在.NET 8.0中,要使用命名空间:using Microsoft.Data.SqlClient;因为 using System.Data.SqlClient; 已经废弃。

3.实例的主窗体Frm_Main: 

(1)Frm_Main.Designer.cs

  1. namespace _197
  2. {
  3. partial class Frm_Main
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9. /// <summary>
  10. /// Clean up any resources being used.
  11. /// </summary>
  12. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  13. protected override void Dispose(bool disposing)
  14. {
  15. if (disposing && (components != null))
  16. {
  17. components.Dispose();
  18. }
  19. base.Dispose(disposing);
  20. }
  21. #region Windows Form Designer generated code
  22. /// <summary>
  23. /// Required method for Designer support - do not modify
  24. /// the contents of this method with the code editor.
  25. /// </summary>
  26. private void InitializeComponent()
  27. {
  28. menuStrip1 = new MenuStrip();
  29. toolStripMenuItem1 = new ToolStripMenuItem();
  30. toolStripMenuItem2 = new ToolStripMenuItem();
  31. toolStripMenuItem3 = new ToolStripMenuItem();
  32. dataGridView1 = new DataGridView();
  33. menuStrip1.SuspendLayout();
  34. ((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit();
  35. SuspendLayout();
  36. //
  37. // menuStrip1
  38. //
  39. menuStrip1.BackColor = SystemColors.ControlDark;
  40. menuStrip1.Items.AddRange(new ToolStripItem[] { toolStripMenuItem1 });
  41. menuStrip1.Location = new Point(0, 0);
  42. menuStrip1.Name = "menuStrip1";
  43. menuStrip1.Size = new Size(335, 25);
  44. menuStrip1.TabIndex = 0;
  45. menuStrip1.Text = "menuStrip1";
  46. //
  47. // toolStripMenuItem1
  48. //
  49. toolStripMenuItem1.DropDownItems.AddRange(new ToolStripItem[] { toolStripMenuItem2, toolStripMenuItem3 });
  50. toolStripMenuItem1.Name = "toolStripMenuItem1";
  51. toolStripMenuItem1.Size = new Size(44, 21);
  52. toolStripMenuItem1.Text = "操作";
  53. //
  54. // toolStripMenuItem2
  55. //
  56. toolStripMenuItem2.Name = "toolStripMenuItem2";
  57. toolStripMenuItem2.Size = new Size(136, 22);
  58. toolStripMenuItem2.Text = "添加或删除";
  59. toolStripMenuItem2.Click += ToolStripMenuItem2_Click;
  60. //
  61. // toolStripMenuItem3
  62. //
  63. toolStripMenuItem3.Name = "toolStripMenuItem3";
  64. toolStripMenuItem3.Size = new Size(136, 22);
  65. toolStripMenuItem3.Text = "退出";
  66. toolStripMenuItem3.Click += ToolStripMenuItem3_Click;
  67. //
  68. // dataGridView1
  69. //
  70. dataGridView1.AllowUserToAddRows = false;
  71. dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
  72. dataGridView1.Dock = DockStyle.Fill;
  73. dataGridView1.Location = new Point(0, 25);
  74. dataGridView1.Name = "dataGridView1";
  75. dataGridView1.Size = new Size(335, 178);
  76. dataGridView1.TabIndex = 1;
  77. //
  78. // Frm_Main
  79. //
  80. AutoScaleDimensions = new SizeF(7F, 17F);
  81. AutoScaleMode = AutoScaleMode.Font;
  82. ClientSize = new Size(335, 203);
  83. Controls.Add(dataGridView1);
  84. Controls.Add(menuStrip1);
  85. IsMdiContainer = true;
  86. MainMenuStrip = menuStrip1;
  87. Name = "Frm_Main";
  88. Text = "主窗体";
  89. Load += Frm_Main_Load;
  90. menuStrip1.ResumeLayout(false);
  91. menuStrip1.PerformLayout();
  92. ((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit();
  93. ResumeLayout(false);
  94. PerformLayout();
  95. }
  96. #endregion
  97. private MenuStrip menuStrip1;
  98. private ToolStripMenuItem toolStripMenuItem1;
  99. private ToolStripMenuItem toolStripMenuItem2;
  100. private ToolStripMenuItem toolStripMenuItem3;
  101. private DataGridView dataGridView1;
  102. }
  103. }

(2)Frm_Main.cs

  1. // 通过子窗体刷新父窗体
  2. // 设置主窗体的IsMdiContainer=true;
  3. using System.Data;
  4. using Microsoft.Data.SqlClient;
  5. namespace _197
  6. {
  7. public partial class Frm_Main : Form
  8. {
  9. public Frm_Main()
  10. {
  11. InitializeComponent();
  12. }
  13. #region 声明的变量
  14. private static bool flag = false; //标识是否创建新的子窗体
  15. readonly Frm_Child BabyWindow = new();//实例化一个子窗体
  16. readonly DataSet PubsSet = new(); //定义一个数据集对象
  17. private static string[]? iDArray; //声明一个一维字符串数组
  18. public DataTable? IDTable; //声明一个数据表对象
  19. SqlDataAdapter? IDAdapter; //声明一个数据读取器对象
  20. SqlDataAdapter? PubsAdapter; //声明一个数据读取器对象
  21. SqlConnection? ConnPubs; //声明一个数据库连接对象
  22. SqlCommand? PersonalInformation; //声明一个执行SQL语句的对象
  23. public static string[]? IDArray { get => iDArray; set => iDArray = value; }
  24. public static bool Flag { get => flag; set => flag = value; }
  25. readonly string? ConnString //数据库连接字符串
  26. = "Data Source=DESKTOP-3LV13FS;DataBase=db_TomeOne;integrated security=SSPI;TrustServerCertificate=true;";
  27. #endregion
  28. private void Frm_Main_Load(object? sender, EventArgs e)
  29. {
  30. string AdapterString = "select userID as 编号,userName as 姓名 ,phone as 电话,address as 住址 from tb_User";//用于查询的字符串
  31. string IDString = "select userID from tb_User";//读取数据库中用户的ID编号字符串
  32. ConnPubs = new SqlConnection(ConnString);//建立数据库连接
  33. PubsAdapter = new SqlDataAdapter(AdapterString, ConnPubs);//创建PubsAdapter数据读取器
  34. IDAdapter = new SqlDataAdapter(IDString, ConnPubs); //用于读取用户编号的读取器
  35. PubsAdapter.Fill(PubsSet, "tb_User"); //填充PubsSet数据集
  36. IDAdapter.Fill(PubsSet, "ID"); //填充PubsSet数据集
  37. DataTable PubsTable = PubsSet.Tables["tb_User"]!;//将数据写入PubsTable表
  38. IDTable = PubsSet.Tables["ID"]!; //将数据写入ID表
  39. IDArray = new string[IDTable.Rows.Count]; //为数组定义最大长度
  40. dataGridView1.DataSource = PubsTable.DefaultView;//设置dataGridView1的数据源
  41. for (int i = 0; i < IDTable.Rows.Count; i++) //循环遍历数据表中的每一行数据
  42. {
  43. for (int j = 0; j < IDTable.Columns.Count; j++)//循环遍历数据表中的每一列数据
  44. {
  45. IDArray[i] = IDTable.Rows[i][j].ToString()!;//将数据表中的数据添加至一个一维数组
  46. }
  47. }
  48. }
  49. #region 创建子窗体的CreateFrmChild方法
  50. /// <summary>
  51. /// 设置子窗体的父窗体为当前窗体;
  52. /// 实例化一个子窗体;
  53. /// 在DataGridView控件中添加子窗体;
  54. /// 显示子窗体;
  55. /// </summary>
  56. public void CreateFrmChild()
  57. {
  58. Frm_Child BabyWindow = new()
  59. {
  60. MdiParent = this,
  61. };
  62. dataGridView1.Controls.Add(BabyWindow);
  63. BabyWindow.UpdateDataGridView += new EventHandler(BabyWindow_UpdateDataGridView!);
  64. BabyWindow.Show();
  65. }
  66. /// <summary>
  67. /// 事件委托
  68. /// </summary>
  69. void BabyWindow_UpdateDataGridView(object sender, EventArgs e)
  70. {
  71. if (Frm_Child.GlobalFlag == false) //当单击删除按钮时
  72. {
  73. if (ConnPubs!.State == ConnectionState.Closed) //当数据库处于断开状态时
  74. {
  75. ConnPubs.Open(); //打开数据库的连接
  76. }
  77. string AfreshString = "delete tb_User where userID=" + Frm_Child.DeleteID!.Trim();//定义一个删除数据的字符串
  78. PersonalInformation = new SqlCommand(AfreshString, ConnPubs); //执行删除数据库字段
  79. PersonalInformation.ExecuteNonQuery(); //执行SQL语句并返回受影响的行数
  80. ConnPubs.Close(); //关闭数据库
  81. DisplayData(); //显示数据库更新后的内容
  82. MessageBox.Show("数据删除成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  83. }
  84. else
  85. {
  86. if (ConnPubs!.State == ConnectionState.Closed) //当数据库处于关闭状态时
  87. {
  88. ConnPubs.Open(); //打开数据库
  89. }
  90. string InsertString = "insert into tb_User values('" + Frm_Child.IdContent + "','" + Frm_Child.NameContent + "','" + Frm_Child.PhoneContent + "','" + Frm_Child.AddressContent + "')";//定义一个插入数据的字符串变量
  91. PersonalInformation = new SqlCommand(InsertString, ConnPubs);//执行插入数据库字段
  92. PersonalInformation.ExecuteNonQuery(); //执行SQL语句并返回受影响的行数
  93. ConnPubs.Close(); //关闭数据库
  94. DisplayData(); //显示更新后的数据
  95. MessageBox.Show("数据添加成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  96. }
  97. }
  98. #endregion
  99. #region 用于读取数据库的DisplayData方法
  100. public void DisplayData()
  101. {
  102. PubsSet.Clear();
  103. ConnPubs = new SqlConnection(ConnString);
  104. string DisplayString = "select userId as 编号,userName as 姓名 ,phone as 电话,address as 住址 from tb_User";//定义读取数据库的字段
  105. SqlDataAdapter PersonalAdapter = new(DisplayString, ConnPubs); //定义一个读取数据库数据的读取器
  106. PersonalAdapter.Fill(PubsSet, "tb_User"); //向表DisplayTable中填充数据
  107. dataGridView1.DataSource = PubsSet.Tables["tb_User"]!.DefaultView; //设定DataGridView控件的数据源
  108. }
  109. #endregion
  110. /// <summary>
  111. /// 添加或删除记录
  112. /// 创建子窗体的CreateFrmChild方法
  113. /// </summary>
  114. private void ToolStripMenuItem2_Click(object sender, EventArgs e)
  115. {
  116. if (flag == false) //判断标识的值决定是否创建窗体
  117. {
  118. CreateFrmChild();//创建子窗体
  119. }
  120. for (int i = 0; i < dataGridView1.Controls.Count; i++) //循环遍历DataGridView控件上的控件集
  121. {
  122. if (dataGridView1.Controls[i].Name.Equals(BabyWindow.Name)) //当存在子窗体时
  123. {
  124. flag = true; //改变标识Flag的值
  125. break; //退出循环体
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// 退出
  131. /// </summary>
  132. private void ToolStripMenuItem3_Click(object sender, EventArgs e)
  133. {
  134. Application.Exit();
  135. }
  136. }
  137. }

4.实例的子窗体Frm_Child: 

(1) Frm_Child.Designer.cs

  1. namespace _197
  2. {
  3. partial class Frm_Child
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9. /// <summary>
  10. /// Clean up any resources being used.
  11. /// </summary>
  12. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  13. protected override void Dispose(bool disposing)
  14. {
  15. if (disposing && (components != null))
  16. {
  17. components.Dispose();
  18. }
  19. base.Dispose(disposing);
  20. }
  21. #region Windows Form Designer generated code
  22. /// <summary>
  23. /// Required method for Designer support - do not modify
  24. /// the contents of this method with the code editor.
  25. /// </summary>
  26. private void InitializeComponent()
  27. {
  28. groupBox1 = new GroupBox();
  29. button2 = new Button();
  30. button1 = new Button();
  31. Record_Address = new TextBox();
  32. Record_Phone = new TextBox();
  33. Record_Name = new TextBox();
  34. Record_ID = new TextBox();
  35. label4 = new Label();
  36. label3 = new Label();
  37. label2 = new Label();
  38. label1 = new Label();
  39. groupBox2 = new GroupBox();
  40. button3 = new Button();
  41. comboBox1 = new ComboBox();
  42. label5 = new Label();
  43. groupBox1.SuspendLayout();
  44. groupBox2.SuspendLayout();
  45. SuspendLayout();
  46. //
  47. // groupBox1
  48. //
  49. groupBox1.Controls.Add(button2);
  50. groupBox1.Controls.Add(button1);
  51. groupBox1.Controls.Add(Record_Address);
  52. groupBox1.Controls.Add(Record_Phone);
  53. groupBox1.Controls.Add(Record_Name);
  54. groupBox1.Controls.Add(Record_ID);
  55. groupBox1.Controls.Add(label4);
  56. groupBox1.Controls.Add(label3);
  57. groupBox1.Controls.Add(label2);
  58. groupBox1.Controls.Add(label1);
  59. groupBox1.Location = new Point(12, 12);
  60. groupBox1.Name = "groupBox1";
  61. groupBox1.Size = new Size(270, 157);
  62. groupBox1.TabIndex = 0;
  63. groupBox1.TabStop = false;
  64. groupBox1.Text = "添加信息";
  65. //
  66. // button2
  67. //
  68. button2.Location = new Point(187, 56);
  69. button2.Name = "button2";
  70. button2.Size = new Size(75, 23);
  71. button2.TabIndex = 9;
  72. button2.Text = "清空";
  73. button2.UseVisualStyleBackColor = true;
  74. button2.Click += Button2_Click;
  75. //
  76. // button1
  77. //
  78. button1.Location = new Point(187, 25);
  79. button1.Name = "button1";
  80. button1.Size = new Size(77, 23);
  81. button1.TabIndex = 8;
  82. button1.Text = "提交";
  83. button1.UseVisualStyleBackColor = true;
  84. button1.Click += Button1_Click;
  85. //
  86. // Record_Address
  87. //
  88. Record_Address.Location = new Point(72, 118);
  89. Record_Address.Name = "Record_Address";
  90. Record_Address.Size = new Size(100, 23);
  91. Record_Address.TabIndex = 7;
  92. Record_Address.TextChanged += Record_Address_TextChanged;
  93. //
  94. // Record_Phone
  95. //
  96. Record_Phone.Location = new Point(72, 87);
  97. Record_Phone.Name = "Record_Phone";
  98. Record_Phone.Size = new Size(100, 23);
  99. Record_Phone.TabIndex = 6;
  100. Record_Phone.TextChanged += Record_Phone_TextChanged;
  101. //
  102. // Record_Name
  103. //
  104. Record_Name.Location = new Point(72, 56);
  105. Record_Name.Name = "Record_Name";
  106. Record_Name.Size = new Size(100, 23);
  107. Record_Name.TabIndex = 5;
  108. Record_Name.TextChanged += Record_Name_TextChanged;
  109. //
  110. // Record_ID
  111. //
  112. Record_ID.Location = new Point(72, 25);
  113. Record_ID.Name = "Record_ID";
  114. Record_ID.Size = new Size(100, 23);
  115. Record_ID.TabIndex = 4;
  116. Record_ID.TextChanged += Record_ID_TextChanged;
  117. //
  118. // label4
  119. //
  120. label4.AutoSize = true;
  121. label4.Location = new Point(11, 118);
  122. label4.Name = "label4";
  123. label4.Size = new Size(44, 17);
  124. label4.TabIndex = 3;
  125. label4.Text = "地址:";
  126. //
  127. // label3
  128. //
  129. label3.AutoSize = true;
  130. label3.Location = new Point(11, 87);
  131. label3.Name = "label3";
  132. label3.Size = new Size(44, 17);
  133. label3.TabIndex = 2;
  134. label3.Text = "电话:";
  135. //
  136. // label2
  137. //
  138. label2.AutoSize = true;
  139. label2.Location = new Point(11, 56);
  140. label2.Name = "label2";
  141. label2.Size = new Size(44, 17);
  142. label2.TabIndex = 1;
  143. label2.Text = "姓名:";
  144. //
  145. // label1
  146. //
  147. label1.AutoSize = true;
  148. label1.Location = new Point(11, 25);
  149. label1.Name = "label1";
  150. label1.Size = new Size(44, 17);
  151. label1.TabIndex = 0;
  152. label1.Text = "编号:";
  153. //
  154. // groupBox2
  155. //
  156. groupBox2.Controls.Add(button3);
  157. groupBox2.Controls.Add(comboBox1);
  158. groupBox2.Controls.Add(label5);
  159. groupBox2.Location = new Point(12, 175);
  160. groupBox2.Name = "groupBox2";
  161. groupBox2.Size = new Size(270, 54);
  162. groupBox2.TabIndex = 1;
  163. groupBox2.TabStop = false;
  164. groupBox2.Text = "删除信息";
  165. //
  166. // button3
  167. //
  168. button3.Location = new Point(187, 24);
  169. button3.Name = "button3";
  170. button3.Size = new Size(75, 23);
  171. button3.TabIndex = 2;
  172. button3.Text = "删除";
  173. button3.UseVisualStyleBackColor = true;
  174. button3.Click += Button3_Click;
  175. //
  176. // comboBox1
  177. //
  178. comboBox1.FormattingEnabled = true;
  179. comboBox1.Location = new Point(72, 24);
  180. comboBox1.Name = "comboBox1";
  181. comboBox1.Size = new Size(100, 25);
  182. comboBox1.TabIndex = 1;
  183. //
  184. // label5
  185. //
  186. label5.AutoSize = true;
  187. label5.Location = new Point(11, 30);
  188. label5.Name = "label5";
  189. label5.Size = new Size(44, 17);
  190. label5.TabIndex = 0;
  191. label5.Text = "编号:";
  192. //
  193. // Frm_Child
  194. //
  195. AutoScaleDimensions = new SizeF(7F, 17F);
  196. AutoScaleMode = AutoScaleMode.Font;
  197. ClientSize = new Size(294, 241);
  198. Controls.Add(groupBox2);
  199. Controls.Add(groupBox1);
  200. MaximizeBox = false; //最大化按钮没有意义
  201. Name = "Frm_Child";
  202. StartPosition = FormStartPosition.CenterScreen;
  203. Text = "子窗体";
  204. FormClosing += Frm_Child_FormClosing;
  205. Load += Frm_Child_Load;
  206. groupBox1.ResumeLayout(false);
  207. groupBox1.PerformLayout();
  208. groupBox2.ResumeLayout(false);
  209. groupBox2.PerformLayout();
  210. ResumeLayout(false);
  211. }
  212. #endregion
  213. private GroupBox groupBox1;
  214. private TextBox Record_Address;
  215. private TextBox Record_Phone;
  216. private TextBox Record_Name;
  217. private TextBox Record_ID;
  218. private Label label4;
  219. private Label label3;
  220. private Label label2;
  221. private Label label1;
  222. private GroupBox groupBox2;
  223. private Button button2;
  224. private Button button1;
  225. private Button button3;
  226. private ComboBox comboBox1;
  227. private Label label5;
  228. }
  229. }

(2)Frm_Child.cs

  1. // 本实例仅可创建一个窗口:
  2. // 最大化后没有实际意义,因此关闭MaximizeBox属性值为False
  3. namespace _197
  4. {
  5. public partial class Frm_Child : Form
  6. {
  7. #region 变量的声明
  8. public event EventHandler? UpdateDataGridView = null;//定义一个处理更新DataGridView控件内容的方法
  9. private static string? deleteID; //定义一个表示删除数据编号的字符串
  10. private static string? idContent; //该变量用来存储数据编号
  11. private static string? nameContent; //该变量用来存储姓名
  12. private static string? phoneContent; //该变量用来存储电话
  13. private static string? addressContent; //该变量用来存储住址
  14. private static bool globalFlag; //该变量用来标识是否创建新的子窗体
  15. public static string? DeleteID { get => deleteID; set => deleteID = value; }
  16. public static string? IdContent { get => idContent; set => idContent = value; }
  17. public static string? NameContent { get => nameContent; set => nameContent = value; }
  18. public static string? PhoneContent { get => phoneContent; set => phoneContent = value; }
  19. public static string? AddressContent { get => addressContent; set => addressContent = value; }
  20. public static bool GlobalFlag { get => globalFlag; set => globalFlag = value; }
  21. #endregion
  22. public Frm_Child()
  23. {
  24. InitializeComponent();
  25. }
  26. /// <summary>
  27. /// 添加
  28. /// </summary>
  29. private void Button1_Click(object? sender, EventArgs e)
  30. {
  31. GlobalFlag = true; //设定标识的值为true
  32. if (!(comboBox1.Items.Equals(idContent))) //当Combobox控件中不存在将添加的信息时
  33. {
  34. comboBox1.Items.Add(idContent!); //在Combobox控件中添加一条记录
  35. }
  36. UpdateData();
  37. }
  38. /// <summary>
  39. /// 清空
  40. /// </summary>
  41. private void Button2_Click(object? sender, EventArgs e)
  42. {
  43. Record_ID.Clear(); //清空编号文本框中的内容
  44. Record_Name.Clear(); //清空姓名文本框中的内容
  45. Record_Phone.Clear(); //清空电话号码文本框中的内容
  46. Record_Address.Clear(); //清空居住地址文本框中的内容
  47. Record_ID.Focus(); //设定当前鼠标的焦点在编号文本框中
  48. }
  49. /// <summary>
  50. /// 删除
  51. /// </summary>
  52. private void Button3_Click(object? sender, EventArgs e)
  53. {
  54. GlobalFlag = false; //设定全局变量表示为false
  55. if (comboBox1.Items.Count > 1) //当ComboBox中剩不小于2条内容时
  56. {
  57. DeleteID = comboBox1.SelectedItem!.ToString(); //将选中项转化为int型
  58. if (comboBox1.Items.Count != 0) //当ComboBox中剩1条内容时
  59. {
  60. comboBox1.Items.Remove(comboBox1.SelectedItem);
  61. comboBox1.SelectedIndex = 0;
  62. }
  63. }
  64. UpdateData();
  65. }
  66. /// <summary>
  67. /// 保存新添加记录的编号
  68. /// </summary>
  69. private void Record_ID_TextChanged(object? sender, EventArgs e)
  70. {
  71. idContent = Record_ID.Text;
  72. }
  73. /// <summary>
  74. /// 保存填入的姓名
  75. /// </summary>
  76. private void Record_Name_TextChanged(object? sender, EventArgs e)
  77. {
  78. nameContent = Record_Name.Text;
  79. }
  80. /// <summary>
  81. /// 保存填入的电话号码
  82. /// </summary>
  83. private void Record_Phone_TextChanged(object? sender, EventArgs e)
  84. {
  85. phoneContent = Record_Phone.Text;
  86. }
  87. /// <summary>
  88. /// 保存填入的地址信息
  89. /// </summary>
  90. private void Record_Address_TextChanged(object? sender, EventArgs e)
  91. {
  92. addressContent = Record_Address.Text;
  93. }
  94. /// <summary>
  95. /// 循环遍历数组中的每一个元素,向Combobox控件中添加内容
  96. /// 默认设定当前选中项的索引为0
  97. /// </summary>
  98. private void Frm_Child_Load(object? sender, EventArgs e)
  99. {
  100. for (int i = 0; i < Frm_Main.IDArray!.Length; i++)
  101. {
  102. comboBox1.Items.Add(Frm_Main.IDArray[i].ToString());
  103. comboBox1.SelectedIndex = 0;
  104. }
  105. }
  106. /// <summary>
  107. /// 设定该值为false表示可以创建新窗体
  108. /// </summary>
  109. private void Frm_Child_FormClosing(object? sender, FormClosingEventArgs e)
  110. {
  111. Frm_Main.Flag = false;
  112. }
  113. /// <summary>
  114. /// 更新DataGridView控件中的内容
  115. /// </summary>
  116. protected void UpdateData()
  117. {
  118. UpdateDataGridView?.Invoke(this, EventArgs.Empty);
  119. }
  120. }
  121. }

5.生成效果

(1)生成

 

(2)添加

 

(3)添加后

 

(4)清空

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/690601
推荐阅读
相关标签
  

闽ICP备14008679号