Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I add an Undo procedure? 1

Status
Not open for further replies.

doctorjellybean

Programmer
May 5, 2003
145
0
0
I have 2 listboxes: one which contains filenames and the other folder locations. A button is used to add a filename via opendialog to the listbox, while clicking on a listbox containing folders adds the folder.

IMG


The selections work in pairs, e.g. Folder A goes with File A, Folder B with File B, etc.

Now I want to add an Undo, where if the user makes a mistake, the last addition is removed. He can't e.g. remove Folder A in the above example, as it is paired with File A and other pairs have already been added. So all I need to do is create an Undo for the last addition (in this case Folder C), at which point the Undo will be disabled until a new addition has been selected.

I thought about selecting the last item in the listbox directly, but I don't know how to only have the last item clickable and not the others.

Thanks in advance.
 
Think the function out. It can work even on something as simple as a listbox. What you do is record what the user previously done in the program in specific terms, then if they hit undo, you essentially do the opposite of what the user previously did.

1. If user added "Smith" to the listbox and it landed on item #32, then for the undo function you delete item #32.
2. if user changed "Smith" to "Jones" on item #32, then for the undo you change "Jones" to "Smith".
3. If user deleted "Smith" on item #32, then you restore "Smith" on item #32.

Of course, if there happens to be things attached to the main meanings of your logic, you have to save those as well.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
It took awhile to think it out, but I managed it in the end. I basically assigned the last action to a string and then the Undo button deleted the matching string

Code:
ListBox_Files.items.Delete(ListBox_Files.Items.IndexOf(lastoperation));

where lastoperation is OpenDialog.Filename which executes when the Add File button is clicked.

Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top