Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3302404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:50:53+00:00 2026-05-17T20:50:53+00:00

Someone gave me this code that works great. But I would really like to

  • 0

Someone gave me this code that works great. But I would really like to understand what is happening inside it. Could somebody explain please? What is the meaning of each part of the code? The code is inside a custom control which has two labels inside a panel.

Also I’ve seen some custom control events that use add/remove syntax, what is that for? What is the difference with what is happening here?

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public event EventHandler MyCustomClickEvent;

    protected virtual void OnMyCustomClickEvent(EventArgs e)
    {
        // Here, you use the "this" so it's your own control. You can also
        // customize the EventArgs to pass something you'd like.

        if (MyCustomClickEvent != null)
            MyCustomClickEvent(this, e);
    }

    private void label1_Click(object sender, EventArgs e)
    {
        OnMyCustomClickEvent(EventArgs.Empty);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:50:54+00:00Added an answer on May 17, 2026 at 8:50 pm

    See my comments below. Also for a more detailed event I blogged on this concept a while back where I go into more detail on the entire process.

    public partial class UserControl1 : UserControl
    {
        //This is the standard constructor of a user control
        public UserControl1()
        {
            InitializeComponent();
        }
    
        //This defines an event called "MyCustomClickEvent", which is a generic
        //event handler.  (EventHander is a delegate definition that defines the contract
        //of what information will be shared by the event.  In this case a single parameter
        //of an EventArgs object.
        public event EventHandler MyCustomClickEvent;
    
    
        //This method is used to raise the event, when the event should be raised, 
        //this method will check to see if there are any subscribers, if there are, 
        //it raises the event
        protected virtual void OnMyCustomClickEvent(EventArgs e)
        {
            // Here, you use the "this" so it's your own control. You can also
            // customize the EventArgs to pass something you'd like.
    
            if (MyCustomClickEvent != null)
                MyCustomClickEvent(this, e);
        }
    
        private void label1_Click(object sender, EventArgs e)
        {
            OnMyCustomClickEvent(EventArgs.Empty);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I posted this yesterday on SO, and I received an answer that works great,
Someone told me about swamp diagrams explaning that they were useful to predict code
Someone posted a great little function here the other day that separated the full
Someone please correct me if I'm wrong, but parsing a yyyy/MM/dd (or other specific
Someone told me that it's faster to concatenate strings with StringBuilder. I have changed
Someone left the organisation but before leaving, he locked all the files for an
Someone on here recently recommended BWToolkit, and it really impressed me, so I started
I've posted some questions on this before, but it's different. So consider a small
This is stemming from a bad answer I gave last night. The curiosity as
I have a userscript (read: my Javascript on someone else's site) that allows users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.