Hello, first you need to know that this is my first Win Application made in C#!!

I`m trying to draw some circles based on mouse left click coordinates but the circles disappear when I minimize the window, Solutions found on the internet didn`t helped me too much :(. Here is my code:

u

sing System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


namespace GraphCApplication1
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		int i=0, x, y;
		String s = "";
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint (e);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		/// 
		public void mouseDownForm(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			Font f;
			SolidBrush br;
			Pen pn;
			Rectangle rct;
			
			switch(e.Button)
			{
				case MouseButtons.Left:
					
					x = e.X;
					y = e.Y;
					i++;
					s = i.ToString();
					//Graphics g = Graphics.FromHwnd(this.Handle);
					Graphics g = CreateGraphics();
					//Draw(g);
					//g.FillRectangle(new SolidBrush(this.BackColor), this.ClientRectangle);
					f = new Font("Arial", 14);
					br = new SolidBrush(Color.Red);
					pn = new Pen(Color.GreenYellow);
					rct = new Rectangle(e.X, e.Y, 50, 50);
					g.DrawEllipse(pn, rct);
					g.DrawString(s, f, br, rct);
					
					//MessageBox.Show(this, "click stanga");
					break;
				case MouseButtons.Right:
					/*if (RightClick != null)
					{
						RightClick(sender, e);
					}*/
					MessageBox.Show(this, "click dreapta");
					break;
			}
		}
		/*public void Draw(Graphics g)
		{
			Rectangle r = new Rectangle(x, y, 20, 20);
			g.DrawEllipse(new Pen(Color.Coral), r);
			g.DrawString(s, new Font("Arial", 10), new SolidBrush(Color.Coral), r);
		}*/
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.Size = new System.Drawing.Size(300,300);
			this.Text = "Form1";
			this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.mouseDownForm);
		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
	}
}

What I would do is create a data structure to keep track of the places you have clicked and any corresponding info you need, then when the form is minimized/maximized/given focus/whatever else, redraw all the circles.

What i think is happening, is the form 'forgets' whats been drawn when you minimize it, not sure about this as I'm no expert, but storing what you need and re-drawing when required should fix it.

Actually, I think you need to put the code

this.Invalidate()

in the minimized event handler or similar.

Actually, I think you need to put the code

this.Invalidate()

in the minimized event handler or similar.

Looks like invalidate will solve this problem. There's always an easier way to do it :)
My solution would work, its just needlessly complicated.

This question has already been answered. Start a new discussion instead.