简体   繁体   中英

C#, User Control, Events - User Control's Control Event Overriding?

In my C# program, I have a Custom Control with a label and a button. How can I setup my control so that when a user clicks the button it overrides the Custom Control's Click event?

Simply call the control's OnClick() method:

    private void button1_Click(object sender, EventArgs e) {
        this.OnClick(e);
    }

Which fires the control's Click event.

You can write it like this:

public new event EventHandler Click {
  add { button1.Click += value; }
  remove { button1.Click -= value; }
}

Here is full article which explain you how you can achieve it : Exposing Custom event from custom control

Following is step for the drodown used in user control and exposing event you need to do same for you button , you will get more idea after reading above link

public event EventHandler DrpChange;

public virtual void OnDropDownChange()
    {
        if (DrpChange != null)
        {
            this.DrpChange(this, EventArgs.Empty);
        }
    }

protected void ddlDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.OnDropDownChange();
    }

<uc1:commondropdowncontrol autopostback="true" drpchange="usrDrp_DrpChange" id="usrDrp" labletext="Country" runat="server">
    </uc1:commondropdowncontrol></div>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM