Board index » cppbuilder » Too many OnClick Events from RadioGroup or Buttons

Too many OnClick Events from RadioGroup or Buttons

Hello list,

the behaviour of the RadioGroups and RadioButtons drive me crazy.
Whenever I change the index of  a RadioGroup with
      RadioGroup->ItemIndex   = 1;
or the checked Property of a RadioButton with
    RadioButton->Checked = true;

The eventmethod OnClick( ) is triggered. But this must not be. The change
was not a user request.
To tried to prevent this behaviour by taking an other way:
The BaseClass TControl provides the Method Perform( ). Which sends
WindowMessages to Controls.

    RadioButton1->Perform(BM_SETCHECK,true,0);
    RadioButton2->Perform(BM_SETCHECK,false,0);

With this piece of code a RadioButton can be checked without triggering the
event OnClick().
But now a MouseClick on the RadioButton2 does not trigger the OnClick()
Method either.
You first have to click one the already checked Button RadioButton1.

Is this behaviour usefull? Why ist the OnClick() Event triggered when the
was not any CLICK?
The problem is, that I need both ways. The user must be able to Change the
settings and I want to change it
within the code without executing the same routines.
I hope anyone has any suggestions? This little problem keeps me up since two
days.

Greetings
    Tim Lapawa

     LocalRadioBtn->Perform(BM_SETCHECK,true,0);

 

Re:Too many OnClick Events from RadioGroup or Buttons


I don't know if the OnClick event should not be triggered in the case you
described or it is useful, however it happens also for CheckBoxes and other
components. When are you setting the ItemIndex property? In my programs
normally it is an initialization (i.e. reading the value from INI file) so I
found the following workaround (put it in the constructor of the main form
for example):

bool Initialize = true;
ReadINI();    /* all initializations here inside (i.e.
RadioGroup1->ItemIndex = 0;) */
Initialize = false;

and in the OnClick event something similar to:

if (!Initialize)
    {
    /* normal OnClick operation */
    }

Hoping this helps,

Steve.

Quote
Tim Lapawa <lap...@insynergie.de> wrote in message 3d1c7263_2@dnews...
> Hello list,

> the behaviour of the RadioGroups and RadioButtons drive me crazy.
> Whenever I change the index of  a RadioGroup with
>       RadioGroup->ItemIndex   = 1;
> or the checked Property of a RadioButton with
>     RadioButton->Checked = true;

> The eventmethod OnClick( ) is triggered. But this must not be. The change
> was not a user request.
> To tried to prevent this behaviour by taking an other way:
> The BaseClass TControl provides the Method Perform( ). Which sends
> WindowMessages to Controls.

>     RadioButton1->Perform(BM_SETCHECK,true,0);
>     RadioButton2->Perform(BM_SETCHECK,false,0);

> With this piece of code a RadioButton can be checked without triggering
the
> event OnClick().
> But now a MouseClick on the RadioButton2 does not trigger the OnClick()
> Method either.
> You first have to click one the already checked Button RadioButton1.

> Is this behaviour usefull? Why ist the OnClick() Event triggered when the
> was not any CLICK?
> The problem is, that I need both ways. The user must be able to Change the
> settings and I want to change it
> within the code without executing the same routines.
> I hope anyone has any suggestions? This little problem keeps me up since
two
> days.

> Greetings
>     Tim Lapawa

>      LocalRadioBtn->Perform(BM_SETCHECK,true,0);

Re:Too many OnClick Events from RadioGroup or Buttons


On Fri, 28 Jun 2002 16:33:07 +0200, "Tim Lapawa"

Quote
<lap...@insynergie.de> wrote:
>The eventmethod OnClick( ) is triggered. But this must not be. The change
>was not a user request.

I often find this behaviour useful, but when I don't I create a form
class flag called IgnoreEvents, which I set before the code
affecting the control and clear after it. The control's handler checks
this flag before executing any other code.

Re:Too many OnClick Events from RadioGroup or Buttons


Hi Tim,
I have trouble with the OnClick event (esp. for Checkboxes) so often. And
the Perform-thing doesn't work for me either. Internally the checkbox
changes the state, but it doesn't show the correct state.
So I decided to do it the safe way. Before setting the Checked property I
save the OnClick method, assign a Null, do the assignment and restore the
OnClick.
I am to lazy, but it must be easy to create own components with appropriate
methods.
HTH
Juergen

"Tim Lapawa" <lap...@insynergie.de> schrieb im Newsbeitrag
news:3d1c7263_2@dnews...

Quote
> Hello list,

> the behaviour of the RadioGroups and RadioButtons drive me crazy.
> Whenever I change the index of  a RadioGroup with
>       RadioGroup->ItemIndex   = 1;
> or the checked Property of a RadioButton with
>     RadioButton->Checked = true;

> The eventmethod OnClick( ) is triggered. But this must not be. The change
> was not a user request.
> To tried to prevent this behaviour by taking an other way:
> The BaseClass TControl provides the Method Perform( ). Which sends
> WindowMessages to Controls.

>     RadioButton1->Perform(BM_SETCHECK,true,0);
>     RadioButton2->Perform(BM_SETCHECK,false,0);

> With this piece of code a RadioButton can be checked without triggering
the
> event OnClick().
> But now a MouseClick on the RadioButton2 does not trigger the OnClick()
> Method either.
> You first have to click one the already checked Button RadioButton1.

> Is this behaviour usefull? Why ist the OnClick() Event triggered when the
> was not any CLICK?
> The problem is, that I need both ways. The user must be able to Change the
> settings and I want to change it
> within the code without executing the same routines.
> I hope anyone has any suggestions? This little problem keeps me up since
two
> days.

> Greetings
>     Tim Lapawa

>      LocalRadioBtn->Perform(BM_SETCHECK,true,0);

Re:Too many OnClick Events from RadioGroup or Buttons


Quote
"Tim Lapawa" <lap...@insynergie.de> wrote in message

news:3d1c7263_2@dnews...

Quote
> Whenever I change the index of  a RadioGroup with
>       RadioGroup->ItemIndex   = 1;
> or the checked Property of a RadioButton with
>     RadioButton->Checked = true;

Correct.  It is explitically hard-coded to do so.

Quote
> The eventmethod OnClick( ) is triggered. But this must not be.
> The change was not a user request.

You'll have to disable the event when you are changing the states
programmably, either by using a flag to tell the handler whether it can
execute the code or not, or else by simply removing the event handler itself
temporarily.

    bool Updating = false;

    Updating = true;
    RadioButton->Checked = true;
    Updating = false;

    void __fastcall TForm1::RadioButtonClick(TObject *Sender)
    {
        if(Updating)
            return;

        // your usual code here
    }

Or:

    TNotifyEvent OldEvent = RadioButton->OnClick;
    RadioButton->OnClick = NULL;
    RadioButton->Checked = true;
    RadioButton->OnClick = OldEvent;

Quote
> Is this behaviour usefull?

Sometimes.

Quote
> Why ist the OnClick() Event triggered when the was not any CLICK?

Because the source code is explitically triggering it manually.

Quote
> The problem is, that I need both ways. The user must be able to
> Change the settings and I want to change it within the code
> without executing the same routines.

See my comment above.

Gambit

Re:Too many OnClick Events from RadioGroup or Buttons


Isn't my code below a better option?
It's simpel, but works with me.
(If needed you could use the *Sender in combination with Casting for broader
use..)

if ( ActiveControl != your-radio-button ) return;

OR:

if ( !your-radio-button->Focused() ) return;

Re:Too many OnClick Events from RadioGroup or Buttons


Hello list,

the behaviour of the RadioGroups and RadioButtons drive me crazy.
Whenever I change the index of  a RadioGroup with
      RadioGroup->ItemIndex   = 1;
or the checked Property of a RadioButton with
    RadioButton->Checked = true;

The eventmethod OnClick( ) is triggered. But this must not be. The change
was not a user request.
To tried to prevent this behaviour by taking an other way:
The BaseClass TControl provides the Method Perform( ). Which sends
WindowMessages to Controls.

    RadioButton1->Perform(BM_SETCHECK,true,0);
    RadioButton2->Perform(BM_SETCHECK,false,0);

With this piece of code a RadioButton can be checked without triggering the
event OnClick().
But now a MouseClick on the RadioButton2 does not trigger the OnClick()
Method either.
You first have to click one the already checked Button RadioButton1.

Is this behaviour usefull? Why ist the OnClick() Event triggered when the
was not any CLICK?
The problem is, that I need both ways. The user must be able to Change the
settings and I want to change it
within the code without executing the same routines.
I hope anyone has any suggestions? This little problem keeps me up since two
days.

Greetings
    Tim Lapawa

     LocalRadioBtn->Perform(BM_SETCHECK,true,0);

Re:Too many OnClick Events from RadioGroup or Buttons


I don't know if the OnClick event should not be triggered in the case you
described or it is useful, however it happens also for CheckBoxes and other
components. When are you setting the ItemIndex property? In my programs
normally it is an initialization (i.e. reading the value from INI file) so I
found the following workaround (put it in the constructor of the main form
for example):

bool Initialize = true;
ReadINI();    /* all initializations here inside (i.e.
RadioGroup1->ItemIndex = 0;) */
Initialize = false;

and in the OnClick event something similar to:

if (!Initialize)
    {
    /* normal OnClick operation */
    }

Hoping this helps,

Steve.

Quote
Tim Lapawa <lap...@insynergie.de> wrote in message 3d1c7263_2@dnews...
> Hello list,

> the behaviour of the RadioGroups and RadioButtons drive me crazy.
> Whenever I change the index of  a RadioGroup with
>       RadioGroup->ItemIndex   = 1;
> or the checked Property of a RadioButton with
>     RadioButton->Checked = true;

> The eventmethod OnClick( ) is triggered. But this must not be. The change
> was not a user request.
> To tried to prevent this behaviour by taking an other way:
> The BaseClass TControl provides the Method Perform( ). Which sends
> WindowMessages to Controls.

>     RadioButton1->Perform(BM_SETCHECK,true,0);
>     RadioButton2->Perform(BM_SETCHECK,false,0);

> With this piece of code a RadioButton can be checked without triggering
the
> event OnClick().
> But now a MouseClick on the RadioButton2 does not trigger the OnClick()
> Method either.
> You first have to click one the already checked Button RadioButton1.

> Is this behaviour usefull? Why ist the OnClick() Event triggered when the
> was not any CLICK?
> The problem is, that I need both ways. The user must be able to Change the
> settings and I want to change it
> within the code without executing the same routines.
> I hope anyone has any suggestions? This little problem keeps me up since
two
> days.

> Greetings
>     Tim Lapawa

>      LocalRadioBtn->Perform(BM_SETCHECK,true,0);

Re:Too many OnClick Events from RadioGroup or Buttons


Hi Tim,
I have trouble with the OnClick event (esp. for Checkboxes) so often. And
the Perform-thing doesn't work for me either. Internally the checkbox
changes the state, but it doesn't show the correct state.
So I decided to do it the safe way. Before setting the Checked property I
save the OnClick method, assign a Null, do the assignment and restore the
OnClick.
I am to lazy, but it must be easy to create own components with appropriate
methods.
HTH
Juergen

"Tim Lapawa" <lap...@insynergie.de> schrieb im Newsbeitrag
news:3d1c7263_2@dnews...

Quote
> Hello list,

> the behaviour of the RadioGroups and RadioButtons drive me crazy.
> Whenever I change the index of  a RadioGroup with
>       RadioGroup->ItemIndex   = 1;
> or the checked Property of a RadioButton with
>     RadioButton->Checked = true;

> The eventmethod OnClick( ) is triggered. But this must not be. The change
> was not a user request.
> To tried to prevent this behaviour by taking an other way:
> The BaseClass TControl provides the Method Perform( ). Which sends
> WindowMessages to Controls.

>     RadioButton1->Perform(BM_SETCHECK,true,0);
>     RadioButton2->Perform(BM_SETCHECK,false,0);

> With this piece of code a RadioButton can be checked without triggering
the
> event OnClick().
> But now a MouseClick on the RadioButton2 does not trigger the OnClick()
> Method either.
> You first have to click one the already checked Button RadioButton1.

> Is this behaviour usefull? Why ist the OnClick() Event triggered when the
> was not any CLICK?
> The problem is, that I need both ways. The user must be able to Change the
> settings and I want to change it
> within the code without executing the same routines.
> I hope anyone has any suggestions? This little problem keeps me up since
two
> days.

> Greetings
>     Tim Lapawa

>      LocalRadioBtn->Perform(BM_SETCHECK,true,0);

Re:Too many OnClick Events from RadioGroup or Buttons


Quote
"Tim Lapawa" <lap...@insynergie.de> wrote in message

news:3d1c7263_2@dnews...

Quote
> Whenever I change the index of  a RadioGroup with
>       RadioGroup->ItemIndex   = 1;
> or the checked Property of a RadioButton with
>     RadioButton->Checked = true;

Correct.  It is explitically hard-coded to do so.

Quote
> The eventmethod OnClick( ) is triggered. But this must not be.
> The change was not a user request.

You'll have to disable the event when you are changing the states
programmably, either by using a flag to tell the handler whether it can
execute the code or not, or else by simply removing the event handler itself
temporarily.

    bool Updating = false;

    Updating = true;
    RadioButton->Checked = true;
    Updating = false;

    void __fastcall TForm1::RadioButtonClick(TObject *Sender)
    {
        if(Updating)
            return;

        // your usual code here
    }

Or:

    TNotifyEvent OldEvent = RadioButton->OnClick;
    RadioButton->OnClick = NULL;
    RadioButton->Checked = true;
    RadioButton->OnClick = OldEvent;

Quote
> Is this behaviour usefull?

Sometimes.

Quote
> Why ist the OnClick() Event triggered when the was not any CLICK?

Because the source code is explitically triggering it manually.

Quote
> The problem is, that I need both ways. The user must be able to
> Change the settings and I want to change it within the code
> without executing the same routines.

See my comment above.

Gambit

Other Threads