How to ignore a checkbox change in listview?
I have a listview with a the property checkbox = true.
When the user clicks on the checkbox and changes its state (chec开发者_StackOverflowked -> unchecked or unchecked -> checked), I catch the ItemCheck event and do some DB implementation.
I want to ask the user for confirmation before working with the DB.
When I the user cancel it's command, I want that the checkbox will return to it's status.
How can I tell the listview to ignore the state change of the checkbox?
Thank,
Mattan
In the ItemCheck
event, set the NewValue
to the CurrentValue
:
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (MessageBox.Show(this, "Change?", "Test", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
e.NewValue = e.CurrentValue;
}
Check out the OnClientClick attribute of the checkBox:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=93&AspxAutoDetectCookieSupport=1
Using this you can cancel the postback, as well as set the value of the checkbox back to what it was before. If you use a templatefield instead of the checkbox=true property, you can add the OnClientClick attribute to the checkbox there; otherwise you need to add it dynamically in the ListView ItemDataBound event.
EDIT Oops, didn't see the "winforms" tag; thought this was ASP.Net (which also has a ListView control). Kindly disregard.
I used the OnChecked event instead of OnCheck.
To cancel, I just set the value to the !value.
精彩评论