473,324 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Drawing new rectangle while preserving the existing rectangles

Hi!

I use this code to draw rectangles while mouse is clicked in Panel.

The rectangle is drawn in the position of the click.

For now it works good but each time i click in new location in the Panel, the previous rectangle is deleted.

This is my code to draw:
Expand|Select|Wrap|Line Numbers
  1.        private void click_AddLocation(object sender, EventArgs e)
  2.         {
  3.     .
  4.     .
  5.     .
  6.     .
  7.  
  8.             this.Panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.PaintOnMap);
  9.             Panel1.Refresh();
  10.         }
  11.  
  12.         private void PaintOnMap(object sender, PaintEventArgs e)
  13.         {
  14.             RedPen = new Pen(Color.Red, 7);
  15.             Graphics g = e.Graphics;
  16.             PosXDraw = PosX;
  17.             PosYDraw = PosY;
  18.             Rect = new Rectangle(PosXDraw * 3, PosYDraw * 3, 1, 1);
  19.             g.DrawRectangle(RedPen,Rect);
  20.         }
  21.  
Thanks in advance!
Mar 6 '12 #1
2 2025
GaryTexmo
1,501 Expert 1GB
You didn't post the code for it but I'm assuming that during the "..." portion of your click you're updating PosX and PosY? If that's the case, you're adding a paint event handler that's going to draw the same rectangle every single time.

Instead, try creating objects when you click and storing them in a list, then drawing them all. You could easily add a private member to your form that was something like..

Expand|Select|Wrap|Line Numbers
  1. private List<Point> m_points = new List<Point>();
Then when you click...
Expand|Select|Wrap|Line Numbers
  1. int x = /* however you're currently getting PosX */;
  2. int y = /* however you're currently getting PosY */;
  3. m_points.Add(new Point(x, y));
Finally, only use a single paint event and just draw all the points how you want...

Expand|Select|Wrap|Line Numbers
  1. /* in some paint event */
  2. Graphics g = e.Graphics;
  3. foreach (Point p in m_points)
  4. {
  5.   g.DrawRectangle(Pens.Red, p.X, p.Y, 1, 1);
  6. }
You'll likely still want to trigger a refresh in your click method so your form updates.
Mar 6 '12 #2
Thanks! works like a charm
Mar 7 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: sachin | last post by:
Please read the code snippets belo snippet 1: Drawing rectangle using Device Context...
0
by: Pramod | last post by:
Hi All, I am working on C# windows Application. I want to draw a rectangle on a richtextbox and write some text inside the rectangle, like this many rectangle should be drawn at the user choice....
4
by: Colin McGuire | last post by:
Hi, this is a really simple question I have been banging my head on a brick wall over. The program below changes the background colour of a form depending on whether the cursor is inside a...
1
by: YYZ | last post by:
Sorry for the multipost, but no one was responding in the other thread. If any solution is forthcoming, I will return to the original thread and post it there as well. I've created a usercontrol...
4
by: Cristian | last post by:
Hello. How do I can to drawing rectangle and line on my form?? PHM (Please Help Me) Cris
7
by: Stefan0 | last post by:
Hi, I'd like to draw a circle on the screen using GDI+ I'm using the DrawEllipse method with width = height and I set the SmootingMode to SmoothingModeAntiAlias but the circle is "squared". Is...
3
by: Ivonne Riedel | last post by:
Working on an incremental drawing algorithm I am facing a problem PInvoking ScrollWindowEx: The code is as follows... C#: public static extern int ScrollWindowEx(IntPtr hWnd, int dx, int...
0
by: bienvankhat | last post by:
Hi, i want to simulate a led matrix panel. I create a 8x8 dot led matrix by drawing 64 rectangles in a small area. The panel is made up by 100 matrices. But the drawing process is very slow, it take...
11
by: dongarbage | last post by:
Hi there, I'm very much a C# novice. How do you do freehand drawing on a panel with a mouse in c#? Thanks, Don
2
by: luckyguy4ru | last post by:
Hello every body, I just joined today this forum hopping that i will get really good help here, My final project is to refashioned an artist work which is mostly pencil work, i have fake color...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.