C# 当';X';是否单击子窗体?
我只使用C#大约一个星期,对编程基本上还是新手 我有两张表格。在家长表单上,我有两个按钮-添加和编辑。这些按钮打开一个子窗体,该窗体也有两个按钮-保存和取消 当在子表单上单击Cancelled时,我在代码中设置了一个指示符变量,将其标记为true:C# 当';X';是否单击子窗体?,c#,forms,C#,Forms,我只使用C#大约一个星期,对编程基本上还是新手 我有两张表格。在家长表单上,我有两个按钮-添加和编辑。这些按钮打开一个子窗体,该窗体也有两个按钮-保存和取消 当在子表单上单击Cancelled时,我在代码中设置了一个指示符变量,将其标记为true: //Indicator on child form; setup in properties. public bool frmCloseInd { get; set; } 在“取消”按钮上设置指示器,以便在单击事件时设置为“真”: //Cancel
//Indicator on child form; setup in properties.
public bool frmCloseInd { get; set; }
在“取消”按钮上设置指示器,以便在单击事件时设置为“真”:
//Cancel Event on child form
private void but_DM_Cancel_Click(object sender, EventArgs e)
{
frmCloseInd = true;
Close();
}
frmCloseInd值被传递回表单的父级。运行IF语句以确定要运行的代码。问题是,当单击X时,它仍然运行与保存时相同的代码。单击X时,如何将true传递给frmCloseInd
我尝试执行关闭事件,但它在“保存”和“取消”按钮中的“关闭”()中启动
以下是父级表单上的添加按钮单击事件:
private void but_DM_Add_Click(object sender, EventArgs e)
{
//instance of child form is called
frmDirectoryManagerAARecord frm = new frmDirectoryManagerAARecord();
//Setting properties specific to add button
frm.Text = "Add Contract";
frm.ShowDialog(this);
if (frm.frmCloseInd == true)
{
//indicates the user canceled out of the userform. No action performed.
}
else
{
if (frm.frmAnalytic_ID == 0 || frm.frmClient_ID == 0 || frm.frmProject_ID == 0)
{
MessageBox.Show("Required values were missing. Analytic ID, Client ID and Project ID are required for entry.");
}
else
{
MessageBox.Show(frm.frmAnalytic_ID + " " + frm.frmClient_ID + " " + frm.frmProject_ID + " " + frm.frmDescrip);
try
{
string database = "DB_NAME";
string query_insert = @"INSERT INTO [dbo].[TestTable]
(Analytic_ID, Master_Client_ID, Project_ID, Description, Active)
VALUES(" + frm.frmAnalytic_ID + ", " + frm.frmClient_ID + ", " + frm.frmProject_ID + ", '" + frm.frmDescrip + "', 1)";
//calling instance of class ContractSearch to use method connectToDatabase
ContractSearch con = new ContractSearch();
con.connectToDatabase(database, query_insert);
//reloads gridview on parent form
Reload_gvDirectoryManager();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
}
private void but_DM_Save_Click(object sender, EventArgs e)
{
try
{
frmAnalytic_ID = Convert.ToInt32(tbox_DM_Analytic_ID.Text);
frmClient_ID = Convert.ToInt32(tbox_DM_Client_ID.Text);
frmProject_ID = Convert.ToInt32(tbox_DM_Project_ID.Text);
frmDescrip = tbox_DM_Descrip.Text;
Close();
}
catch(Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
以及子窗体上保存按钮的代码:
private void but_DM_Add_Click(object sender, EventArgs e)
{
//instance of child form is called
frmDirectoryManagerAARecord frm = new frmDirectoryManagerAARecord();
//Setting properties specific to add button
frm.Text = "Add Contract";
frm.ShowDialog(this);
if (frm.frmCloseInd == true)
{
//indicates the user canceled out of the userform. No action performed.
}
else
{
if (frm.frmAnalytic_ID == 0 || frm.frmClient_ID == 0 || frm.frmProject_ID == 0)
{
MessageBox.Show("Required values were missing. Analytic ID, Client ID and Project ID are required for entry.");
}
else
{
MessageBox.Show(frm.frmAnalytic_ID + " " + frm.frmClient_ID + " " + frm.frmProject_ID + " " + frm.frmDescrip);
try
{
string database = "DB_NAME";
string query_insert = @"INSERT INTO [dbo].[TestTable]
(Analytic_ID, Master_Client_ID, Project_ID, Description, Active)
VALUES(" + frm.frmAnalytic_ID + ", " + frm.frmClient_ID + ", " + frm.frmProject_ID + ", '" + frm.frmDescrip + "', 1)";
//calling instance of class ContractSearch to use method connectToDatabase
ContractSearch con = new ContractSearch();
con.connectToDatabase(database, query_insert);
//reloads gridview on parent form
Reload_gvDirectoryManager();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
}
private void but_DM_Save_Click(object sender, EventArgs e)
{
try
{
frmAnalytic_ID = Convert.ToInt32(tbox_DM_Analytic_ID.Text);
frmClient_ID = Convert.ToInt32(tbox_DM_Client_ID.Text);
frmProject_ID = Convert.ToInt32(tbox_DM_Project_ID.Text);
frmDescrip = tbox_DM_Descrip.Text;
Close();
}
catch(Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
解决问题的最简单方法是将
frmCloseInd
属性的初始值设置为true
:
// Default value TRUE for this property indicates that by default we mean
// that the form will be closed using Cancel or X button. If user clicks
// Save button then we set this property to FALSE.
public bool frmCloseInd { get; set; } = true;
此外,我们应该在Save
和Close
事件处理程序中为此属性设置适当的值(如果您有FormClose
事件处理程序,则不能更改其中属性frmCloseInd
的值):
作为旁注,我想指出,要在WinForms中实现此类功能,最好使用。请参阅我的双表单项目: