Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6351331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:59:04+00:00 2026-05-24T21:59:04+00:00

I implemented an Undo/Redo system in my application but because it is a separate

  • 0

I implemented an Undo/Redo system in my application but because it is a separate class from my main form it cannot access any of its controls. Currently I’m passing every control that needs undoing or redoing in to the class’s constructor but it is quickly becoming one of the largest constructors in my project.

It’d be nice if only this class were allowed to access to my form’s controls. If I pass in the form to the constructor I still cannot access the control’s because they are all private. I suppose the simple solution would be to make all the controls I need to use public but what is the ideal or elegant solution?

P.S. I have already read about the technique of having a public property for everything required and passing in the form to the constructor but that will end up being quite a bit of properties and the solution does not seem very applicable in this situation.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:59:05+00:00Added an answer on May 24, 2026 at 9:59 pm

    Very, very, very rarely is it good practice to pass UI controls or forms to a class. This is just a maintenance/dependency nightmare in the making.

    You don’t want your class to know diddly about your form’s controls, all it should care about is its data.

    Your form knows itself, knows the data it needs to save and how to restore it to the proper controls.

    If all you are looking for is undo/redo capability, you want to look into the Memento design pattern. You don’t need to make a fully propertied class unless you have some other business need for it.

    For the memento pattern.

    A simple object holds the state you want to preserve….

     internal class Memento: IMemento
        {
            private object _state;
    
            public void SetState(object state)
            {
                _state = state;
            }
    
            public object GetState()
            {
                return _state;
            }
        }
    

    A caretaker object manages the mementos for the form.

     public class Caretaker
        {
            private Dictionary<int,IMemento> _mementos = new Dictionary<int,IMemento>();
            public void AddMemento(int tag, IMemento memento)
            {
                _mementos.Add(tag, memento);
            }
    
            public IMemento GetMemento(int tag)
            {
    
                if (_mementos.ContainsKey(tag))
                {
                    return _mementos[tag];
                }
    
                return null;
            }
        }
    

    The form saves a new memento to the Caretaker after changing state. We take advantage of the extreme flexibility of the object class…

    private void SaveMemento()
    {
                Memento memento = new Memento();
                string[] state = {TextBox1.Text, TextBox2.Text};//expand as needed
                memento.SetState(state);
                _caretaker.AddMemento(_currentStateIndex, memento);
                _currentStateIndex++;
    }
    

    The form reverts as needed.

    private void RevertToPreviousState()
            {
                Memento memento = (Memento)_caretaker.GetMemento(--_currentStateIndex);
                string[] state = (string[]) memento.GetState();
                TextBox1.Text = state[0];
                TextBox2.Text = state[1];//expand as needed
    
            }
    

    Forgot IMemento –just a tagging interface, in case you want to put the caretaker in another assembly for full Memento pattern implementation.

    public interface IMemento
        {
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i try to implement a redo undo in my windows form application. i build
I'm trying to implement an undo/redo feature into my application, using the Command Pattern
Implemented the Sink Class - to receive event notifications from COM Server Event Interface
I am trying to get Undo/Redo keyboard shortcuts working in my WPF application (I
I want to implement Undo and Redo functionality not only for client side but
I'm trying to implement an Undo/Redo stack in a C# application that I'm working
I have a Java application and I want to implement an Undo/Redo option. the
I am making a paint application using OpenGl framework and I'm stuck at UNDO/REDO
I need to implement undo-redo functionality in my project. But it is very complex
I need to implement Undo/Redo frame work for my window application(editor like powerpoint), what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.