积极答复者
关闭窗体前如何取消控件的验证

问题
-
对datagridview单元格中的数据进行验证,并弹出对话框,目前在关闭窗体时也会进行验证和弹出对话框,如何在关闭窗体时取消验证对话框直接关闭窗体?
private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == 1) { if (int.Parse(e.FormattedValue.ToString()) > 10 || int.Parse(e.FormattedValue.ToString()) < 0) { e.Cancel = true; MessageBox.Show("输入字符不正确。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = false; }
努力~
答案
-
对datagridview单元格中的数据进行验证,并弹出对话框,目前在关闭窗体时也会进行验证和弹出对话框,如何在关闭窗体时取消验证对话框直接关闭窗体?
private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == 1) { if (int.Parse(e.FormattedValue.ToString()) > 10 || int.Parse(e.FormattedValue.ToString()) < 0) { e.Cancel = true; MessageBox.Show("输入字符不正确。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = false; }
努力~
再给你追加一种方式,实现“关闭窗体时取消验证对话框直接关闭窗体”,注意,就是你点击了“关闭”按钮后,不弹出任何对话框,而不论你的 DataGridView 是否处于编辑状态,根据此方法,你可以灵活来运用,以实现允许用户先确认或退出等功能:
private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 1)
{
if (int.Parse(e.FormattedValue.ToString()) > 10 || int.Parse(e.FormattedValue.ToString()) < 0)
{
// e.Cancel = true; 我们不需要这个了。
MessageBox.Show("输入字符不正确。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// e.Cancel = false; 我们也不需要这个了。
}protected override void WndProc(ref Message m)
{
int WM_SYSCOMMAND =0x0112;
int SC_CLOSE= 0xF060;if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE)
{
this.dataGridView1.CancelEdit(); // 不管三七二十一,先取消单元格的编辑状态。
}base.WndProc(ref m);
}- 已标记为答案 zjyh16 2013年11月5日 8:43
全部回复
-
namespace CSharp { public partial class Form1 : Form { public Form1() { InitializeComponent(); DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(string)); for (int i = 0; i < 11; i++) { dt.Rows.Add(i.ToString()); } dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = dt; } private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == 0) { int errorNum = 0; if (int.TryParse(e.FormattedValue.ToString(), out errorNum)) { if (errorNum >= 10 || errorNum < 0) { MessageBox.Show("数字范围错误!"); //加了这句,关闭窗体照样强制改正! //e.Cancel=true; dataGridView1.Rows[e.RowIndex].Cells[0].ErrorText = "数字范围错误"; } else { dataGridView1.Rows[e.RowIndex].Cells[0].ErrorText = string.Empty; } } else { MessageBox.Show("必须输入数字!"); //加了这句,关闭窗体照样强制改正! //e.Cancel=true; dataGridView1.Rows[e.RowIndex].Cells[0].ErrorText = "必须输入数字"; } } } } }
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
namespace CSharp { public partial class Form1 : Form { public Form1() { InitializeComponent(); DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(string)); for (int i = 0; i < 11; i++) { dt.Rows.Add(i.ToString()); } dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = dt; } private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == 0) { int errorNum = 0; if (int.TryParse(e.FormattedValue.ToString(), out errorNum)) { if (errorNum >= 10 || errorNum < 0) { MessageBox.Show("数字范围错误!"); //加了这句,关闭窗体照样强制改正! //e.Cancel=true; dataGridView1.Rows[e.RowIndex].Cells[0].ErrorText = "数字范围错误"; } else { dataGridView1.Rows[e.RowIndex].Cells[0].ErrorText = string.Empty; } } else { MessageBox.Show("必须输入数字!"); //加了这句,关闭窗体照样强制改正! //e.Cancel=true; dataGridView1.Rows[e.RowIndex].Cells[0].ErrorText = "必须输入数字"; } } } } }
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report努力~
-
你不必加上e.Cancel=true,请删除Form_Closing事件。
另外注意我代码注释的部分,你去掉注释看看就明白了,出错自动会强制不允许关闭界面的。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已编辑 ThankfulHeartModerator 2013年6月25日 5:44 补充说明
-
你不必加上e.Cancel=true,请删除Form_Closing事件。
另外注意我代码注释的部分,你去掉注释看看就明白了,出错自动会强制不允许关闭界面的。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report不好意思,可能我的意思没讲清楚。不加上e.Cancel=true,就无法将焦点保持在当前单元格,而且我并不希望阻止关闭窗体,而是希望即使有错误也能顺利关闭窗体。
我的意思是:输入数据有误,焦点保持在当前单元格,并弹出错误提示框,并且此时想要关闭窗体的话就顺利关闭窗体并不提示错误。
努力~
-
首先很抱歉,你的逻辑我不敢苟同:
如果你输入的数据是错误的,不应当允许关闭界面,因为DataGridView会将错误信息记录到DataTable中去,这个很容易引发写入数据库导致错误。
此外,如果你一定这样做,我还是强调我一点:
1)必须删除Form_Closing,否则界面无法正常关闭。
2)另外附上我的代码,你亲自一试便知:
namespace CSharp { public partial class Form1 : Form { public Form1() { InitializeComponent(); DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(string)); for (int i = 0; i < 11; i++) { dt.Rows.Add(i.ToString()); } dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = dt; } private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == 0) { int errorNum = 0; if (int.TryParse(e.FormattedValue.ToString(), out errorNum)) { if (errorNum >= 10 || errorNum < 0) { MessageBox.Show("数字范围错误!"); //加了这句,关闭窗体照样强制改正! //e.Cancel=true; dataGridView1.Rows[e.RowIndex].Cells[0].ErrorText = "数字范围错误"; } else { dataGridView1.Rows[e.RowIndex].Cells[0].ErrorText = string.Empty; } } else { MessageBox.Show("必须输入数字!"); //加了这句,关闭窗体照样强制改正! //e.Cancel=true; dataGridView1.Rows[e.RowIndex].Cells[0].ErrorText = "必须输入数字"; } } } } }
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
一、我用datagridview这个控件,但是这个程序并跟数据库无关,仅仅只是使用这个控件而已,所以不用担心错误数据输入到数据库中。所以我希望能直接关闭程序,因为我可能突然发现其实不需要运行这个程序了。
二、其次,你的代码我是试过了。由于你这一句e.Cancel=true是注释掉的,在出现错误的数据格式,弹出对话框后点击“确定”按钮后,单元格就失去编辑状态了,而我希望的是该单元格继续保持编辑状态直到输入的数据正确。
三、但是如果不注释e.Cancel=true,就会阻止关闭窗体,即使在Form_Closing加入e.Cancel=false,也依然会弹出MessageBox。
努力~
-
e.Cancel=true这一句貌似不能存在在Cell的验证事件中,否则窗体无法正常关闭。
如果你不希望MsgBox弹出来,那么可以连同把这些一起注释掉,只要在单元格上显示红色的“!”应该就可以提示用户了,在Update按钮事件中检测行列是否带有错误即可,没有才允许更新,这样既可以保证更新时没有错误,同时也可以关闭窗体。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
对datagridview单元格中的数据进行验证,并弹出对话框,目前在关闭窗体时也会进行验证和弹出对话框,如何在关闭窗体时取消验证对话框直接关闭窗体?
private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == 1) { if (int.Parse(e.FormattedValue.ToString()) > 10 || int.Parse(e.FormattedValue.ToString()) < 0) { e.Cancel = true; MessageBox.Show("输入字符不正确。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = false; }
努力~
虽然你可能还需要更精细的控制,以提示用户是否在关闭窗体前需要保存修改,但是下面的代码可以让你看到你想要的结果:
private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 1)
{
if (int.Parse(e.FormattedValue.ToString()) > 10 || int.Parse(e.FormattedValue.ToString()) < 0)
{
//e.Cancel = true; 这个不要。
MessageBox.Show("输入字符不正确。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// e.Cancel = false; 这个也不要。this.dataGridView1.CancelEdit(); // 不管三七二十一,先取消单元格的编辑状态。
} -
对datagridview单元格中的数据进行验证,并弹出对话框,目前在关闭窗体时也会进行验证和弹出对话框,如何在关闭窗体时取消验证对话框直接关闭窗体?
private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == 1) { if (int.Parse(e.FormattedValue.ToString()) > 10 || int.Parse(e.FormattedValue.ToString()) < 0) { e.Cancel = true; MessageBox.Show("输入字符不正确。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = false; }
努力~
再给你追加一种方式,实现“关闭窗体时取消验证对话框直接关闭窗体”,注意,就是你点击了“关闭”按钮后,不弹出任何对话框,而不论你的 DataGridView 是否处于编辑状态,根据此方法,你可以灵活来运用,以实现允许用户先确认或退出等功能:
private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 1)
{
if (int.Parse(e.FormattedValue.ToString()) > 10 || int.Parse(e.FormattedValue.ToString()) < 0)
{
// e.Cancel = true; 我们不需要这个了。
MessageBox.Show("输入字符不正确。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// e.Cancel = false; 我们也不需要这个了。
}protected override void WndProc(ref Message m)
{
int WM_SYSCOMMAND =0x0112;
int SC_CLOSE= 0xF060;if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE)
{
this.dataGridView1.CancelEdit(); // 不管三七二十一,先取消单元格的编辑状态。
}base.WndProc(ref m);
}- 已标记为答案 zjyh16 2013年11月5日 8:43
-
CellValidating中不用e.Cancel = true的话,验证通不过时如何阻止单元格离开?LZ要的是又要阻止离开,又要能关闭窗体。其实MS已经给出办法,就是在FormClosing中加入e.Cancel = false
- 已标记为答案 ThankfulHeartModerator 2013年8月24日 7:04
- 取消答案标记 ThankfulHeartModerator 2013年8月24日 7:04
- 已建议为答案 ThankfulHeartModerator 2013年8月24日 7:04
-
CellValidating中不用e.Cancel = true的话,验证通不过时如何阻止单元格离开?LZ要的是又要阻止离开,又要能关闭窗体。其实MS已经给出办法,就是在FormClosing中加入e.Cancel = false
你试过?在DataGridView出错情况下照样可以关闭?If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -