BindingSource.EndEdit doesn't work with FormClosing

E

Evan M.

I have an appication, where a NumericUpDown control has a databinding
to a BindingSource, which has it's data source set to something else.
I also have my form's Formclosing event wired up to detect if any
changes have been made, and if so to prompt the user asking them if
they want to save. What I am finding that when I call the
BindingSource.EndEdit() method within my FormClosing event handler,
the changes aren't being propigated to my data source (and hence the
changes aren't being detected). Here is a sample application that
shows this behavior:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EndEditExample {

static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main () {
Application.EnableVisualStyles ();
Application.SetCompatibleTextRenderingDefault (false);
Application.Run (new Form1 ());
}
}

class ExampleClass {
private int exampleVal = 0;

public int ExampleVal {
get { return exampleVal; }
set { exampleVal = value; }
}
}

public partial class Form1 : Form {
private ExampleClass exValue;

#region Form1.Designer.cs code
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose (bool disposing) {
if (disposing && (components != null)) {
components.Dispose ();
}
base.Dispose (disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent () {
this.components = new System.ComponentModel.Container ();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown ();
this.bindingSource1 = new System.Windows.Forms.BindingSource
(this.components);
((System.ComponentModel.ISupportInitialize)
(this.numericUpDown1)).BeginInit ();
((System.ComponentModel.ISupportInitialize)
(this.bindingSource1)).BeginInit ();
this.SuspendLayout ();
//
// numericUpDown1
//
this.numericUpDown1.DataBindings.Add (new
System.Windows.Forms.Binding ("Value", this.bindingSource1,
"ExampleVal", true));
this.numericUpDown1.Location = new System.Drawing.Point (13, 13);
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size (120, 20);
this.numericUpDown1.TabIndex = 0;
//
// bindingSource1
//
this.bindingSource1.DataSource = typeof
(EndEditExample.ExampleClass);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF (6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size (292, 266);
this.Controls.Add (this.numericUpDown1);
this.Name = "Form1";
this.Text = "Form1";
this.FormClosing += new
System.Windows.Forms.FormClosingEventHandler (this.Form1_FormClosing);
this.Load += new System.EventHandler (this.Form1_Load);
((System.ComponentModel.ISupportInitialize)
(this.numericUpDown1)).EndInit ();
((System.ComponentModel.ISupportInitialize)
(this.bindingSource1)).EndInit ();
this.ResumeLayout (false);

}

#endregion

private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.BindingSource bindingSource1;
#endregion

public Form1 () {
InitializeComponent ();
}

private void Form1_FormClosing (object sender, FormClosingEventArgs
e) {
bindingSource1.EndEdit ();

MessageBox.Show (String.Format ("Numeric Value: {0}\nObject Value:
{1}",
numericUpDown1.Value, exValue.ExampleVal));
}

private void Form1_Load (object sender, EventArgs e) {
exValue = new ExampleClass ();
bindingSource1.DataSource = exValue;
}
}
}

Run the program, change the value in the numeric by typing in a value,
then close the form. You will see that the two values are different
when they should be the same.

Any thoughts on how I can correct this?
 
K

kremp

Check this out: http://support.microsoft.com/default.aspx?scid=kb;en-us;814347

Under resolution you will find the solution. Works perfect for me,
although its only a workaround :)

I used this code (VB)

Private Sub NumericUpDown_Validating(ByVal sender As System.Object,
ByVal e As System.ComponentModel.CancelEventArgs) Handles
Numeric1.Validating, Numeric2.Validating, Numeric3.Validating
Dim s As String = CType(sender,
System.Windows.Forms.NumericUpDown).Value.ToString()
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top