Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a C# project wherein i have registered checkox event.When the checkbox is Unchecked i am displaying a message box which will prompt the user to confirm,if the user does not confirm then the checkbox should retain the state(Checked) So in code i try to check it which again raises the same event.
Is there a way to change the value without raising the event
I know this can be done by un-registering the event,set the value then re-register.but i am looking for a cleaner way. :doh:
Posted

public class MyCheckEventArgs: EventArgs {
    public bool CurrentState { get; protected set; }
    public bool CancelEvent { get; set; }

    public MyCheckEventArgs(bool currentState) {
        CancelEvent = false;
        CurrentState = currentState;
    }
}

public class MyCheck : CheckBox {
    public EventHandler<MyCheckEventArgs> BeforeChecked;

    protected override void OnClick(EventArgs e) {
        bool handled = false;
        if (BeforeChecked != null) {
            MyCheckEventArgs myCheckEventArgs = new MyCheckEventArgs(this.Checked);
            BeforeChecked(this, myCheckEventArgs);
            handled = myCheckEventArgs.CancelEvent;
        }

        if (!handled)
            base.OnClick(e);
    }
}


I'm sure the rest you can do by yourself.

Regards,
Alexey
 
Share this answer
 
There are multiple reasons why the code someone else posted will not work.

if (UserChangedMind)
        CheckBox1.Checked = true;
    UserChangedMind = false;


is just one of them UserChangedMind is only ever true for a microsecond, as the second assignment always runs.
 
Share this answer
 
Try it this way in your form class:


private bool initialized = false;

public MyForm()
{
    InitializeComponents();
    this.initialized = true;
}

void checkbox_clickevent(object sender, EventArgs e)
{
    // initialized prevents the message box from being displayed in the 
    // setting is loaded at startup that results in the checkbox being 
    // unchecked.
    if (initialized)
    {
        CheckBox checkBox = sender as CheckBox;
        // this will only ex3cute if the checkbox is unchecked by the user
        if (!checkBox.Checked)
        {
            if (!UserVerified())
            {
                checkBox.Checked = true;
            }
        }
    }
}


You may need to tweak this a bit, but I think you get the idea.
 
Share this answer
 
v3
You can achieve it easliy using Javascipt.

1. Add a onclick/onchange javascript event to the checkbox
2. In the event raised - handle the scenario
a. Checkbox unchecked - leave as it is procceed
b. Checkbox checked - prompt a 'confirm' box. Ask for confirmation. If Yes: return true and move on. such that the server onchange event is fired. Take appropriate action needed there.
If No: toggle the checkbox value back and return false, move on. Such that the server event is no more fired and the click/confirm was all on the client side.

Hope that helps
 
Share this answer
 
Well, you're asking for the event framework to break itself. No, you can't do that. Sadly, and it's not pretty, the only way I know of to do stuff like that, is to set some sort of flag that the event code knows to check, in order to ignore the event.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900