How to show a different text for bool
Hi,
is there a simple way to display a different text on a bool-bound text.
Assume I have an app with a label showing some bool value from an SQL-Table.
This shows True or False - what I want is to show (for an example) Yes / No, or 1 / 0 on "Out off office" / "In the office".
Another "problem" is that I sometimes want to bind reverse.
So for an example I want to hide a button if some expression is true.
The other way is simple - I bind the Visible attribute to the bool - but what if this is different.
Or what if I want to enable when a textfield has a certain value?
In ASP.NET I would write something like Attr="<%# Eval("Field")=="Simple" ? "Is Simple" : "Comlicated" %>
I messed around a lot with custom format and could not find a way (other than codeing).
Cheers
Manfred
[818 byte] By [
ManniAT] at [2008-2-10]
Hi Joe,
thank you for the reply, but I'm not very shure if this helps in my situation.
First I have a label bound to a Field in a binding source.
The binding code looks like this
| | lblIsRegistred.DataBindings.Add(new Binding("Text", SettingsBindingSource, "IsRegistred", true, DataSourceUpdateMode.Never)); |
Since it is a label no changes are needed. But it writes True / False instead of Yes / No.
The other thing is a button I want to show when a boolean is FALSE.
At the moment it is doing exact the oposite
| | btnStartSync.DataBindings.Add(new Binding("Visible", SettingsBindingSource, "IsRegistred", true, DataSourceUpdateMode.Never)); |
I can't see how that would work with a lookup table!!
Cheers
Manfred
Yes - in the first scenario you'll need to hook the Binding.Format event:
| | b = new Binding("Text", SettingsBindingSource, "IsRegistered", true, DataSourceUpdateMode.Never); b.Format += delegate(object s, ConvertEventArgs c) { c.Value = (((bool)c.Value) ? "Yes" : "No"); }; lblIsRegistred.DataBindings.Add(b); |
For the second, you need to do a similar thing:
| | Binding b = new Binding("Visible", SettingsBindingSource, "IsRegistered", true, DataSourceUpdateMode.Never); b.Format += delegate(object send, ConvertEventArgs args) { args.Value = (((bool)args.Value) ? false : true); }; btnStartSync.DataBindings.Add(b); |
Joe
Hi Joe,
so the answer to my Thread is not YES - it is NO 
NO: there is no simple way.....
I'm used to do such "declarative binding" from ASP.NET and had a hope that this is also possible in Windows.Forms.
But - as you showed - it is possible without doing "extreme things".
I'v seen a similar example on MSDN - but they did a more complex thing so I was hopeing....
Thanks for your kindfull help
Manfred