this question is a further development of a previous question.
I’m working with C# .NET framework 2.0, Visual Studios 10.
I have a text editor upon which I would like to have a dirty marker in the form title, in this case a simple ““. If the textbox has been changed, then the “” should appear in the title. When the textbox has been saved, then the “*” should disappear. I’ve tried the following things, but perhaps incorrectly:
1 form, editor, has the save button and textbox
—- Editor.cs
—- Functions.cs
a Different FILE, NOT A FORM, Functions.cs, gets called upon save which performs the save
(to keep things neat, only buttons etc on form code, and a different file does the dirty work).
–Changing Editor._isDirty value from within the second functional file
–Changing _isDirty value from within the editor file itself
–Changing IsDirty from within the editor (i can’t figure out how to do that from the functional file)
and here is the relevant code:
public static bool _isDirty = false;
public plainTextEditor()
{
InitializeComponent();
functionsProxy = new Functions();
IsDirty = false;
}
/* Property added to flag Changed _isDirty event */
public bool IsDirty
{
get { return _isDirty; }
set
{
if (_isDirty != value)
{
_isDirty = value;
OnIsDirtyChanged(IsDirty);
}
}
}
protected void OnIsDirtyChanged(bool _isDirty)
{
if (_isDirty == true)
{
//textBox1.BackColor = Color.LightCoral;
this.Text += "*";
}
else
{
this.Text = "Text Editor";
//textBox1.BackColor = SystemColors.Window;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string newtext = textBox1.Text;
if (currentText != newtext)
{
// This solves the problem of initial text being tagged as changed text
if (currentText == "")
{
//textBox1.BackColor = SystemColors.Window;
IsDirty = false;
}
else
{
IsDirty = true;
}
currentText = newtext;
}
}
private void btnSave_Click(object sender, EventArgs e)
{
functionsProxy.doSave(textBox1.Text);
//_isDirty = false;
IsDirty = false;
}
Now, from what I understood about the events and properties.. if I even change the value of _isDirty, the OnIsDirty should be called and changed right?? No matter where I change the value of _isDirty, say it be from a different form. That’s what I want anyways.. that the event of * either appears or disappears depending on whether the _isDirty changes! Somehow it’s only working to mark the dirty and not to clear it.
Please help if you can, or suggest another method (sample code would be ace!) 😉
There are several issues in your code. One of them is the mix-up of a static member and a parameter in the method. I tried to correct the code. Here is my suggestion for you:
Keep “_isDirty” private to your form. You already expose the Dirty state with your property IsDirty. If you don’t want any code from outside your form to change IsDirty, then make the setter (“set”) private.
Now your code should run.