CPB Mailing List

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: TRadioGroup question



Here are some old messages I saved. Might help...

--------------

This one kept me up late, but this is what we came up with:
PS: Thanks for the help.
D.

********************  Header file  *************************
//---------------------------------------------------------------------------
#ifndef TMYRadioButtonH
#define TMYRadioButtonH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include <StdCtrls.hpp>
//---------------------------------------------------------------------------
class PACKAGE TMYRadioButton : public TRadioButton
{
private:
    HIDESBASE MESSAGE void __fastcall BMSetState(Messages::TMessage &Message);
    HIDESBASE MESSAGE void __fastcall BMSetCheck(Messages::TMessage &Message);
protected:
    bool    bDisableCheck;
public:
    __fastcall TMYRadioButton(TComponent* Owner);
    __published:
    BEGIN_MESSAGE_MAP
    VCL_MESSAGE_HANDLER(BM_SETSTATE, TMessage, BMSetState);
    VCL_MESSAGE_HANDLER(BM_SETCHECK, TMessage, BMSetCheck);
    END_MESSAGE_MAP(TRadioButton);
};
//---------------------------------------------------------------------------
#endif

********************  Source file  *************************

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "MYRadioButton.h"
#pragma resource "*.res"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.

static inline void ValidCtrCheck(TMYRadioButton *)
{
    new TMYRadioButton(NULL);
}
//---------------------------------------------------------------------------
namespace MYradiobutton
{
    void __fastcall PACKAGE Register()
	{
	    TComponentClass classes[1] = {__classid(TMYRadioButton)};
	    RegisterComponents("MY", classes, 0);
	}
}
//---------------------------------------------------------------------------
__fastcall TMYRadioButton::TMYRadioButton(TComponent* Owner)
    : TRadioButton(Owner), bDisableCheck(false)
{

}
//---------------------------------------------------------------------------
MESSAGE void __fastcall TMYRadioButton::BMSetState(Messages::TMessage &Message)
{
    TRadioButton::Dispatch(&Message);
    
    if (!(HandleAllocated() && (::GetFocus() == Handle))) return;
    BOOL bPushed = (SendMessage(Handle, BM_GETSTATE, 0, 0) & BST_PUSHED) != 0;

    if (Checked && !bPushed) {
        bDisableCheck = true;
        Checked = false;
    }
}
//---------------------------------------------------------------------------
MESSAGE void __fastcall TMYRadioButton::BMSetCheck(Messages::TMessage &Message)
{
    if (bDisableCheck && Message.WParam & BST_CHECKED) {
	bDisableCheck = false;
	Checked = false;
    }
    else
    {
	TRadioButton::Dispatch(&Message);
    }
}
//---------------------------------------------------------------------------


Ken Moffat wrote:

> Actually (sorry) I thought to use something like RadioButton1->Checked =
> !RadioButton1->Checked; for each button in the OnClick Event handler. But
I
> tried it and it doesn't work. hmmm...
>
> -----Original Message-----
> From: Daniel Vachon <daniel.vachon@cgi.ca>
> To: cpb-thread@zdtips.com <cpb-thread@zdtips.com>
> Date: Saturday, April 18, 1998 8:17 AM
> Subject: Re: TRadioButton
>
> >Do you mean to override all events associated to RB's??
> >Thanks,
> >Dan.
> >
> >

=============================
Daniel Vachon
Conseiller
CGI Inc.
Québec (Canada)
=============================

/*
This works: but it's tacky ( I specialize in tacky code...)Simply focus
somewhere else, then uncheck the RadioButtons.

In this test project, the form's property KeyPreview is set to true. The
form has only three TRadioButtons  and a TButton on it.
*/

//The Form's OnKeyPress event-
void __fastcall TForm1::FormKeyPress(TObject *Sender, char &Key)
{
    switch(Key) {
    case VK_SPACE:
	Button1->SetFocus( );
	RadioButton1->Checked = false;
        RadioButton2->Checked = false;
	RadioButton3->Checked = false;
	break;
	
    default:
	break;
    }
}
/*
The apparent outcome of this test is that the focus needs to change from
the "Checked" RadioButton to something else **first**, or it will retain
it's checked status. RadioButtons, by nature, are checked when they have
the focus. Unlike their sister - the CheckBox.
Try commenting out the Button1->SetFocus( ); line.

Now as for using the mouse to deactivate it, how about using the
RadioButton's OnDbl-Click event??? Here's RadioButton1's OnDblClick event:
*/

void __fastcall TForm1::RadioButton1DblClick(TObject *Sender)
{
    if (RadioButton1->Checked == true)
    {
	Button1->SetFocus();
	RadioButton1->Checked = false;
	RadioButton2->Checked = false;
	RadioButton3->Checked = false;
    }
}
//Same code, no switch(key)

"Does this mean you don't need to write a new component?"

Rick Malik
rmfci@pacbell.net
Visit the C++ Builder Hobby Pages
http://home.pacbell.net/rmfci/bcbhp.htm

----------
> From: Daniel Vachon <daniel.vachon@cgi.ca>
> To: cpb-thread@zdtips.com
> Subject: TRadioButton
> Date: Thursday, April 09, 1998 8:32 AM
>
> Hello,
> I'm creating a TRadioButton component, it's particularity is: i want it
> to react in a group like a regular RB, but if checked, i want the
> possibility to uncheck it by a click or a spacebar down. Windows seems
> to receive 2 BNclicks so i can't change it's checked status with the
> OnClick event.
> Has anyone ever done this...
> Thanks in advance,
> Dan.
>
> =============================
> Daniel Vachon
> Conseiller
> CGI Inc.
> Québec (Canada)
> =============================


W Komornicki's Home Page | Main Index | Thread Index