79410

Draw the shape on the Button click

Question:

I create a Windows Forms application and I want to draw a shape on the Button click. How can I call Form_Paint on the Button_Click event?

Answer1:

Here's a quick example that stores each "shape" as a GraphicsPath in a class level List. Each path is drawn using the supplied Graphics in the Paint() event of the form. A random rectangle is added to the List<> with each button click and Refresh() is called against the form to force it to redraw itself:

public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Paint += new PaintEventHandler(Form1_Paint); } private Random R = new Random(); private List<System.Drawing.Drawing2D.GraphicsPath> Paths = new List<System.Drawing.Drawing2D.GraphicsPath>(); private void button1_Click(object sender, EventArgs e) { Point pt1 = new Point(R.Next(this.Width), R.Next(this.Height)); Point pt2 = new Point(R.Next(this.Width), R.Next(this.Height)); System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath(); shape.AddRectangle(new Rectangle(new Point(Math.Min(pt1.X,pt2.X), Math.Min(pt1.Y, pt2.Y)), new Size(Math.Abs(pt2.X - pt1.X), Math.Abs(pt2.Y - pt1.Y)))); Paths.Add(shape); this.Refresh(); } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics G = e.Graphics; foreach (System.Drawing.Drawing2D.GraphicsPath Path in Paths) { G.DrawPath(Pens.Black, Path); } } }

Answer2:

To raise the Paint even by hand read this SO post (basically call the Invalidate() method)

<a href="https://stackoverflow.com/questions/952906/how-do-i-call-paint-event" rel="nofollow">SO post: How do I call paint event?</a>

However, you'll probably need to have some sort of internal "drawshape" flag that you set/clear on the button click and check inside your paint even handler method. This flag will tel your paint event handler to either continue drawing your shape or not draw your shape at all (every time the form paint is called)

Recommend

  • Highcharts: To rotate the chart 90 degree but also redraw it
  • How do I modify ajax url of a query datatable?
  • Need help in optimizing a drawing code …
  • Fabric.js ruler with zoom
  • Getting coordinates of a component in java
  • Circle movement upon rectangle Collision
  • R h2o.glm - issue with max_active_predictors
  • React Router and Arbitrary Query Params: Page Refreshes Unintentionally on Load?
  • WP7 difficulties binding data to listbox itemssource - won't refresh
  • c# winform DrawToBitmap offscreen
  • IE6 changes DOCTYPE to a bad one
  • Why people use prototype in javascript when it is easy to inherit using apply () and call () methods
  • Problem with Django using Apache2 (mod_wsgi), Occassionally is “unable to import from module” for no
  • Doctrine/Symfony entity generator and generating entity from one table
  • Default parameter as generic type
  • Low TTL with Leveled Compaction, should I reduce gc_grace_seconds to improve read performance withou
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • Android fill_parent issue
  • Scrapy recursive link crawler
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • How to handle AllServersUnavailable Exception
  • jquery mobile loadPage not working
  • jQuery tmpl and DataLink beta
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Why winpcap requires both .lib and .dll to run?
  • SQL merge duplicate rows and join values that are different
  • Proper way to use connect-multiparty with express.js?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How to set the response of a form post action to a iframe source?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal