Board index » cppbuilder » Basic Help with pointers to VCL objects?

Basic Help with pointers to VCL objects?

I have 4 TMemo(s) on a form, named A1, A2, A3 & A4 respectively. They
display possible answers to amultiple choice question. These Memos set on a
TPanel.

Under certain situations I wish to have the TMemo which contains the correct
answer to be colored clLime and not ClWindow. There are several of these
situations, and  I use this code to set the color. It takes an integer and
returns a ptr of the TMemo type. Example: If I pass it a 1 it should color
A1 clLime.

///-------------------------------------------------------------------------
-------------
TMemo* TForm1::ColorCorrectMemo(int correct)
{
TMemo *right;
switch (correct){
case 1: { right = A1; break; }
case 2: { right = A2; break; }
case 3: { right = A3; break; }
case 4: { right = A4; }

Quote
}

correct->Color = clLime;
return right;
Quote
}

///-------------------------------------------------------------------------
-------------

This works fine when I call it like this: TMemo *right_ans =
ColorCorrectMemo(correct);

*** UNLESS *** (and here's the trouble - this is  the only place where the
code fails).

I have a TButton, which sits on a TTabSheet which in turn sits on a
TPageControl.
If I call the above function from the Button's OnClick, it colors the
TabSheet clLime, and has no apparent effect on the Memos. This only happens
when the call is manually made from this button. Elsewhere, when the call is
made from within the bosy of the app it does fine, and gives the desired
results.

I've only recently (like yesterday) started working with pointers. Any idea
where I'm fouling up?

Thanks,

Jerry

 

Re:Basic Help with pointers to VCL objects?


Quote
"Jerry Dunn" <dd...@earthlink.net> wrote in message news:3b2bb732_1@dnews...
> correct->Color = clLime;

That should be right->Color = clLime;

Gambit

Re:Basic Help with pointers to VCL objects?


Yep, just a mistake when I copied the text into the message to be posted.
That's not the trouble though...

Quote
Remy Lebeau <gambi...@gte.net> wrote in message news:3b2bbcca$2_1@dnews...

> "Jerry Dunn" <dd...@earthlink.net> wrote in message

news:3b2bb732_1@dnews...
Quote
> > correct->Color = clLime;

> That should be right->Color = clLime;

> Gambit

Re:Basic Help with pointers to VCL objects?


I would have opted to use FindComponent() instead of a switch, ie:

(Assuming you named your TMemos as "A1", "A2", "A3", and "A4")

TMemo *memo = Form1->FindComponent("A"+IntToStr(correct));
if(memo) memo->Color = clLime;
return memo;

Gambit

Quote
"Jerry Dunn" <dd...@earthlink.net> wrote in message news:3b2bc5e0_2@dnews...
> Yep, just a mistake when I copied the text into the message to be posted.
> That's not the trouble though...

Re:Basic Help with pointers to VCL objects?


Perhaps... but I'm after the reason that the function fails while called
from the button and not from anywhere else. Calling it from the button (on a
tabsheet within a pagecontrol) causes it to change the color of another
component (the tabsheet) other than what I'm specifying a pointer to (a
memo).

Without knowing what going on I can't avoid the same mistake in the
future....thanks, though.

Quote
Remy Lebeau <gambi...@gte.net> wrote in message news:3b2bd11f$1_2@dnews...
> I would have opted to use FindComponent() instead of a switch, ie:

> (Assuming you named your TMemos as "A1", "A2", "A3", and "A4")

> TMemo *memo = Form1->FindComponent("A"+IntToStr(correct));
> if(memo) memo->Color = clLime;
> return memo;

> Gambit

> "Jerry Dunn" <dd...@earthlink.net> wrote in message

news:3b2bc5e0_2@dnews...
Quote
> > Yep, just a mistake when I copied the text into the message to be
posted.
> > That's not the trouble though...

Re:Basic Help with pointers to VCL objects?


The button OnClick handler is called in context of the FORM that contains
the button, not the PageControl or TabSheet.  You need to supply more code
in ordfer for us to track your peoblem down.  Because obviously, you're
assigning your pointer to the wrong object before setting it's color, that's
why something other than the correct memo is changing color.

Gambit

Quote
"Jerry Dunn" <dd...@earthlink.net> wrote in message news:3b2bd737_1@dnews...
> Perhaps... but I'm after the reason that the function fails while called
> from the button and not from anywhere else. Calling it from the button (on
a
> tabsheet within a pagecontrol) causes it to change the color of another
> component (the tabsheet) other than what I'm specifying a pointer to (a
> memo).

Re:Basic Help with pointers to VCL objects?


I did a bit of experimenting and learned a bit...maybe this will help you
help me. Thanks for the patience, again, I'm new at this and struggling....

If I call the ColorCorrectMemo() function from a button placed anywhere on
the form (seems that the TabSheet that it sits on has nothing to do with the
trouble) I get the problem. Rather than the proper Memo changing color, the
TabSheet changes color. Unsure as to why it's picking on the TabSheet, since
nothing I can see references that.

I've found somewhat of a fix. I can move the call to ColorCorrectMemo() to
another one of my own functions ( "MyFunction()" ), then call MyFunction()
from a button's OnClick and it works fine. Code is below.

Again, here's the function that changes the color. It takes an integer from
1 to 4 and decides which Memo to change the color of.
////--------------------------------------------------
TMemo* TForm1::ColorCorrectMemo(int correct)
{
TMemo *edit;
switch (correct){
case 1: { edit = A1; break; }
case 2: { edit = A2; break; }
case 3: { edit = A3; break; }
case 4: { edit = A4; }

Quote
}

edit->Color = clLime;
return edit;
Quote
}

////--------------------------------------------------

And here's how I called it (previously) from a button's OnClick. This brings
about the problem....no Memo changes color, just the TabSheet.
////--------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int correct = 3;
TMemo *right_ans;
right_ans = Form1->ColorCorrectMemo(correct);

Quote
}

////--------------------------------------------------

But, if I put the call in my own function

////--------------------------------------------------
void TForm1::MyFunction()
{
int correct = 3;
TMemo *right_ans;
right_ans = Form1->ColorCorrectMemo(correct);

Quote
}

////--------------------------------------------------

and call that from a button it will work

////--------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
MyFunction();

Quote
}

////--------------------------------------------------

Does this make the trouble clearer? Again, thanks.

Quote
Remy Lebeau <gambi...@gte.net> wrote in message news:3b2befcb_2@dnews...
> The button OnClick handler is called in context of the FORM that contains
> the button, not the PageControl or TabSheet.  You need to supply more code
> in ordfer for us to track your peoblem down.  Because obviously, you're
> assigning your pointer to the wrong object before setting it's color,
that's
> why something other than the correct memo is changing color.

> Gambit

> "Jerry Dunn" <dd...@earthlink.net> wrote in message

news:3b2bd737_1@dnews...
Quote
> > Perhaps... but I'm after the reason that the function fails while called
> > from the button and not from anywhere else. Calling it from the button
(on
> a
> > tabsheet within a pagecontrol) causes it to change the color of another
> > component (the tabsheet) other than what I'm specifying a pointer to (a
> > memo).

Re:Basic Help with pointers to VCL objects?


Quote
> I've found somewhat of a fix. I can move the call to ColorCorrectMemo() to
> another one of my own functions ( "MyFunction()" ), then call MyFunction()
> from a button's OnClick and it works fine. Code is below.

No, something is wrong in your description, because:

1] This code:

Quote
> TMemo *edit;
> switch (correct){
> case 1: { edit = A1; break; }
> case 2: { edit = A2; break; }
> case 3: { edit = A3; break; }
> case 4: { edit = A4; }
> }
> edit->Color = clLime;

Sets the color of a TMemo. I don't see how it could set the color of a TabSheet,
no matter where it is called from. There is a Property called ParentColor you
might take a look at and make sure it isn't true.

Quote
> But, if I put the call in my own function

> ////--------------------------------------------------
> void TForm1::MyFunction()
> {
> int correct = 3;
> TMemo *right_ans;
> right_ans = Form1->ColorCorrectMemo(correct);
> }
> ////--------------------------------------------------

> and call that from a button it will work

> ////--------------------------------------------------
> void __fastcall TForm1::Button1Click(TObject *Sender)
> {
> MyFunction();
> }
> ////--------------------------------------------------

There's no difference between these two ways of calling things, again if you
are giving us all the info. Calling it directly via the button handler or
indirectly via a function called from the button handler is the same thing.
And you don't need the "Form1->" in the MyFunction, as it is a member function
of the same class, TForm1.

This code works just fine for me, no matter where the TMemo is located:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    ColorCorrect(1);

Quote
}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    ColorCorrect(2);

Quote
}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
    ColorCorrect(3);

Quote
}

//---------------------------------------------------------------------------
void TForm1::ColorCorrect( int ans )
{
    TMemo* memo;

    switch( ans )
    {
    case 1:
        memo=Memo1;
        break;
    case 2:
        memo=Memo2;
        break;
    case 3:
        memo=Memo3;
        break;
    }

    memo->Color = clLime;

Quote
}

+=====================================================+
| Jonathan Arnold (mailto:jdarn...@buddydog.org)      |
|         Comprehensive C++Builder link site:         |
| http://www.buddydog.org/C++Builder/c++builder.html  |
+=====================================================+

Other Threads