changing UI state generate UI events

  • Thread starter Thread starter # Cyrille37 #
  • Start date Start date
C

# Cyrille37 #

Hello,

When we update an UI control by code, that control generates a event.
For example, if we make a CheckBox Checked, then the CheckedChanged event is fired.
That is a goog behavior, but not at the first time (initialization).

Here the scenario :

<code>
SomeObject data ;
....
void UpdateData2UI()
{
this.checkbox.checked = data.IsPlaying ;
}
void checkbox_CheckedChanged(...)
{
data.IsPlaying = this.checkbox.checked ;
}
</code>

At UI initialization data.IsPlaying is "read" then "write" without necessity.
It could be a bad stuff if data.IsPlaying has to do some stuff.

Is the only solution is to add a test in checkbox_CheckedChanged ??
Like :
<code>
void checkbox_CheckedChanged(...)
{
if( this.checkbox.checked != data.IsPlaying )
{
data.IsPlaying = this.checkbox.checked ;
}
}
</code>

How do you do for that situation ?

I hope my explanation are clear enough ...

cyrille
 
"# Cyrille37 #" <[email protected]> a écrit dans le message de (e-mail address removed)...

| When we update an UI control by code, that control generates a event.
| For example, if we make a CheckBox Checked, then the CheckedChanged event
is fired.
| That is a goog behavior, but not at the first time (initialization).

You really ought to make the UI update itself based on the state of the
object, not the other way around. Try to set the Checked state based on the
IsPlaying property when you initialise the form.

Joanna
 
Joanna Carter [TeamB] :
"# Cyrille37 #" <[email protected]> a écrit dans le message de
(e-mail address removed)...

| When we update an UI control by code, that control generates a event.
| For example, if we make a CheckBox Checked, then the CheckedChanged event
is fired.
| That is a goog behavior, but not at the first time (initialization).

You really ought to make the UI update itself based on the state of the
object, not the other way around. Try to set the Checked state based onthe
IsPlaying property when you initialise the form.

thanks for your help.

That is the form (an UserControl in fact) is always present.
In another UserControl when selected a Item in a TreeView, I'm refreshingdata.
So the Checked state could not be set in Form/UserControl initialization.

cyrille
 
"# Cyrille37 #" <[email protected]> a écrit dans le message de (e-mail address removed)...

| That is the form (an UserControl in fact) is always present.
| In another UserControl when selected a Item in a TreeView, I'm refreshing
data.
| So the Checked state could not be set in Form/UserControl initialization.

Then I would suggest that you hold a local variable that is set to the
selected item in the TreeView. In the event handler that detects the change
in the TreeView, you can then set the local variable and update the
CheckBox.

Joanna
 
Joanna Carter [TeamB] a écrit :
"# Cyrille37 #" <[email protected]> a écrit dans le message de
(e-mail address removed)...

| That is the form (an UserControl in fact) is always present.
| In another UserControl when selected a Item in a TreeView, I'm refreshing
data.
| So the Checked state could not be set in Form/UserControl initialization.

Then I would suggest that you hold a local variable that is set to the
selected item in the TreeView. In the event handler that detects the change
in the TreeView, you can then set the local variable and update the
CheckBox.

Yes, it's what I do.
But when I'm updating the checkbox, in response the checkbox fire an event which
update data ... The only solution I've found is to compare checkbox stateand
data state, if they are different I update data.

cyrille
 
# Cyrille37 # said:
Hello,

When we update an UI control by code, that control generates a event.
For example, if we make a CheckBox Checked, then the CheckedChanged event
is fired.
That is a goog behavior, but not at the first time (initialization).

Here the scenario :

<code>
SomeObject data ;
...
void UpdateData2UI()
{
this.checkbox.checked = data.IsPlaying ;
}
void checkbox_CheckedChanged(...)
{
data.IsPlaying = this.checkbox.checked ;
}
</code>

At UI initialization data.IsPlaying is "read" then "write" without
necessity.
It could be a bad stuff if data.IsPlaying has to do some stuff.

Is the only solution is to add a test in checkbox_CheckedChanged ??
Like :
<code>
void checkbox_CheckedChanged(...)
{
if( this.checkbox.checked != data.IsPlaying )
{
data.IsPlaying = this.checkbox.checked ;
}
}
</code>

How do you do for that situation ?

There are a few options. One is to set the event handlers not at design time
but at run time. Then the order of displaying your data is remove handlers,
update your controls and then add the handlers. Second option is to have a
bool flag like loadingData that you check in each event handler like
if(loadingData) return;. Third option is to use another event like Leave on
some controls where it is not really necessary for the changes that are made
to immediately update the state of your application but rather update when
the user has finished and leaves the control to do something else so the
Leave indicates "I have finished".

HTH

SP
 
SP a écrit :
There are a few options. One is to set the event handlers not at designtime
but at run time. Then the order of displaying your data is remove handlers,
update your controls and then add the handlers. Second option is to have a
bool flag like loadingData that you check in each event handler like
if(loadingData) return;. Third option is to use another event like Leave on
some controls where it is not really necessary for the changes that aremade
to immediately update the state of your application but rather update when
the user has finished and leaves the control to do something else so the
Leave indicates "I have finished".


Thank you very much for your clear explanation of choices.

Now I've material for thinking ;o)

bye
cyrille
 
Back
Top