I'm trying to create an undo function for changes made in a PropertyGrid. To accomplish this I push an object to a Stack before assigning the object to the PropertyGrid. To my surprise the object in the stack changes accordingly to the changes made in the PropertyGrid.
Why is this happening?
public class Employee
{
[Browsable(false)]
public int employeeKey { get; set; }
[DisplayName("Name")]
[Description("The name of the employee.")]
public string employeeName { get; set; }
[DisplayName("Active")]
[Description("Indicates whether the employee is currently employed.")]
public bool employeeIsActive { get; set; }
}
public partial class frmProperties : Form
{
public object propertyGridObject;
private Stack<object> _stack;
public frmProperties()
{
InitializeComponent();
}
private void frmProperties_Load(object sender, EventArgs e)
{
_stack = new Stack<object>();
_stack.Push(propertyGridObject);
propertyGrid.SelectedObject = propertyGridObject;
}
private void btnReset_Click(object sender, EventArgs e)
{
if (_stack.Count > 0)
{
propertyGrid.SelectedObject = _stack.Pop();
}
}
}
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnViewProperties_Click(object sender, EventArgs e)
{
Employee employee = new Employee()
{
employeeKey = 123,
employeeName = "John Smith",
employeeIsActive = true
};
using (frmProperties frm = new frmProperties())
{
frm.propertyGridObject = employee;
frm.ShowDialog(this);
}
}
}