Databinding a control to the negative of a bool property

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I posted this message to another board and have hardly had any views on it,
leave alone answers. So I am cross-posting here.

This may be a very simple question but I can't get my head around it.

I have a textbox's enabled property bound to the IsNew property of an object.

this.txtPhone1.DataBindings.Add("Enabled", this.BindingSource, "IsNew", true);

So when the IsNew property is true, my textbox is enabled. This is correct
behavior.

However, I want to reverse this behavior. In other words, I'd like to bind
the enabled property to !IsNew, so that the textbox is disabled, when the
IsNew property is true. How can I do this without creating a new property on
the object that returns !IsNew??

Thanks,

Naveen
 
Naveen,

I assume that the IsNew property has 2 stati.

True and false.

Therefore your binding says when it is false.
("Enabled", this.BindingSource, Property , true);
Textbox.Enabled = false;

The IsNew in your binding is only an address, it has no sense to set that
negative.

Cor
 
Cor,
Thanks for your reply. I'm not sure I fully understand your reply.Let me
explain in a little detail.

1) A textbox is bound to an object.

2) The object has an IsNew property.
private bool _isNew = false;
public bool IsNew
{
get{return _isNew;}
set{_isNew = value}
}

3) I need to bind the enabled property of a textbox to the negative of the
value of IsNew. i.e. if IsNew = true, I want the textbox.Enabled = false and
if IsNew = false, I want textbox.Enabked = true.
Obviously I would like to do this in 1 databinding statement, without any if
statements. This enables me to set the databinding and forget about it,
because I'll have the DataSourceUpdateMode to OnPropertyChanged.

So this statement,

this.txtPhone1.DataBindings.Add("Enabled", this.BindingSource, "IsNew",
true, DataSourceUpdateMode.OnPropertyChanged);

does the exact reverse of what I need. It sets txtPhone1.Enabled = true if
IsNew = true and vice-versa.

I guess my question is, is there a way to change this statement to set
txtPhone1.Enabled = true, if IsNew = false and vice-versa??

Thanks,
Naveen
 
Hi,

Naveen said:
Cor,
Thanks for your reply. I'm not sure I fully understand your reply.Let me
explain in a little detail.

1) A textbox is bound to an object.

2) The object has an IsNew property.
private bool _isNew = false;
public bool IsNew
{
get{return _isNew;}
set{_isNew = value}
}

3) I need to bind the enabled property of a textbox to the negative of the
value of IsNew. i.e. if IsNew = true, I want the textbox.Enabled = false
and
if IsNew = false, I want textbox.Enabked = true.
Obviously I would like to do this in 1 databinding statement, without any
if
statements. This enables me to set the databinding and forget about it,
because I'll have the DataSourceUpdateMode to OnPropertyChanged.

So this statement,

this.txtPhone1.DataBindings.Add("Enabled", this.BindingSource, "IsNew",
true, DataSourceUpdateMode.OnPropertyChanged);

You could add an eventhandler for Parse and Format:

Dim bnd As Binding = New Binding("Enabled", this.BindingSource, _
"IsNew", True, DataSourceUpdateMode.OnPropertyChanged)

AddHandler bnd.Parse, AddressOf InverseBool
AddHandler bnd.Format, AddressOf InverseBool

txtPhone1.DataBindings.Add( bnd )

Private Sub InverseBool( sender As Object, e As ConvertEventArgs )
e.Value = Not CBool( e.Value )
End Function


HTH,
Greetings
 
Bart,

That it is, I was thinking how to do it, but this is of course the answer.

I always forget how handy that binding class is.

Cor
 
I mean

how handy that binding event.

Cor

Bart Mermuys said:
Hi,



You could add an eventhandler for Parse and Format:

Dim bnd As Binding = New Binding("Enabled", this.BindingSource, _
"IsNew", True, DataSourceUpdateMode.OnPropertyChanged)

AddHandler bnd.Parse, AddressOf InverseBool
AddHandler bnd.Format, AddressOf InverseBool

txtPhone1.DataBindings.Add( bnd )

Private Sub InverseBool( sender As Object, e As ConvertEventArgs )
e.Value = Not CBool( e.Value )
End Function


HTH,
Greetings
 
Works great. Thanks a lot.

Bart Mermuys said:
Hi,



You could add an eventhandler for Parse and Format:

Dim bnd As Binding = New Binding("Enabled", this.BindingSource, _
"IsNew", True, DataSourceUpdateMode.OnPropertyChanged)

AddHandler bnd.Parse, AddressOf InverseBool
AddHandler bnd.Format, AddressOf InverseBool

txtPhone1.DataBindings.Add( bnd )

Private Sub InverseBool( sender As Object, e As ConvertEventArgs )
e.Value = Not CBool( e.Value )
End Function


HTH,
Greetings
 
Back
Top