Hi! I'm a newbie and i have kind of a teoric doubt.
I wonder where the functions/events truly reside. For example, when i doubleclick a form or a button, i can see in the events properties that a function is created in my code (private void Form1_Load(object sender, EventArgs e), for example).
So I imagine there some events that can be coded like this one, I just have to make sure that the name i give it to is the same that there is in the event properties section (in this case, Form1_Load).

But what about those that aren't created from that event properties section? For example i downloaded some sample code that does simple graphics, here's one snippet:

protected override void OnMouseDown(MouseEventArgs e)
        mouseDown = true;
        if (e.Button == MouseButtons.Right)
            ContextMenu m = new ContextMenu();
            m.MenuItems.Add(0, new MenuItem("black", new EventHandler(RightMouseButton_Click)));
            m.MenuItems.Add(1, new MenuItem("white", new EventHandler(RightMouseButton_Click)));
            m.MenuItems.Add(2, new MenuItem("red", new EventHandler(RightMouseButton_Click)));
            m.MenuItems.Add(3, new MenuItem("green", new EventHandler(RightMouseButton_Click)));
            m.MenuItems.Add(4, new MenuItem("blue", new EventHandler(RightMouseButton_Click)));
            m.Show(this, new Point(e.X, e.Y));

    protected void RightMouseButton_Click(object sender, EventArgs e)
        color = ((MenuItem)sender).Text;
        p = new Pen(Color.FromName(color));

So ok, the override does just that, override the existing OnMouseDown function in the Form class.
But what about RightMouseButton_Click? I can't see it in the event properties like i would with a Form1_Load or Button1_Click.
Where is it exactly? Where does it come from? It's not a regular function because if i change its name it will say that it doesn't exist in the current context. But if it does exist already, why isn't it written with the "override" parameter?

Thank you and excuse my bad english and my newbieness :P

Event handler methods need to be attached to the events.
The form designer hides the attachment from you by putting them in the InitializeComponent() method which is found in the form1.designer.cs file.

To attach an event handler using the designer:
Select the control with the event.
Click on the Event button at the top of the Properties window (the little lightning button)
Find the event you need.
Select the method from the list provided in the drop-down.
Or to create a new handler double click on the event name.

To do it using code see [POST=1250258]this post[/POST].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.