Control validation problem prevents me from tabbing out of a contr

G

Guest

I am using simple databinding on a small form with only about 10
controls. My problem is that the _second_ time I open the form and
edit a value, I am no longer able to tab or click out of the control
that I just changed the value in.

Here's what I mean:
1. Open the app
2. Open the Details form (form uses databound values for textboxes,
code below)
3. Edit the value in the ItemName textbox
4. Click the OK button to close the form
5. Open the Details form a second time
6. Edit the value in the ItemName textbox

Result --> I am now unable to tab out of the ItemName textbox, click
OK or do anything but close the form.

This appears to be a problem with validation, since I cannot click on any
buttons with CausesValidation = true but I can click on buttons with
CausesValidation = false. However, none of my controls on the form have an
event handler for the Validating event.

This happens consistently throughout my application, on both small
forms and large forms.

I must be doing something incorrectly. Can someone point it out to me?


Here's some code:

MyDataSet.MyRow row = // a valid row from a typed dataset
Inside Form_Load:
BindField(ItemName, "Text", row, "ItemName");

public static void BindField(Control control,
string propertyName,
object dataSource,
string dataMember)
{
Binding binding =
control.DataBindings[propertyName];
if (null != binding)
control.DataBindings.Remove(binding);
control.DataBindings.Add(
propertyName, dataSource, dataMember);
}
 
G

Guest

Joseph,

I am closing the form, but the problem was still happenning.

What I finally discovered is that I needed to clear all my databindings in
the Dispose method of the form. Adding the code to clear the bindings in the
Dispose method corrected the problem.

For future reference here's the code I am using. I got if from a MSDN
article title "Field Views on Windows Forms 1.0 and 1.1 Data Binding":

internal static void ClearBindings(Control c)
{
Binding[] bindings = new Binding[c.DataBindings.Count];
c.DataBindings.CopyTo(bindings, 0);
c.DataBindings.Clear();
foreach (Binding binding in bindings)
{
TypeDescriptor.Refresh(binding.DataSource);
}
foreach (Control cc in c.Controls)
{
ClearBindings(cc);
}
}

-- doug

Sijin Joseph said:
Hi Doug,

Are you closing the form or just hiding it?..Do you create a new
instance of the form when you show it again?

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Doug said:
I am using simple databinding on a small form with only about 10
controls. My problem is that the _second_ time I open the form and
edit a value, I am no longer able to tab or click out of the control
that I just changed the value in.

Here's what I mean:
1. Open the app
2. Open the Details form (form uses databound values for textboxes,
code below)
3. Edit the value in the ItemName textbox
4. Click the OK button to close the form
5. Open the Details form a second time
6. Edit the value in the ItemName textbox

Result --> I am now unable to tab out of the ItemName textbox, click
OK or do anything but close the form.

This appears to be a problem with validation, since I cannot click on any
buttons with CausesValidation = true but I can click on buttons with
CausesValidation = false. However, none of my controls on the form have an
event handler for the Validating event.

This happens consistently throughout my application, on both small
forms and large forms.

I must be doing something incorrectly. Can someone point it out to me?


Here's some code:

MyDataSet.MyRow row = // a valid row from a typed dataset
Inside Form_Load:
BindField(ItemName, "Text", row, "ItemName");

public static void BindField(Control control,
string propertyName,
object dataSource,
string dataMember)
{
Binding binding =
control.DataBindings[propertyName];
if (null != binding)
control.DataBindings.Remove(binding);
control.DataBindings.Add(
propertyName, dataSource, dataMember);
}
 

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