Changes to Windows Forms C # Application

I have a form with several components, such as TextBox and ComboBox , and I need to know when you click the exit button if there are any changes in the form. Is there any way to do this?

+4
source share
4 answers

You can create a general change event handler that sets a flag when a change occurs, and then assign all the Change Events changes to it.

This can be done quite easily by going through all your onload controls.

+3
source

You can scroll through all the controls, but this should be recursive because the control may contain controls, for example. (for brevity, there are no null checks):

 private void IterateOverControls( Control parent ) { ProcessControl( parent ); foreach( Control control in parent.Controls ) IterateOverControls( control ); } 

In ProcessControl you can connect event handlers to handle OnEnter (to save state) and OnLeave (to check the current state for a saved state). When disposing, you need to unhook all event handlers. In addition, the code for saving the verification status must change for different types of control, for example. TextBox will be a Text property, but the switch will be an index, etc. Obviously, this will be simplified if you can compare the state of the form with the base state of the data store, in which case you can simply do a comparison for each OnLeave event.

You might also wonder if you need to track real changes. For example, I have 2 switches: A and B. I check B (change), so the exit button or any other Enabled property changes. Then I press A (returning to the initial state). Do you need to return the button at this moment?

That is why you should look at the model controller approach :)

+1
source

The easiest way to do this is to simply use a variable in the form called "IsChanged". Set it to false when the form is initially displayed, and set to true if they make any changes.

Alternatively, you can record the values ​​of everything when the form is displayed, and when they end, check the current values ​​for old ones to see if something has changed.

0
source

If it’s almost finished and you need something fast, it will probably be easier to just assume that something has changed, and then in your update logic after (whatever it does) the material that still remains is not updated same thing.

As already mentioned, it is very possible for someone to change something, and then change it. What would you like to do in this case? You will not be able to maintain the proper dirty state of the form without extra work. This is what you need to plan before you begin, really.

0
source

All Articles