net windows form application. I have a combo box and a text box and a close window button. Now If I make any change in the combo box or textbox and click on the close window button, it should prompt the user to save the modifications.. If no modification are made ( The user will just run the application, doesnt make any modification) then it should not prompt the user. It should close directly.. How can I do this?
Share
One way is to keep a bool flag called _changed or something like that as a member variable on your form.
Then in the TextChanged event of the TextBox, and the SelectedIndexChanged event of the ComboBox you just set _changed = true.
Then, just before your form closes you prompt the user if _changed is true.
Edit:
If you have many TexBox controls on the form, you could hook them all up to the same TextChanged event handler. Then, no matter which TextBox’s text changed, _changed will be set to true.
Then do the same with multiple ComboBox controls and one SelectedIndexChanged event.
If you really have many controls, rather than hooking each up manually, you could even write a method that recursively loops through the Controls collection of your form and hooks each type of control up to the appropriate event handler. Then you could reuse that method in more than 1 form to save you lots of time and maintenance, as when ever you add new controls, they will automatically be taken care of.