Trying to bind to Visible or Enabled property of form control

G

Guest

Hi,

just a quick one.
i'm trying to bind the Visible property of a control to a public property of
the parent form.
i.e.
private bool _okToDoSomething = false;

public bool IsOkToDoSometing {
get { return this._okToDoSomething; }
}

/// then in initialisation
this.buttonDoSomething.DataBindings.Add("Enabled",this, "IsOkToDoSomething");

i've read that binding can only occur if bound object implement IList (which
the form obviously doesn't) - so is something like this possible? and if so
how do I do it?
 
B

Bart Mermuys

Hi,

Sam Martin said:
Hi,

just a quick one.
i'm trying to bind the Visible property of a control to a public property
of
the parent form.
i.e.
private bool _okToDoSomething = false;

public bool IsOkToDoSometing {
get { return this._okToDoSomething; }
}

/// then in initialisation
this.buttonDoSomething.DataBindings.Add("Enabled",this,
"IsOkToDoSomething");

i've read that binding can only occur if bound object implement IList
(which
the form obviously doesn't) - so is something like this possible? and if
so

Well, binding of lists obvious need to implement IList (or at least
IListSource), but you can also bind to any property of any object.

You do need to add an event (propertyname + 'Changed'), so that bound
control(s) know when the property has changed, eg:

public event IsOkToDoSomethingChanged;

public bool IsOkToDoSometing
{
get { return this._okToDoSomething; }
set
{
_okToDoSomething = value;

// fire event
if ( IsOkToDoSomethingChanged!=null )
IsOkToDoSomething( this, EventArgs.Empty );
}
}

// then in initialisation
this.buttonDoSomething.DataBindings.Add("Enabled",this,
"IsOkToDoSomething");

Now you can use IsOkToDoSometing property to toggle button enabled state.


HTH,
Greetings
 
G

Guest

hi bart, thanks for that

however, i've done as you said but the bindings are being updated.
when i step through a call to the IsNewAgreementCreated_set, the event is
not handled and is <undefined value>.

the binding is done after the call to InitialiseComponent.

this.newAgreementPanel.DataBindings.Add("Visible",this,"IsNewAgreementCreated");

public event EventHandler IsNewAgreementCreatedChanged;
public bool IsNewAgreementCreated
{
get { return this._createdAgreement; }
set
{
this._createdAgreement = value;

if (IsNewAgreementCreatedChanged!=null)
IsNewAgreementCreatedChanged(this,EventArgs.Empty);
}
}

is there anything else i need to do??

--
TIA
Sam Martin


Bart Mermuys said:
Hi,

Sam Martin said:
Hi,

just a quick one.
i'm trying to bind the Visible property of a control to a public property
of
the parent form.
i.e.
private bool _okToDoSomething = false;

public bool IsOkToDoSometing {
get { return this._okToDoSomething; }
}

/// then in initialisation
this.buttonDoSomething.DataBindings.Add("Enabled",this,
"IsOkToDoSomething");

i've read that binding can only occur if bound object implement IList
(which
the form obviously doesn't) - so is something like this possible? and if
so

Well, binding of lists obvious need to implement IList (or at least
IListSource), but you can also bind to any property of any object.

You do need to add an event (propertyname + 'Changed'), so that bound
control(s) know when the property has changed, eg:

public event IsOkToDoSomethingChanged;

public bool IsOkToDoSometing
{
get { return this._okToDoSomething; }
set
{
_okToDoSomething = value;

// fire event
if ( IsOkToDoSomethingChanged!=null )
IsOkToDoSomething( this, EventArgs.Empty );
}
}

// then in initialisation
this.buttonDoSomething.DataBindings.Add("Enabled",this,
"IsOkToDoSomething");

Now you can use IsOkToDoSometing property to toggle button enabled state.


HTH,
Greetings
 
B

Bart Mermuys

Hi,

Sam Martin said:
hi bart, thanks for that

however, i've done as you said but the bindings are being updated.

I guess you mean aren't updated .
when i step through a call to the IsNewAgreementCreated_set, the event is
not handled and is <undefined value>.

If you set IsNewAgreementCreated after binding, then the event shouldn't be
the binding is done after the call to InitialiseComponent.

That shouldn't be a problem.
this.newAgreementPanel.DataBindings.Add("Visible",this,"IsNewAgreementCreated");

public event EventHandler IsNewAgreementCreatedChanged;
public bool IsNewAgreementCreated
{
get { return this._createdAgreement; }
set
{
this._createdAgreement = value;

if (IsNewAgreementCreatedChanged!=null)
IsNewAgreementCreatedChanged(this,EventArgs.Empty);
}
}

I have copy/pasted the above code and added the private variable
_createdAgreement and a panel and it works perfectly (using net 1.1 vc2003).
I have no idea why it doesn't for you, maybe something's different but it's
not in the posted code.

Greetings
is there anything else i need to do??
 
G

Guest

thank for your help mate

--
TIA
Sam Martin


Bart Mermuys said:
Hi,



I guess you mean aren't updated .


If you set IsNewAgreementCreated after binding, then the event shouldn't be


That shouldn't be a problem.


I have copy/pasted the above code and added the private variable
_createdAgreement and a panel and it works perfectly (using net 1.1 vc2003).
I have no idea why it doesn't for you, maybe something's different but it's
not in the posted code.

Greetings
 

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