473,325 Members | 2,805 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,325 software developers and data experts.

undo what we have drawn on a form

Hi,
this it a sample code. let say I draw squares on a form
Expand|Select|Wrap|Line Numbers
  1. SolidBrush myBrush = new SolidBrush(Color.Red);
  2.             Graphics formGraphics = formnew.CreateGraphics();
  3.             formGraphics.FillRectangle(myBrush, new Rectangle(x1, y1, x2, y2));
  4.             myBrush.Dispose();
  5.             formGraphics.Dispose();
now question is on a given instance of a form i can draw any no of squares.But how can i erace a just drawn rectangle(same as undo). I know i should use a memory stack and a array list but don't know how to implement it.

Stack undoStack,redoStack;
Expand|Select|Wrap|Line Numbers
  1.  //Drawobject which contains shape properties like color etc. 
  2.         ArrayList shapesArray; 
  3.  
  4.  
  5.           public void undoobject() 
  6.            { 
  7.          //undoStack contains the list of drawn objects 
  8.         //by using shapesArray i ve drawn the objects on the drawArea. 
  9.         // drawArea is the area which is used to draw objects. 
  10.  
  11.           if (undoStack.Count>0) 
  12.             { 
  13.           foreach(DrawObject shape in undoStack) 
  14.              { 
  15.           //object will be added to the redoStack. 
  16.            redoStack.Push(undoStack.Peek()); 
  17.  
  18.           // objects will be removed from the drawArea 
  19.  
  20.            shapesArray.Remove(shape); 
  21.  
  22.          // drawArea will be refreshed. 
  23.             drawArea.Invalidate(); 
  24.  
  25.            undoStack.Pop(); 
  26.            return; 
  27.  
  28.              } 
  29.              } // 
  30.              } 
  31.  
  32.           public void RedoObject() 
  33.                { 
  34.  
  35.  
  36.  
  37.              if (redoStack.Count>0) 
  38.              { 
  39.              foreach(DrawObject shape in redoStack) 
  40.              { shapesArray.Add(shape); 
  41.                undoStack.Push(shape); 
  42.                redoStack.Pop(); 
  43.               drawArea.Refresh(); 
  44.                 return; 
  45.                  } 
  46.                 } 
  47.  
  48.                 }*/ 
something like this. plese give a sample code.
Apr 2 '09 #1
2 2334
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Apr 2 '09 #2
cloud255
427 Expert 256MB
Hi

Your code looks ok, you are definitely on the right path, a quick suggestion would be to use only one stack. You simply need to add an additional property to your shape class. Use this property as a Boolean value to determine if the shape is visible or not.

This approach allows you to only add objects to your stack and when you undo, just 'hide' rather than remove from the stack. Similar logic applies for redo.

The advantage of this approach is memory usage, you can set a maximum size for the stack and once that size is reached you can set the child pointer of the second oldest entry in the stack to NULL and then delete the oldest entry (by delete I mean dispose() the object, you could also wait for the garbage collector to do it.)

As for the actual implementation of the stack, there are many examples out there (I'm not gonna post the code for the stack object here). I suggest that you seriously consider making your own stack class as this is a really good thing to practice OOP and memory management with.

Stacks require the use of pointers, I don't know if you have used pointers before, so you may have to read up about them.

Don't hesitate to post any code with which you are having a problem following, I will be more than willing to help you through the tricky bits of code.

Hope this helps
Apr 3 '09 #3

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

Similar topics

3
by: Patrick S | last post by:
I am using IE 6.0. On a page, I have placed a contenteditable div which I want to use as a Rich Text Area. I am using the execCommand method of the document to control bold, italics,...
2
by: Roger | last post by:
form A has a command button that opens form B in dialog mode DoCmd.openForm "frmProductPrice", acNormal, , , acFormEdit, acDialog, productId form B is a tabular form with 3 'required' fields ...
2
by: Lyn | last post by:
Hi, I have a text control on a form which is bound to table field StartDate which is in Date format. When updating the table record via the form, any data entered into the StartDate control is...
18
by: Darryl Kerkeslager | last post by:
I don't do much with subforms - in fact I've deliberately avoided them - but .... I have a tab control that will contain several subforms, each bound to a separate table, and each table/subform...
2
by: Leslie | last post by:
On a form, I create a record and save, and am ready to enter another. I decide to Quit the form which executes an "Undo" in case there is data entered. This then tries to "delete" my just saved...
6
by: Raji16 | last post by:
Hi, I am a new member. i am designing a simple judicial database system. however after creating tables i am a bit confused on setting the relationships between tables :confused: here is the link...
4
by: lesperancer | last post by:
I've got a comboBox with 2 choices and based on the choice, I make button A or button B visible if choose choice 1, button A is visible now, I undo the choice so that the comboBox is null, but...
5
by: jmarcrum | last post by:
Hey everybody! I've created 2 buttons that I've placed on all my forms: a Save button & a Cancel button. The cancel button I have a question about. I used the wizard to create the button...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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)...
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.