What if I only want one class to access my form's controls?
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!
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
{
}
accessing the controls for tracking state is aint the best way, but if thats wat you want then i would suggest you access the controls which are private through reflection http://social.msdn.microsoft.com/forums/en-US/clr/thread/ef7cc349-cf35-4b01-95b3-b71675f9b9d5/
精彩评论