Home Ask Login Register

Developers Planet

Your answer is one click away!

youchana February 2016

How to save automatically for the user changes

How to save automatically for a validation part in windows form,

For example in windows form page I have a tab control with name called validation if user make changes

i.e., checking the check box and changing the color box all the changes should save automatically without clicking on save button?


Mamta D February 2016

You could add event listeners/handlers to those controls and upon any change, invoke your Save method wherein you save the contents of the controls.


private void CheckBox1_CheckedChanged(Object sender, EventArgs e) {
    CheckBox cb1 = (CheckBox)Gridview.Rows[index].FindControl("CheckBox1");
    string checkboxstatus;
    if (cb1.Checked == true)
        checkboxstatus = "YES";
    else if(cb1.Checked == false)
        checkboxstatus = "NO";
   SaveData(checkboxstatus); // or something similar where you send the  data to a method that will actually perform the save operation


Mihail Stancescu February 2016

First, why do you even need a Save button if you save automatically? Second, you may want to look into "Dirty" checking for the form you are in to see if there were changes in the fields.

You can create your own library to check/implement this but there are also few examples out there: First example and Second example.

Mohith P February 2016

Auto save is much complex operation. Because it interrupts user activity while auto saving. one simple method for implementing auto save

  1. Create one Timer and Boolean flag variables .

  2. Start the timer when application get started.

  3. Set the flag if any changes done by user activity.

  4. Call auto save function frequently according to timer interval. if the flag is set then the complete application changes get saved and reset flag.

Gabe February 2016

You can do the same as Mamta D has suggested except when your form is generated create a handler for each type of control and subscribe all relevant controls to the handler. After which you can handle the saving process.

It's hard to really suggest a foolproof method of doing this without seeing how your code is being generated, however I will give it a shot for you.

Forewarning - I'm at work and this code likely will have 'issues' as I cannot compile and test. Firing from the hip as it were. Also I haven't handled assigning your controls to a container or anything like that. You'll have to figure that out yourself.

There are a few ways you can create on the dynamic event handlers you can either do your 'logic' on creation, using anonymous delegates and handle events like below:

var cb = new CheckBox();    

cb.CheckedChanged += (sender, e) => 
        // handle your saving logic here

Or you can do it after it's been generated and implicitly create a handler to process all checkboxes in the form within the same handler like below:

var cb = new CheckBox();
cb.CheckedChanged += new EventHandler(cb_CheckedChanged);

private void cb_CheckedChanged(object sender, EventArgs e)
  //handle your checking and saving logic here.

Now if you're concerned about where the data is coming from and it needs to be saved to a specific location, for example - each checkbox is tied to a 'product' and the checkbox designates where or not the product is still in stock, then you will need to dynamically apply a name with cb.Name = "nameHere" and make it meaningful so you can check it in the event handler.

Using the second example:

   private List<Product> _products = new List<Product>();
   _products.Add(new Product("Product_1"));
   _products.Add(new Product("Product_2"));

   //Before anybody gets antsy in their pantsy, I know the abo 

Post Status

Asked in February 2016
Viewed 3,210 times
Voted 7
Answered 4 times


Leave an answer

Quote of the day: live life