Databinding Winforms Textbox
In a nutshell, this databinding works correctly
tbTotalTaxDue.DataBindings.Add("Text", I, "TotalTaxDue");
Namely, when I enter an invalid value—like an empty string—then tab out of the TB, the value therein just reverts to the previous value. This makes sense since the value开发者_Python百科 entered won't go into the object property of type decimal. Unfortunately though, either of these databindings:
tbTotalTaxDue.DataBindings.Add("Text", I, "TotalTaxDue", true, DataSourceUpdateMode.OnPropertyChanged, 0, "C");
tbTotalTaxDue.DataBindings.Add("Text", I, "TotalTaxDue", true, DataSourceUpdateMode.OnValidation, 0, "C");
Behave differently in that when the user enters an empty string, the input will not let the user tab out of the Text Box. Is there any way to get the databinding to display as a currency, but simply cancel any invalid edits?
You have a couple options:
Subclass
TextBox
and override theOnValidating
event. IfText
is empty, exit before callingMyBase.OnValidating()
to suppress the event.Set
CausesValidation = False
on theTextBox
, and handle theTextChanged
event. IfText
is not empty, manually validate it.
I hope this helps.
精彩评论