45086

C# WinForms - Paint method questions

Question:

I am not sure what is the best way of using graphics - should I attach my classes to main form Paint event and then do the drawing, or it is better to call it from overidden OnPaint void like this? I mean, is it OK to do that like this:

protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e) //what is this good for? My app works without it as well Graphics g=e.Graphics; DrawEnemies(g); UpdateHUD(g); DrawSelectedUnit(g); }

Answer1:

It is recommended that controls override the On... methods rather than subscribe to their own events.

You should call base.OnPaint to ensure the Paint method is fired properly.

<a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx" rel="nofollow">From MSDN</a>:

<blockquote>

The OnPaint method also enables derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors<br /> When overriding OnPaint in a derived class, be sure to call the base class's OnPaint method so that registered delegates receive the event.

</blockquote>

Answer2:

It doesn't really matter; both work. Overriding OnPaint might be ever so slightly faster in theory, but it's not a difference that anyone will notice. Microsoft recommends overriding OnPaint but doesn't really motivate this.

You need to call base.OnPaint because this method will invoke handlers attached to the Paint event.

Recommend

  • Set SqlDataSource parameter to null
  • Creating a “repeater fields” without a Plugin
  • Chart.js t.ticks.map is not a function
  • Popularity of MVP for SharePoint WebParts
  • Deserializing Json array into Scala object
  • How to prevent d3.drag().on('end' from firing .on('click'
  • line break and subscript in axis title using plotly in R
  • How to register eureka-clients with eureka-server on different hosts. Spring-boot
  • Setting up Akka with Eclipse
  • How to know if SQL Server is in the cloud or on premises?
  • How to import both xls and xlsx files using java in spring mvc
  • How to create pagination with PDO PHP [closed]
  • Send objects from flex to java servlet
  • Pushing notifications using ApnsPHP is slow
  • cmd error: 'ANT' is not recognized as an internal or external command
  • ZPK Encryption ISO format 9594-1 Format 0
  • PHP file_get_contents
  • having a condition to fragment adding
  • JPA+SQLite problem
  • How to access images in UIATablecell
  • Swing vertical Navigation Menu
  • WEBrick fails to start default rails app: “uninitialized constant ActiveRecord::ConnectionAdapters::
  • How to use javascript to get information from the content of another page (same domain)?
  • Check every condition in Python if else even if one evaluates to true
  • How can I access the Google account user_id?
  • How to add ng-app after page load?
  • How to create mirrored image effect with CSS single element
  • Hibernate Caused by: org.hibernate.AnnotationException: Unknown mappedBy
  • How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to s
  • Authorize Attribute Authentication with Postman in Web Api
  • Multiple canvases (pages) in Fabric.js
  • Unable to start a WebView from an AsyncTask
  • content must have a ListView whose id attribute is 'android.R.id.list'
  • C# code can't “see” the methods in my C++ dll
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • All Event listing on specified date in Google Calender api (V3) in java?
  • Find angle of point on circle
  • Multiplying polynomials/simplifying like terms
  • How to run chrome.tabs.insertCSS from the background page on each page?
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma