Younghyo Kim
Younghyo Kim

Reputation: 373

Cancelling event invoking sequence in previous invoked method

I made a derived class from Button control class. When I use the control, I need to make to be prevented from invocation in some situation. The situation is already defined in the derived class, myClick function. I guessed there is a way like setting e.Cancel = true, but I can't. Can you give a simple suggestion to solve this task?

public class SButton : Button
{
    public SButton() : base()
    {
        Click += new System.EventHandler(myClick);
    }


    private void myClick(object sender, EventArgs e)
    {
        if( meHandsome )
        {
            // here I want to prevent no more event invocation!
        }
    }
}



public partial class UTeachAdvanced : DevExpress.XtraEditors.XtraUserControl
{
    private void UTeachAdvanced_Load(object sender, EventArgs e)
    {
        SButton btn = new SButton();
        Controls.Add(btn);
        btn.Click += new EventHandler(delegate(object sender, EventArgs args)
        {
            Console.Write("ugly");
        }
    }
}

Upvotes: 1

Views: 73

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

The situation is already defined in the derived class.

The Click event raises by Control.OnClick method. To prevent raising Click event, you can override OnClick and call base.OnClick only if the criteria to prevent the click is not true:

public class SampleButton : Button
{
    protected override void OnClick(EventArgs e)
    {
        if(!criteriaToPrevent)
            base.OnClick(e);
    }
}

Upvotes: 1

Related Questions