Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

Why can't I close my Form when using Data Binding?

By default, simple binding updates a bound data source property value as part of Control validation.  Control validation occurs when a Control loses focus (see Controlling the Binding Operation for more information).  Validation also occurs when a Form is closed either by using the OS provided close button (the red X on XP platforms) or when explicitly calling Form.Close().  If an exception occurs during data binding, the data binding runtime will force the validation process to fail.   If this data binding fails when closing a Form, this will have the affect of preventing the form from closing.  In VS 2005, a new cancelable event was adding to Form called FormClosing.  This event can be used to force close a Form when validation fails (see sample below).

In cases where you are trying to close a Form via a Close or Cancel button, you will need to use the combination of the "CausesValidation" property and the FormClosing event (see sample below).  When you set "CausesValidation" to false on a Control, it will prevent automatic validation from occurring as a result of the Control taking focus.  For example, if you have a TextBox and a Button and set the Button.CausesValidation to false, tabbing from the TextBox to the Button will not cause validation to occur on the TextBox (and therefore, simple data binding will not occur on the TextBox).

Sample: Allowing a Form to close when a data binding error occurs (VS 2005) (VS Project: SimpleBindingTabIssue)

/*******************************************************************
* Setup (using the Visual Studio Form designer):
* 
* Add 1 TextBox to the Form (textBox1)
* Add 1 Button to the Form (closeBtn)
* Add 2 CheckBoxes to the Form (allowFormCloseCB, allowCloseOnCloseFormCB)
* 
* Add the following public property to the Form class:
*   
*   private int _intValue;
*
*   public int IntegerValue
*   {
*       get { return _intValue; }
*       set { _intValue = value; }
*   }
* 
* Add the following code to the Form.Load event:
******************************************************************/

Binding         tbBinding = new Binding("Text", this, "IntegerValue", true);
bool            forceClose = false;

/* Add binding */
this.textBox1.DataBindings.Add(tbBinding);

/* Allow Form to Close */
this.FormClosing += delegate(object form, FormClosingEventArgs fargs)
{
    if (fargs.Cancel && (this.allowFormCloseCB.Checked || forceClose))
    {
        /* Allow close */
        fargs.Cancel = false;
    }
};

/* Allow Form to Close when using the "Close Form" button */
this.allowCloseOnCloseFormCB.CheckedChanged += delegate
{
    this.closeBtn.CausesValidation = !this.allowCloseOnCloseFormCB.Checked;
};

/* Exit the application */
this.closeBtn.Click += delegate
{
    /* Close the Form */
    forceClose = true;
    this.Close();
};


Page view counter