C#通過子窗體刷新父窗體的實(shí)現(xiàn)方法
1、實(shí)現(xiàn)方法
實(shí)現(xiàn)通過子窗體刷新父窗體時(shí),主要通過在自定義事件中執(zhí)行數(shù)據(jù)綁定來對(duì)主窗體進(jìn)行刷新,即當(dāng)子窗體產(chǎn)生更新操作時(shí),通過子窗體的一個(gè)方法觸發(fā)主窗體中對(duì)應(yīng)的事件,這個(gè)過程主要用到了EventHandler事件。
EventHandler事件主要是通過EventHandler委托來實(shí)現(xiàn)的,該委托表示處理不包含事件數(shù)據(jù)的事件的方法。語法格式如下:
public delegate void EventHandler(Object sender,EventArgs e) 參數(shù)說明 Sender:事件源。 e:不包含任何事件數(shù)據(jù)的EventArgs事件數(shù)據(jù)。
例如,通過使用EventHandler事件為子窗體添加一個(gè)事件處理程序,以便能夠刷新父窗體中的數(shù)據(jù)。
在子窗體里:
/// <summary>
/// 某一個(gè)事件,比如,添加
/// </summary>
private void Button1_Click(object? sender, EventArgs e)
{
GlobalFlag = true; //設(shè)定標(biāo)識(shí)的值為true
if (!(comboBox1.Items.Equals(idContent))) //當(dāng)Combobox控件中不存在將添加的信息時(shí)
{
comboBox1.Items.Add(idContent!); //在Combobox控件中添加一條記錄
}
UpdateData();
}
// 其它
/// <summary>
/// 更新DataGridView控件中的內(nèi)容
/// </summary>
protected void UpdateData()
{
UpdateDataGridView?.Invoke(this, EventArgs.Empty);
}在主窗體里:
// 添加子窗體的事件
public void CreateFrmChild()
{
Frm_Child BabyWindow = new()
{
MdiParent = this,
};
dataGridView1.Controls.Add(BabyWindow);
BabyWindow.UpdateDataGridView += new EventHandler(BabyWindow_UpdateDataGridView!);
BabyWindow.Show();
}
// 其它
/// <summary>
/// 實(shí)現(xiàn)事件委托
/// </summary>
void BabyWindow_UpdateDataGridView(object sender, EventArgs e)
{
// 其它
}2.SqlCommand類
本實(shí)例中使用了SQL,主窗體的數(shù)據(jù)讀取自SQL,在子窗體里更新,然后回寫SQL,其結(jié)果再次更新到主窗體。
在C#中執(zhí)行SQL語句時(shí),可以使用SqlCommand類,該類主要用于向SQL Server數(shù)據(jù)庫發(fā)送SQL語句。
本實(shí)例的數(shù)據(jù)庫連接字符串:
//數(shù)據(jù)庫連接字符串 readonly string? ConnString = "Data Source=DESKTOP-3LV13FS;DataBase=db_TomeOne;integrated security=SSPI;TrustServerCertificate=true;";
還有,在.NET 8.0中,要使用命名空間:using Microsoft.Data.SqlClient;因?yàn)?using System.Data.SqlClient; 已經(jīng)廢棄。
3.實(shí)例的主窗體Frm_Main:
(1)Frm_Main.Designer.cs
namespace _197
{
partial class Frm_Main
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
menuStrip1 = new MenuStrip();
toolStripMenuItem1 = new ToolStripMenuItem();
toolStripMenuItem2 = new ToolStripMenuItem();
toolStripMenuItem3 = new ToolStripMenuItem();
dataGridView1 = new DataGridView();
menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit();
SuspendLayout();
//
// menuStrip1
//
menuStrip1.BackColor = SystemColors.ControlDark;
menuStrip1.Items.AddRange(new ToolStripItem[] { toolStripMenuItem1 });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(335, 25);
menuStrip1.TabIndex = 0;
menuStrip1.Text = "menuStrip1";
//
// toolStripMenuItem1
//
toolStripMenuItem1.DropDownItems.AddRange(new ToolStripItem[] { toolStripMenuItem2, toolStripMenuItem3 });
toolStripMenuItem1.Name = "toolStripMenuItem1";
toolStripMenuItem1.Size = new Size(44, 21);
toolStripMenuItem1.Text = "操作";
//
// toolStripMenuItem2
//
toolStripMenuItem2.Name = "toolStripMenuItem2";
toolStripMenuItem2.Size = new Size(136, 22);
toolStripMenuItem2.Text = "添加或刪除";
toolStripMenuItem2.Click += ToolStripMenuItem2_Click;
//
// toolStripMenuItem3
//
toolStripMenuItem3.Name = "toolStripMenuItem3";
toolStripMenuItem3.Size = new Size(136, 22);
toolStripMenuItem3.Text = "退出";
toolStripMenuItem3.Click += ToolStripMenuItem3_Click;
//
// dataGridView1
//
dataGridView1.AllowUserToAddRows = false;
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.Location = new Point(0, 25);
dataGridView1.Name = "dataGridView1";
dataGridView1.Size = new Size(335, 178);
dataGridView1.TabIndex = 1;
//
// Frm_Main
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(335, 203);
Controls.Add(dataGridView1);
Controls.Add(menuStrip1);
IsMdiContainer = true;
MainMenuStrip = menuStrip1;
Name = "Frm_Main";
Text = "主窗體";
Load += Frm_Main_Load;
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private MenuStrip menuStrip1;
private ToolStripMenuItem toolStripMenuItem1;
private ToolStripMenuItem toolStripMenuItem2;
private ToolStripMenuItem toolStripMenuItem3;
private DataGridView dataGridView1;
}
}(2)Frm_Main.cs
// 通過子窗體刷新父窗體
// 設(shè)置主窗體的IsMdiContainer=true;
using System.Data;
using Microsoft.Data.SqlClient;
namespace _197
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
#region 聲明的變量
private static bool flag = false; //標(biāo)識(shí)是否創(chuàng)建新的子窗體
readonly Frm_Child BabyWindow = new();//實(shí)例化一個(gè)子窗體
readonly DataSet PubsSet = new(); //定義一個(gè)數(shù)據(jù)集對(duì)象
private static string[]? iDArray; //聲明一個(gè)一維字符串?dāng)?shù)組
public DataTable? IDTable; //聲明一個(gè)數(shù)據(jù)表對(duì)象
SqlDataAdapter? IDAdapter; //聲明一個(gè)數(shù)據(jù)讀取器對(duì)象
SqlDataAdapter? PubsAdapter; //聲明一個(gè)數(shù)據(jù)讀取器對(duì)象
SqlConnection? ConnPubs; //聲明一個(gè)數(shù)據(jù)庫連接對(duì)象
SqlCommand? PersonalInformation; //聲明一個(gè)執(zhí)行SQL語句的對(duì)象
public static string[]? IDArray { get => iDArray; set => iDArray = value; }
public static bool Flag { get => flag; set => flag = value; }
readonly string? ConnString //數(shù)據(jù)庫連接字符串
= "Data Source=DESKTOP-3LV13FS;DataBase=db_TomeOne;integrated security=SSPI;TrustServerCertificate=true;";
#endregion
private void Frm_Main_Load(object? sender, EventArgs e)
{
string AdapterString = "select userID as 編號(hào),userName as 姓名 ,phone as 電話,address as 住址 from tb_User";//用于查詢的字符串
string IDString = "select userID from tb_User";//讀取數(shù)據(jù)庫中用戶的ID編號(hào)字符串
ConnPubs = new SqlConnection(ConnString);//建立數(shù)據(jù)庫連接
PubsAdapter = new SqlDataAdapter(AdapterString, ConnPubs);//創(chuàng)建PubsAdapter數(shù)據(jù)讀取器
IDAdapter = new SqlDataAdapter(IDString, ConnPubs); //用于讀取用戶編號(hào)的讀取器
PubsAdapter.Fill(PubsSet, "tb_User"); //填充PubsSet數(shù)據(jù)集
IDAdapter.Fill(PubsSet, "ID"); //填充PubsSet數(shù)據(jù)集
DataTable PubsTable = PubsSet.Tables["tb_User"]!;//將數(shù)據(jù)寫入PubsTable表
IDTable = PubsSet.Tables["ID"]!; //將數(shù)據(jù)寫入ID表
IDArray = new string[IDTable.Rows.Count]; //為數(shù)組定義最大長度
dataGridView1.DataSource = PubsTable.DefaultView;//設(shè)置dataGridView1的數(shù)據(jù)源
for (int i = 0; i < IDTable.Rows.Count; i++) //循環(huán)遍歷數(shù)據(jù)表中的每一行數(shù)據(jù)
{
for (int j = 0; j < IDTable.Columns.Count; j++)//循環(huán)遍歷數(shù)據(jù)表中的每一列數(shù)據(jù)
{
IDArray[i] = IDTable.Rows[i][j].ToString()!;//將數(shù)據(jù)表中的數(shù)據(jù)添加至一個(gè)一維數(shù)組
}
}
}
#region 創(chuàng)建子窗體的CreateFrmChild方法
/// <summary>
/// 設(shè)置子窗體的父窗體為當(dāng)前窗體;
/// 實(shí)例化一個(gè)子窗體;
/// 在DataGridView控件中添加子窗體;
/// 顯示子窗體;
/// </summary>
public void CreateFrmChild()
{
Frm_Child BabyWindow = new()
{
MdiParent = this,
};
dataGridView1.Controls.Add(BabyWindow);
BabyWindow.UpdateDataGridView += new EventHandler(BabyWindow_UpdateDataGridView!);
BabyWindow.Show();
}
/// <summary>
/// 事件委托
/// </summary>
void BabyWindow_UpdateDataGridView(object sender, EventArgs e)
{
if (Frm_Child.GlobalFlag == false) //當(dāng)單擊刪除按鈕時(shí)
{
if (ConnPubs!.State == ConnectionState.Closed) //當(dāng)數(shù)據(jù)庫處于斷開狀態(tài)時(shí)
{
ConnPubs.Open(); //打開數(shù)據(jù)庫的連接
}
string AfreshString = "delete tb_User where userID=" + Frm_Child.DeleteID!.Trim();//定義一個(gè)刪除數(shù)據(jù)的字符串
PersonalInformation = new SqlCommand(AfreshString, ConnPubs); //執(zhí)行刪除數(shù)據(jù)庫字段
PersonalInformation.ExecuteNonQuery(); //執(zhí)行SQL語句并返回受影響的行數(shù)
ConnPubs.Close(); //關(guān)閉數(shù)據(jù)庫
DisplayData(); //顯示數(shù)據(jù)庫更新后的內(nèi)容
MessageBox.Show("數(shù)據(jù)刪除成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
if (ConnPubs!.State == ConnectionState.Closed) //當(dāng)數(shù)據(jù)庫處于關(guān)閉狀態(tài)時(shí)
{
ConnPubs.Open(); //打開數(shù)據(jù)庫
}
string InsertString = "insert into tb_User values('" + Frm_Child.IdContent + "','" + Frm_Child.NameContent + "','" + Frm_Child.PhoneContent + "','" + Frm_Child.AddressContent + "')";//定義一個(gè)插入數(shù)據(jù)的字符串變量
PersonalInformation = new SqlCommand(InsertString, ConnPubs);//執(zhí)行插入數(shù)據(jù)庫字段
PersonalInformation.ExecuteNonQuery(); //執(zhí)行SQL語句并返回受影響的行數(shù)
ConnPubs.Close(); //關(guān)閉數(shù)據(jù)庫
DisplayData(); //顯示更新后的數(shù)據(jù)
MessageBox.Show("數(shù)據(jù)添加成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
#endregion
#region 用于讀取數(shù)據(jù)庫的DisplayData方法
public void DisplayData()
{
PubsSet.Clear();
ConnPubs = new SqlConnection(ConnString);
string DisplayString = "select userId as 編號(hào),userName as 姓名 ,phone as 電話,address as 住址 from tb_User";//定義讀取數(shù)據(jù)庫的字段
SqlDataAdapter PersonalAdapter = new(DisplayString, ConnPubs); //定義一個(gè)讀取數(shù)據(jù)庫數(shù)據(jù)的讀取器
PersonalAdapter.Fill(PubsSet, "tb_User"); //向表DisplayTable中填充數(shù)據(jù)
dataGridView1.DataSource = PubsSet.Tables["tb_User"]!.DefaultView; //設(shè)定DataGridView控件的數(shù)據(jù)源
}
#endregion
/// <summary>
/// 添加或刪除記錄
/// 創(chuàng)建子窗體的CreateFrmChild方法
/// </summary>
private void ToolStripMenuItem2_Click(object sender, EventArgs e)
{
if (flag == false) //判斷標(biāo)識(shí)的值決定是否創(chuàng)建窗體
{
CreateFrmChild();//創(chuàng)建子窗體
}
for (int i = 0; i < dataGridView1.Controls.Count; i++) //循環(huán)遍歷DataGridView控件上的控件集
{
if (dataGridView1.Controls[i].Name.Equals(BabyWindow.Name)) //當(dāng)存在子窗體時(shí)
{
flag = true; //改變標(biāo)識(shí)Flag的值
break; //退出循環(huán)體
}
}
}
/// <summary>
/// 退出
/// </summary>
private void ToolStripMenuItem3_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}4.實(shí)例的子窗體Frm_Child:
(1) Frm_Child.Designer.cs
namespace _197
{
partial class Frm_Child
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
groupBox1 = new GroupBox();
button2 = new Button();
button1 = new Button();
Record_Address = new TextBox();
Record_Phone = new TextBox();
Record_Name = new TextBox();
Record_ID = new TextBox();
label4 = new Label();
label3 = new Label();
label2 = new Label();
label1 = new Label();
groupBox2 = new GroupBox();
button3 = new Button();
comboBox1 = new ComboBox();
label5 = new Label();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
SuspendLayout();
//
// groupBox1
//
groupBox1.Controls.Add(button2);
groupBox1.Controls.Add(button1);
groupBox1.Controls.Add(Record_Address);
groupBox1.Controls.Add(Record_Phone);
groupBox1.Controls.Add(Record_Name);
groupBox1.Controls.Add(Record_ID);
groupBox1.Controls.Add(label4);
groupBox1.Controls.Add(label3);
groupBox1.Controls.Add(label2);
groupBox1.Controls.Add(label1);
groupBox1.Location = new Point(12, 12);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(270, 157);
groupBox1.TabIndex = 0;
groupBox1.TabStop = false;
groupBox1.Text = "添加信息";
//
// button2
//
button2.Location = new Point(187, 56);
button2.Name = "button2";
button2.Size = new Size(75, 23);
button2.TabIndex = 9;
button2.Text = "清空";
button2.UseVisualStyleBackColor = true;
button2.Click += Button2_Click;
//
// button1
//
button1.Location = new Point(187, 25);
button1.Name = "button1";
button1.Size = new Size(77, 23);
button1.TabIndex = 8;
button1.Text = "提交";
button1.UseVisualStyleBackColor = true;
button1.Click += Button1_Click;
//
// Record_Address
//
Record_Address.Location = new Point(72, 118);
Record_Address.Name = "Record_Address";
Record_Address.Size = new Size(100, 23);
Record_Address.TabIndex = 7;
Record_Address.TextChanged += Record_Address_TextChanged;
//
// Record_Phone
//
Record_Phone.Location = new Point(72, 87);
Record_Phone.Name = "Record_Phone";
Record_Phone.Size = new Size(100, 23);
Record_Phone.TabIndex = 6;
Record_Phone.TextChanged += Record_Phone_TextChanged;
//
// Record_Name
//
Record_Name.Location = new Point(72, 56);
Record_Name.Name = "Record_Name";
Record_Name.Size = new Size(100, 23);
Record_Name.TabIndex = 5;
Record_Name.TextChanged += Record_Name_TextChanged;
//
// Record_ID
//
Record_ID.Location = new Point(72, 25);
Record_ID.Name = "Record_ID";
Record_ID.Size = new Size(100, 23);
Record_ID.TabIndex = 4;
Record_ID.TextChanged += Record_ID_TextChanged;
//
// label4
//
label4.AutoSize = true;
label4.Location = new Point(11, 118);
label4.Name = "label4";
label4.Size = new Size(44, 17);
label4.TabIndex = 3;
label4.Text = "地址:";
//
// label3
//
label3.AutoSize = true;
label3.Location = new Point(11, 87);
label3.Name = "label3";
label3.Size = new Size(44, 17);
label3.TabIndex = 2;
label3.Text = "電話:";
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(11, 56);
label2.Name = "label2";
label2.Size = new Size(44, 17);
label2.TabIndex = 1;
label2.Text = "姓名:";
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(11, 25);
label1.Name = "label1";
label1.Size = new Size(44, 17);
label1.TabIndex = 0;
label1.Text = "編號(hào):";
//
// groupBox2
//
groupBox2.Controls.Add(button3);
groupBox2.Controls.Add(comboBox1);
groupBox2.Controls.Add(label5);
groupBox2.Location = new Point(12, 175);
groupBox2.Name = "groupBox2";
groupBox2.Size = new Size(270, 54);
groupBox2.TabIndex = 1;
groupBox2.TabStop = false;
groupBox2.Text = "刪除信息";
//
// button3
//
button3.Location = new Point(187, 24);
button3.Name = "button3";
button3.Size = new Size(75, 23);
button3.TabIndex = 2;
button3.Text = "刪除";
button3.UseVisualStyleBackColor = true;
button3.Click += Button3_Click;
//
// comboBox1
//
comboBox1.FormattingEnabled = true;
comboBox1.Location = new Point(72, 24);
comboBox1.Name = "comboBox1";
comboBox1.Size = new Size(100, 25);
comboBox1.TabIndex = 1;
//
// label5
//
label5.AutoSize = true;
label5.Location = new Point(11, 30);
label5.Name = "label5";
label5.Size = new Size(44, 17);
label5.TabIndex = 0;
label5.Text = "編號(hào):";
//
// Frm_Child
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(294, 241);
Controls.Add(groupBox2);
Controls.Add(groupBox1);
MaximizeBox = false; //最大化按鈕沒有意義
Name = "Frm_Child";
StartPosition = FormStartPosition.CenterScreen;
Text = "子窗體";
FormClosing += Frm_Child_FormClosing;
Load += Frm_Child_Load;
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
ResumeLayout(false);
}
#endregion
private GroupBox groupBox1;
private TextBox Record_Address;
private TextBox Record_Phone;
private TextBox Record_Name;
private TextBox Record_ID;
private Label label4;
private Label label3;
private Label label2;
private Label label1;
private GroupBox groupBox2;
private Button button2;
private Button button1;
private Button button3;
private ComboBox comboBox1;
private Label label5;
}
}(2)Frm_Child.cs
// 本實(shí)例僅可創(chuàng)建一個(gè)窗口:
// 最大化后沒有實(shí)際意義,因此關(guān)閉MaximizeBox屬性值為False
namespace _197
{
public partial class Frm_Child : Form
{
#region 變量的聲明
public event EventHandler? UpdateDataGridView = null;//定義一個(gè)處理更新DataGridView控件內(nèi)容的方法
private static string? deleteID; //定義一個(gè)表示刪除數(shù)據(jù)編號(hào)的字符串
private static string? idContent; //該變量用來存儲(chǔ)數(shù)據(jù)編號(hào)
private static string? nameContent; //該變量用來存儲(chǔ)姓名
private static string? phoneContent; //該變量用來存儲(chǔ)電話
private static string? addressContent; //該變量用來存儲(chǔ)住址
private static bool globalFlag; //該變量用來標(biāo)識(shí)是否創(chuàng)建新的子窗體
public static string? DeleteID { get => deleteID; set => deleteID = value; }
public static string? IdContent { get => idContent; set => idContent = value; }
public static string? NameContent { get => nameContent; set => nameContent = value; }
public static string? PhoneContent { get => phoneContent; set => phoneContent = value; }
public static string? AddressContent { get => addressContent; set => addressContent = value; }
public static bool GlobalFlag { get => globalFlag; set => globalFlag = value; }
#endregion
public Frm_Child()
{
InitializeComponent();
}
/// <summary>
/// 添加
/// </summary>
private void Button1_Click(object? sender, EventArgs e)
{
GlobalFlag = true; //設(shè)定標(biāo)識(shí)的值為true
if (!(comboBox1.Items.Equals(idContent))) //當(dāng)Combobox控件中不存在將添加的信息時(shí)
{
comboBox1.Items.Add(idContent!); //在Combobox控件中添加一條記錄
}
UpdateData();
}
/// <summary>
/// 清空
/// </summary>
private void Button2_Click(object? sender, EventArgs e)
{
Record_ID.Clear(); //清空編號(hào)文本框中的內(nèi)容
Record_Name.Clear(); //清空姓名文本框中的內(nèi)容
Record_Phone.Clear(); //清空電話號(hào)碼文本框中的內(nèi)容
Record_Address.Clear(); //清空居住地址文本框中的內(nèi)容
Record_ID.Focus(); //設(shè)定當(dāng)前鼠標(biāo)的焦點(diǎn)在編號(hào)文本框中
}
/// <summary>
/// 刪除
/// </summary>
private void Button3_Click(object? sender, EventArgs e)
{
GlobalFlag = false; //設(shè)定全局變量表示為false
if (comboBox1.Items.Count > 1) //當(dāng)ComboBox中剩不小于2條內(nèi)容時(shí)
{
DeleteID = comboBox1.SelectedItem!.ToString(); //將選中項(xiàng)轉(zhuǎn)化為int型
if (comboBox1.Items.Count != 0) //當(dāng)ComboBox中剩1條內(nèi)容時(shí)
{
comboBox1.Items.Remove(comboBox1.SelectedItem);
comboBox1.SelectedIndex = 0;
}
}
UpdateData();
}
/// <summary>
/// 保存新添加記錄的編號(hào)
/// </summary>
private void Record_ID_TextChanged(object? sender, EventArgs e)
{
idContent = Record_ID.Text;
}
/// <summary>
/// 保存填入的姓名
/// </summary>
private void Record_Name_TextChanged(object? sender, EventArgs e)
{
nameContent = Record_Name.Text;
}
/// <summary>
/// 保存填入的電話號(hào)碼
/// </summary>
private void Record_Phone_TextChanged(object? sender, EventArgs e)
{
phoneContent = Record_Phone.Text;
}
/// <summary>
/// 保存填入的地址信息
/// </summary>
private void Record_Address_TextChanged(object? sender, EventArgs e)
{
addressContent = Record_Address.Text;
}
/// <summary>
/// 循環(huán)遍歷數(shù)組中的每一個(gè)元素,向Combobox控件中添加內(nèi)容
/// 默認(rèn)設(shè)定當(dāng)前選中項(xiàng)的索引為0
/// </summary>
private void Frm_Child_Load(object? sender, EventArgs e)
{
for (int i = 0; i < Frm_Main.IDArray!.Length; i++)
{
comboBox1.Items.Add(Frm_Main.IDArray[i].ToString());
comboBox1.SelectedIndex = 0;
}
}
/// <summary>
/// 設(shè)定該值為false表示可以創(chuàng)建新窗體
/// </summary>
private void Frm_Child_FormClosing(object? sender, FormClosingEventArgs e)
{
Frm_Main.Flag = false;
}
/// <summary>
/// 更新DataGridView控件中的內(nèi)容
/// </summary>
protected void UpdateData()
{
UpdateDataGridView?.Invoke(this, EventArgs.Empty);
}
}
}5.生成效果
(1)生成

(2)添加

(3)添加后

(4)清空

以上就是C#通過子窗體刷新父窗體的實(shí)現(xiàn)方法的詳細(xì)內(nèi)容,更多關(guān)于C#子窗體刷新父窗體的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
VB.NET中TextBox的智能感知應(yīng)用實(shí)例
這篇文章主要介紹了VB.NET中TextBox的智能感知應(yīng)用實(shí)例,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
C#實(shí)現(xiàn)文件讀寫到SQLite數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了使用?C#?將文件讀寫到?SQLite?數(shù)據(jù)庫的幾種方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2025-01-01
WPF實(shí)現(xiàn)雷達(dá)圖(仿英雄聯(lián)盟)的示例代碼
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)雷達(dá)圖(仿英雄聯(lián)盟)的繪制,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-07-07
C#循環(huán)與循環(huán)控制的表達(dá)式樹實(shí)現(xiàn)
這篇文章介紹了C#循環(huán)與循環(huán)控制的表達(dá)式樹實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
C#實(shí)現(xiàn)DVD借出歸還管理系統(tǒng)
這篇文章主要介紹了C#實(shí)現(xiàn)DVD借出歸還管理系統(tǒng),類似DVD管理器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
C#使用struct直接轉(zhuǎn)換下位機(jī)數(shù)據(jù)的示例代碼
這篇文章主要介紹了C#使用struct直接轉(zhuǎn)換下位機(jī)數(shù)據(jù)的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01

