adspace
Answer Posted / Tarun Kumar Chaturvedi
{"In order to implement Redo and Undo functionality for a TextBox control, you can use the undo stack pattern. This involves maintaining an array or linked list of previous states (strings) that the TextBox has been in. You can then push new states onto this stack when changes are made, and pop off old states to undo those changes. To redo, simply apply the popped-off state again. Here's a simplified example using an ArrayList:
```csharp
ArrayList undoStack = new ArrayList(); // Initialize an empty undo stack
// To push a new state onto the undo stack:
undoStack.Add(textBox1.Text);
textBox1.Text = newText; // Set the TextBox's text to the new value
// To undo the last change:
if (undoStack.Count > 0)
{
textBox1.Text = (string)undoStack[undoStack.Count - 1]; // Get and set the previous state
undoStack.RemoveAt(undoStack.Count - 1); // Remove the last state from the stack
}
```
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers