Discard changes confirm on program exit if richtextbox content changed - c#

I would like to ask the user when he/she clicks the closebutton: save the file, discard the changes, or go back IF the RichTextBox content changed. Like Windows Notepad or any other text editor does. How could I do that?

You need to read about events and how they work. In this case, you are interested in the TextChanged event of the RichTextBox and the FormClosing event of the form.
TextChanged Event : MSDN
The TextChanged event is raised whenever the contents of the textbox are modified. One way to track changes in the textbox is simply to use a boolean value. Be sure to set it to false when you are loading data into the textbox. Then, when the user changes the text the TextChanged event will fire and you can set the _textChanged (in the example below) value to true.
Similarly, making use of the FormClosing event allows you to react to a user attempting to close the form.
FormClosing Event : MSDN
This event passes a FormClosingEventArgs value e that allows you to cancel the closing of the form (in this case if the user selects to cancel when prompted about the text having been changed). It also allows you to perform any other action before the form is closed.
To create the message dialog you can use an appropriate overload of MessageBox.Show - this function returns a DialogResult indicating which of the buttons the user clicked. This allows you to take different actions depending on the selection the user made.
MessageBox.Show : MSDN
If you don't know how to connect these events, then I suggest you read through some of the basic documentation and examples. This is pretty elementary stuff that you will need to understand to get much of anything done in C#. The examples here are winforms since you have not indicated otherwise.
This is a trivial example :
public partial class Form1 : Form
{
private bool _textChanged;
public Form1()
{
InitializeComponent();
// load data to richtextbox, then ....
_textChanged = false;
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
_textChanged = true;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (_textChanged)
{
DialogResult rslt = MessageBox.Show("save changes?", "some caption",
MessageBoxButtons.YesNoCancel);
if (rslt == DialogResult.Yes)
{
// save changes and exit
}
else if (rslt == DialogResult.Cancel)
{
e.Cancel = true;
// cancel close, return to form
}
// else do not save and continue closing form
}
}
}

Related

I want to validate a radiobutton before it changes state, but it really fires when the focus leaves the control

I have a simple Winform example that demonstrates what happens.
There is a GroupBox with two RadioButtons. The two buttons share a validating event handler. Also on the form is a Button that does nothing. No events are connected. Finally there is a CheckBox that controls the state of a passing validation. Checked it passes and unchecked it fails.
When the program starts and the user clicks on either RadioButton the validating event does not fire. Then when I click on any control other than the current button the Validating Event fires. The NOP button gives me something to click besides the CheckBox.
In this test the CheckBox represents the status of passing the validation.
This will not work because once I uncheck the CheckBox and then click a radio button the focus is forever stuck. You can't get the focus to the CheckBox to change its state.
The reason is the Validating event is always called when the focus is leaving and not when the RadioButton is clicked. It sees that "valdating" fails and cancels the event.
This is obviously the wrong approach. What should I be doing to test at the time of the initial click? The Click event happens after the state has changed.
What event should I use so that I can test validation before changing the RadioButton state? Then I can leave the button and fix the issue before trying again.
This example is a simplified test that shows my dead end. My real world example is the two RadioButtons select one of two similar tables in a DataGridView. The two tables are related and I want them to be on the same TabPage. When the user selects the alternate table I want to do a validation/confirmation before switching away. If the confirmation fails I want to cancel the radio button.
// Validate is called when the radio button is clicked and when it leaves the box
using System.ComponentModel;
using System.Windows.Forms;
namespace ValidateRadioButton
{
public partial class Form1 : Form
{
int count;
public Form1()
{
InitializeComponent();
}
private void radiobutton_Validating(object sender, CancelEventArgs e)
{
if (sender is RadioButton) {
// If box not checked, cancel radioButton change
// This becomes a Hotel California test, once unchecked you
// can never leave the control
e.Cancel = !chkAllowChange.Checked;
}
count++;
//Display the number of times validating is called in the title bar
//Demonstrates when the event is called
Text = count.ToString();
}
}
}
I have reproduced your form in a test project and I think I see what is going on.
Setting the Checked property of CancelEventArgs to true will prevent whatever control set it from losing focus until input is "corrected". As you are aware, the Validating event is triggered whenever a control loses focus. The user becomes "stuck" as you say because the only way they can "correct" their input is to modify the check box, which they cannot get to because of the Validating event which fires on the radio button.
A solution which I came up with was moving the Validated event to the GroupBox, instead of the radio buttons:
private void groupBox_Validating(object sender, CancelEventArgs e)
{
if (sender is RadioButton)
{
e.Cancel = !checkBox1.Checked;
}
count++;
//Display the number of times validating is called in the title bar
//Demonstrates when the event is called
Text = count.ToString();
}
Be sure to remove the Validating event handler from the radio buttons.
For clarity, I have set my form up in this manner:
The result is now I cannot click the 'OK' until I have ticked the check box. I have preserved your Text = count.ToString() assignment so that you can see that the form now calls Validating only as it should.
I ended up using the CheckChanged and Click events for the RadioButtons.
I track the currently active RadioButton and do the validation in the CheckChanged event. Then in the Click event, if the validation failed I restore the previous active RadioButton. Otherwise we can continue to the purpose of the RadioButton.
This solution works for two or more RadioButtons in a group and it works when using the keyboard.
using System.ComponentModel;
using System.Windows.Forms;
namespace ValidateRadioButton
{
public partial class Form1 : Form
{
// RadioButton that is currently active
RadioButton ActiveRadioButton;
bool cancelingChange;
public Form1()
{
InitializeComponent();
activeRadioButton = this.radioButton1;
}
private void radioButton_CheckedChanged(object sender, System.EventArgs e)
{
// Each click fires two events
// One for the RadioButton loosing its check and the other that is gaining it
// We are interested in the one gaining it
if (sender is RadioButton) {
RadioButton radioButton = (RadioButton)sender;
if (radioButton.Checked) {
// If this button is changing because of a canceled check
// do not validate
if (!cancelingChange) {
cancelingChange = !ValidateData();
if (!cancelingChange) {
// Mark this as the active value
activeRadioButton = radioButton;
}
}
}
}
}
private void radioButton_Click(object sender, System.EventArgs e)
{
// This is called after the RadioButton has changed
if (cancelingChange) {
// Check theRadioButton that was previously checked
activeRadioButton.Checked = true;
cancelingChange = false;
}
else {
// Do the thing the RadioButton should do
// If using separate events they all must start with the condition above.
this.Text = ((RadioButton)sender).Name;
}
}
/// <summary>
/// Validate state of data
/// </summary>
/// <returns></returns>
private bool ValidateData()
{
bool result = chkAllowChange.Checked;
if (!result) {
result = MessageBox.Show("Do you want to save your data?", "CheckBox unchecked", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK;
if (result) {
// This is where I would save the data
chkAllowChange.Checked = true;
}
}
return result;
}
}
}

How to detect if the user clicked the X button in the leave event of a text box

I have a Leave Event for a TextEditor in which I perform a validation that an entry is required and display an error message.
Before I perform the validation, I check if the form is disposing, or the Cancel button was clicked. In that case I exit the leave event.
But if the user clicks the X button, these two checks do not capture that and the error message is displayed. I do not want the error message to display if the user clicks the X button. How can I achieve that?
private void TitleTextEditor_Leave(object sender, EventArgs e)
{
UltraTextEditor _currentControl = sender as UltraTextEditor;
if (this.CancelUButton.Focused || this.Disposing)
{
return;
}
if (_currentControl.Text.IsNullOrStringEmpty())
{
MessageBox.Show("Title is required.");
}
}
This is a cruddy problem if you want to suppress the validation error message you display. The only decent way to get ahead of it is by detecting the WM_CLOSE message before the Winforms code sees it and generates the Validating event on the control with the focus.
Paste this code to solve your problem:
protected override void WndProc(ref Message m) {
// Prevent validation on close:
if (m.Msg == 0x10) this.AutoValidate = AutoValidate.Disable;
base.WndProc(ref m);
}
Do consider that you are yelling too loud. The ErrorProvider component is a very decent way to display validation errors and be subtle about it. And nothing drastic goes wrong when the form validates itself on closure, you only have to do this:
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
e.Cancel = false;
}
In the FormClosingEventArgs you have a CloseReason property, you can probably use it.
How to find out if a control's Form has closed or not. I have listened to the VisibleChanged event in order to divine the Form because ParentChanged can happen before the control is added to a Form (e.g. if it is in a Panel). You might also want to unsubscribe from VisibleChanged events after the first one.
//put this at class level
bool _parentClosed;
//put this in controls constructor
//when control first becomes visible
this.VisibleChanged += (s1, a1) =>
{
//find parent Form (not the same as Parent)
Form form = this.FindForm();
//If we are on a Form
if (form != null)
//subscribe to it's closing event
form.Closing += (s2, a2) => { _parentClosed = true; };
else
throw new Exception("How did we become visible without being on a Form?");
};

How do I catch the event of exiting a Winforms application?

If the user wants to exit the application by clicking on the exit icon or by ALT+F4, I'd like to make a dialog box questioning the user if he/she is really sure about exiting.
How do I capture this event before the application is actually closed?
Check out the OnClosing event for the form.
Here is an extract from that link actually checks for a change on a text field and prompts a save:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Determine if text has changed in the textbox by comparing to original text.
if (textBox1.Text != strMyOriginalText)
{
// Display a MsgBox asking the user to save changes or abort.
if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Cancel the Closing event from closing the form.
e.Cancel = true;
// Call method to save file...
}
}
}
You can change the text to suit your needs, and then also I think you might want to switch the DialogResult.Yes to DialogResult.No based on your text.
Here is some modified code just for you:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(MessageBox.Show("Are you sure you want to quit?", "My Application", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}
You should subscribe to the Form_Closing event
Post a dialog box there, and if user abort the closing set the FormCloseEventArgs.Cancel to true.
For example in the Form_Load or using the designer, subscribe the event
Form1.FormClosing += new FormClosingEventHandler(Form1_Closing);
....
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
DialogResult d = MessageBox.Show("Confirm closing", "AppTitle", MessageBoxButtons.YesNo );
if(d == DialogResult.No)
e.Cancel = true;
}
Depending on the situation is not always a good thing to annoy the user with this kind of processing.
If you have valuable modified data and don't want the risk to loose the changes, then it is always a good thing to do, but if you use this only as a confirm of the close action then it's better to do nothing.
You can handle the Form_Closing or Form_Closed events for this.
In Visual Studio click the lightning bolt icon and scroll down to these events in the form properties list. Double click on the one you want and it will hook up the event for you.
Read msdn sample: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx
Is this just one form? If so, you might want to use the FormClosing event, which allows you to cancel it (show the dialog, then set CancelEventArgs.Cancel to true if the user chooses to cancel closing).
If you're talking about windows forms, should be enough to catch your MainWindow's
FormClosing event and in case you want prevent closing, just set the argument of the event handler got fired, to true.
Example:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(MessageBox.Show("Do you really want to exit?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.No){
// SET TO TRUE, SO CLOSING OF THE MAIN FORM,
// SO THE APP ITSELF IS BLOCKED
e.Cancel = true;
}
}
}

How to detect if an event is sent by an argument

I don't know if it is called an argument (i.e. textbox1.text = "Hello";).
I have a control and there is a text box in it. It has a dropdown box that opens when the text is changed. But when I update the text in the text box that box drops down.
I need a way to make it so it only drops down if someone manually does it.
TBAddressBar.ABText.Text = getCurrentBrowser().Source.ToString();
and
public void ABText_TextChanged(object sender, TextChangedEventArgs e)
{
if (sender == 1*)
{
ABDropDown.Visibility = Visibility.Visible;
}
else
{
ABDropDown.Visibility = Visibility.Collapsed;
}
}
If someone manually does it, presumably they are using keypresses to do so. In that case, use KeyDown or KeyUp events to show the dropdown instead.
What I have done in the past is use a boolean variable that I set when I update my textboxes programically to bypass the TextChangedEvent.
i.e.
bool loading;
....
loading =true;
TBAddressBar.ABText.Text = getCurrentBrowser().Source.ToString();
loading = false;
public void ABText_TextChanged(object sender, TextChangedEventArgs e)
{
if(loading) return;
....
}
Simple, just remove the code from your TextChanged Event.
Anyway you got the basic idea.. Now do your dropdown logic in KeyPress event, since it accepts only characters and not the modifiers. So it behaves closer to your requirement. Not that you cant handle the same using KeyDown and KeyUp, you can, but more code..

Form closing save changes before close

I have a Windows forms application in C# and I have a form that when the user closes it I ask, "do you want to save the changes"? How can I get the changes in my form? Here is some code:
public partial class DepartEdit : Form
{
string _nameDep; //This variavel get value textbox when form load
{
InitializeComponent();
}
private void DepartamentEdit_FormClosing(object sender, FormClosingEventArgs e)
{
if (txtNameDepart.Text != _nameDep && codDepartament > 0)//Here i compare
{
DialogResult dlg = MessageBox.Show("Save changes?", "Question", MessageBoxButtons.YesNo);
if (dlg == DialogResult.Yes)
{
saveDepart(); // Metod save depart
e.Cancel = false;
}
if(dlg ==DialogResult.No)
{
e.Cancel = false;
}
}
}
There are a lot of textboxs and combo boxes? Is there any other way to get the changes in the form?
A lot will depending on where the information is held.
It you are using DataBinding you should be just monitoring the listChanged event or calling dataTable.GetChanges() if you are using a DataTable.
If the information comes from a class the implements ICloneable and IComparable, then you can take just take a backup copy when intialising the form (and after saving) and when closing you prepare you class for saving and compare it with the original.
Otherwise you can do something like
Declare a private variable
private bool requiresSaving =false;
Declare an event
private void SomethingChanged(object sender, EventArgs e)
{
requiresSaving = true;
}
Hook up this event to the various changed events, eg
this.txtNameDepart.TextChanged += new System.EventHandler(this.SomethingChanged);
(The actual event is sometimes called something different , eg ValueChanged, SelectedIndexChanged , but they can all point to SomethingChanged unless you need a particular event to do something else.)
Check this variable when you are closing the form
private void DepartamentEdit_FormClosing(object sender, FormClosingEventArgs e)
{
if (requiresSaving)
{
....
You also need to set requiresSaving false in the saveDepart method.
Alternatively I have seem code where, when the control is being intialised, the tag value is also set, and the formclosing event loops through each control and compares the original values (in the tag object) with the current values.
Create a string (or string[] I guess) within the Form_Load event and initialise them with the values present when the form first opens.
eg
string oName = nameTextBox.Text;
string oCompany = companyComboBox.Text;
Then during the Form_Closing() event you can check these against the current values
private void Contact_FormClosing(object sender, FormClosingEventArgs e)
{
if (oName!=nameTextBox.Text||oCompany!=companyComboBox.Text)
{
DialogResult result = MessageBox.Show("Would you like to save your changes",
"Save?",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Stop);
if (result == DialogResult.Yes)
{
SaveFormValues();
}
else if (result == DialogResult.Cancel)
{
// Stop the closing and return to the form
e.Cancel = true;
}
else
{
this.Close();
}
}
}
Loop the forms controls, and add your event watchers. These events will call a function in the form that will keep a Hashtable or some other various collection up to date w/ the status of any changes to a particular control.
like I have, Hashtable changes;
then each time my event is called, i say, Add Control.Name, and then a change status, whatever you want. Then you have a list of controls that have been updated.
I can go into more detail if need be, hopefully this will get you started.
Well I think the theory is good. There are some problems with the implementation.
if (dlg == DialogResult.Yes)
{
saveDepart(); // Metod save depart
e.Cancel = false;
}
if(dlg ==DialogResult.No)
{
e.Cancel = false;
}
I think it would be a lot similier to write this:
if(dlg == DialogResult.Yes)
{
saveDepart();
}
// You don't need to change e.Cancel to false here unless you set it to true previously.
It's simple
Declare a global variable count=0
Now on your form u might already have a Save button so just increment count on save button click event
Now on form close event just check for the value of count if it's 0 ask user to save info either by reminding by message box
If it's 1 or greater than1 just close the form
I hope u get it

Categories