Checkbox binding Parse event

J

JezB

I'm binding a checkbox to a Y/N value from the database, and providing
format/parse events to handle the conversion between Y/N and true/false
states :-

Binding b = new Binding("Checked", myView, "YNField");
b.Format += new ConvertEventHandler(boolFormat);
b.Parse += new ConvertEventHandler(boolParse);
myCheckBox.DataBindings.Add(b);

private void boolFormat(object sender, ConvertEventArgs e)
{ if (e.Value.ToString() == "Y" || e.Value.ToString() == "")
e.Value = true;
else e.Value = false;
}
private void boolParse(object sender, ConvertEventArgs e)
{ if ((bool)e.Value == true) e.Value = "Y";
else e.Value = "N";
}
This seems to work fine, but I have noticed that the Parse event is not
fired immediately on changing the checkbox. It fires when (for example), I
move to a different tab page or close the form. I have tried putting the
following into the CheckedChanged event of the checkbox to force it :-

BindingManagerBase bm = BindingContext[myView, "YNField"];
bm.EndCurrentEdit();

but it still doesn't work. Any ideas ?
 
J

JezB

I saw my own error just as I posted. Second last line of code should of
course be :-

BindingManagerBase bm = BindingContext[myView];
 
J

JezB

But now I find weird things can happen if I do this, like losing the context
of the binding completely if I turn the checkbox off and on again ... so I'm
still stuck.

JezB said:
I saw my own error just as I posted. Second last line of code should of
course be :-

BindingManagerBase bm = BindingContext[myView];


JezB said:
I'm binding a checkbox to a Y/N value from the database, and providing
format/parse events to handle the conversion between Y/N and true/false
states :-

Binding b = new Binding("Checked", myView, "YNField");
b.Format += new ConvertEventHandler(boolFormat);
b.Parse += new ConvertEventHandler(boolParse);
myCheckBox.DataBindings.Add(b);

private void boolFormat(object sender, ConvertEventArgs e)
{ if (e.Value.ToString() == "Y" || e.Value.ToString() == "")
e.Value = true;
else e.Value = false;
}
private void boolParse(object sender, ConvertEventArgs e)
{ if ((bool)e.Value == true) e.Value = "Y";
else e.Value = "N";
}
This seems to work fine, but I have noticed that the Parse event is not
fired immediately on changing the checkbox. It fires when (for example), I
move to a different tab page or close the form. I have tried putting the
following into the CheckedChanged event of the checkbox to force it :-

BindingManagerBase bm = BindingContext[myView, "YNField"];
bm.EndCurrentEdit();

but it still doesn't work. Any ideas ?
 

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