0

I was wondering how I can override the default methods of form controls? This question arose from the following goal:

I have a picturebox. When I call its .Refresh() method, it will do some procedures under the hood, including triggering the picturebox's Paint event. Basically I want to add an argument for the Paint bearing a condition based on which the method should do different tasks.

After all, seems like I would need to override the .Refresh() method so that I could pass an argument from there. How can I do that?

Thanks

  • Your Refresh wont interact with the Paint event - or call it - Windows does that. Subclassing the control might be what you are looking for. – Ňɏssa Pøngjǣrdenlarp Jul 10 '16 at 20:09
  • @Plutonix What method should I override and how should I pass an argument to that method? – Arash Vahabpour Jul 10 '16 at 20:12
  • 1
    Probably what you are after is overriding the `OnPaint` method: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint%28v=vs.110%29.aspx and yes, do inherit from PictureBox to override. – Stefano d'Antonio Jul 10 '16 at 20:25
  • You cannot change the method signature itself. However, you can subscribe to the event, and use a wrapper as the handler that calls some other method with additional parameters. See various answers on the marked duplicate, including http://stackoverflow.com/a/34622752 which includes the kind of answer I'd post here if your question wasn't a duplicate (not surprising, as it happens that answer author referenced a very old post of mine on the same topic :) ). – Peter Duniho Jul 10 '16 at 21:11
  • I will also note that calling the `Refresh()` method of a control is almost always the wrong thing to do. It's usually done in scenarios where someone has already made the mistake of blocking the UI thread, and they are trying to fix it the wrong way (by forcing an immediate redraw, rather than simply not blocking the UI thread). If you would like help avoiding the call to `Refresh()`, or you are still having trouble understanding the guidance in the marked duplicate, post a new question with a good [mcve] showing exactly what problem you're having. – Peter Duniho Jul 10 '16 at 21:13

0 Answers0