Board index » cppbuilder » get parent component / access to other components of my class

get parent component / access to other components of my class

Hello,

my new class, has the following structure:

class TMyGroupBox : public TGroupBox
{
    private:
        TLabel *LTable;
        TLabel *LColumn;
        TBitBtn *BAddColumn;
        TBitBtn *BDeleteColumn;

       void __fastcall MyCTableBoxOnChange(TObject *Sender);

    public:

    TListBox *LColumnBox;
    TListBox *LViewBox;
    TComboBox *CTableBox;
    TFont *font;

        __fastcall TMyGroupBox(TComponent *Owner);

Quote
};

In the combobox CTableBox are items to select. my aim is to add the selected
item to the listbox LViewBox.

void __fastcall TMyGroupBox::MyCTableBoxOnChange(TObject *Sender)
{
     if (static_cast<TComboBox*>(Sender)->ItemIndex == -1)
   {
      LViewBox->Clear();
      LViewBox->Items->Add("hallo");
   }

Quote
}

My problem is that i can only get access to the comboxox component, using
static_cast<TComboBox*>(Sender). So i am not able to get access to my
LViewBox component. My idea was to determine the parent component of the
combobox (this is TMyGroupBox). Over this component i might have full access
to al components of this class.

Could anybody help me to do that, or better, is there an other way to get
access to the LViewBox?

Philipp

 

Re:get parent component / access to other components of my class


Why don't you have access to your combobox?
It is in your class and you are accessing it from your class.
The only thing I see wrong is this:

     if (static_cast<TComboBox*>(Sender)->ItemIndex == -1)

change it to this:

     if (static_cast<TComboBox*>(Sender)->ItemIndex != -1)

you do not even need to use static_cast, change it to this:

     if( CTableBox->ItemIndex != -1 )

Todd

Re:get parent component / access to other components of my class


Hi,

I have already seen, that i might use if( CTableBox->ItemIndex != -1 ).

But know I ask me, in which cases I may use
if( CTableBox->ItemIndex != -1 )
and when to use this
if (static_cast<TComboBox*>(Sender)->ItemIndex == -1)

Could you tell please, when do I have to cast and when I can work without
casting.

As I tested have seen, that I do not need to cast, when I create classes,
which components, communicate each other. And when I create only new
objects, for example TButton, I have to cast.

void __fastcall TForm1::ButtonClick(TObject *Sender)
{
   ShowMessage(static_cast<TButton*>(Sender)->Caption);

Quote
}

Is that what I thought correct or, when do I have to cast?

Philipp

Quote
----- Original Message -----
From: "Todd Brylski" <tbryl...@new.rr.com>

Newsgroups: borland.public.cppbuilder.vcl.components.using
Sent: Saturday, May 17, 2003 8:45 PM
Subject: Re: get parent component / access to other components of my class

> Why don't you have access to your combobox?
> It is in your class and you are accessing it from your class.
> The only thing I see wrong is this:

>      if (static_cast<TComboBox*>(Sender)->ItemIndex == -1)

> change it to this:

>      if (static_cast<TComboBox*>(Sender)->ItemIndex != -1)

> you do not even need to use static_cast, change it to this:

>      if( CTableBox->ItemIndex != -1 )

> Todd

"Todd Brylski" <tbryl...@new.rr.com> schrieb im Newsbeitrag
news:3ec6845b@newsgroups.borland.com...
> Why don't you have access to your combobox?
> It is in your class and you are accessing it from your class.
> The only thing I see wrong is this:

>      if (static_cast<TComboBox*>(Sender)->ItemIndex == -1)

> change it to this:

>      if (static_cast<TComboBox*>(Sender)->ItemIndex != -1)

> you do not even need to use static_cast, change it to this:

>      if( CTableBox->ItemIndex != -1 )

> Todd

Re:get parent component / access to other components of my class


On Sat, 17 May 2003 21:17:53 +0200, "Philipp" <p...@yaemach.co1>
wrote:

        [snip]

Quote
>As I tested have seen, that I do not need to cast, when I create classes,
>which components, communicate each other. And when I create only new
>objects, for example TButton, I have to cast.

>void __fastcall TForm1::ButtonClick(TObject *Sender)
>{
>   ShowMessage(static_cast<TButton*>(Sender)->Caption);
>}

'Sender' (being a 'TObject') doesn't have a 'Caption', so saying
        Sender->Caption
wouldn't mean anything to the compiler.
But if you *know* that 'Sender' is a TButton, it's ok to treat
'Sender' as this type (that's what the cast does) - and then you can
get at its 'Caption'.
HTH
Helmut Giese

Re:get parent component / access to other components of my class


Quote
"Philipp" <p...@yaemach.co1> wrote:
> [...] Could you tell please, when do I have to cast and when
> I can work without casting.

Generally speaking, one would cast when the Sender might be unknown.

In the first example, no casting is needed because the event is
assigned to only one object (Button1):

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    ShowMessage( Button1->Caption );

Quote
}

//------------------------------------------------------------
In the next example the event could be assigned to multiple TButtons:

void __fastcall TForm1::ButtonClick(TObject *Sender)
{
    ShowMessage( static_cast<TButton*>(Sender)->Caption );

Quote
}

//------------------------------------------------------------
In this example the event could be assigned to both TButtons
and TSpeedButtons:

void __fastcall TForm1::ButtonClick(TObject *Sender)
{
    TButton *btn = static_cast<TButton*>(Sender);
    if( btn ) ShowMessage( btn->Caption );
    else
      {
      TSpeedButton *sbtn = static_cast<TSpeedButton*>(Sender);
      if( sbtn ) ShowMessage( sbtn->Caption );
      else ShowMessage( "Invalid Sender" );
      }

Quote
}

//-------------------------------------------------------------
This example shows how use casting to write a common function
for a StringGrids OnDrawCell event. There is no testing to
confirm that the Sender is a StringGrid because we know that it
is. The casting is performed only to determine who the sender
is so that we can work with the correct object:

void __fastcall TForm1::OnDrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
    TStringGrid* sGrid = static_cast<TStringGrid*>(Sender);
    TCanvas* pCanvas = sGrid->Canvas;

    if( State.Contains( gdFixed ) )
      {
          pCanvas->Brush->Color = sGrid->FixedColor;
          pCanvas->Font->Color  = sGrid->Font->Color;
      }
    else if( State.Contains( gdSelected ) )
      {
          pCanvas->Brush->Color  = clHighlight;
          pCanvas->Font->Color   = clHighlightText;
      }
    else
      {
          pCanvas->Brush->Color = sGrid->Color;
          pCanvas->Font->Color  = sGrid->Font->Color;
      }
    pCanvas->FillRect( Rect );
    DrawText(pCanvas->Handle, sGrid->Cells[ACol][ARow].c_str(), -1, &Rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER);

Quote
}

//-------------------------------------------------------------
In the final example, I have subclassed the default WndProc for
a group of SpeedButtons Parent (a ToolBar) so that I can
determine when the mouse has entered or left a particular
SpeedButton. I use this to make the SpeedButtons behave like
buttons in Windows Explorer. I have not shown the loading of
the bitmaps or the header definitions.

__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
    OldWinProc = ToolBar1->WindowProc;
    ToolBar1->WindowProc = NewTBProc;

Quote
}

//-----------------
__fastcall TForm1::~TForm1()
{
    ToolBar1->WindowProc = OldWinProc;
Quote
}

//-----------------
void __fastcall TForm1::NewTBProc( TMessage& Message )
{
    TControl* Ctrl = reinterpret_cast<TControl*>( Message.LParam );
    TSpeedButton *sBtn = dynamic_cast<TSpeedButton *>(Ctrl);
    if( sBtn )
      {
          if( Message.Msg == CM_MOUSEENTER )
            {
                Label1->Caption = "Mouse Enter";
                sBtn->Glyph->Assign( btnColor[ sBtn->Tag ] );
            }
          else if( Message.Msg == CM_MOUSELEAVE )
            {
                Label1->Caption = "Mouse Leave";
                sBtn->Glyph->Assign( btnTwoTone[ sBtn->Tag ] );
            }
      }
    OldWinProc( Message );

Quote
}
> And when I create only new objects, for example TButton, I
> have to cast.

No you don't. If you are assigning a common click event you
need to cast but if the new Button has it's own click event
then you could use the name of the new button without casting.

~ JD

Re:get parent component / access to other components of my class


Quote
"Philipp" <p...@yaemach.co1> wrote in message

news:3ec68aee$1@newsgroups.borland.com...

Quote
> Could you tell please, when do I have to cast
> and when I can work without casting.

You only need to cast the Sender when you don't otherwise have direct access
to the original pointer.  However, in this case, you do have direct access
because your OnChange event handler is a member of your TMyGroupBox
component, and both the ComboBox and ListBox pointers are also members of
the same class as the event handler.  Because they are all members within
the same class, they have direct access to each other without any additional
casting.

Quote
> when I create only new objects, for example TButton, I have to cast.

Only because you are probably not storing the original pointer assigned by
'new' in such a location (or at all) where the event handler can gain access
to it directly.  Thus Sender is your only available pointer to the button.

Gambit

Other Threads