 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Db Guest
|
Posted: Sun Feb 19, 2006 7:03 pm Post subject: TListBox - Controling Selection Color |
|
|
When A list box item is selected and focused I want it to be in a
different color than when it is just selected. I used the owner draw
style to accomplish this and it worked great at 800x600 screen size.
When my resizing object came into play though it didn't work properly.
The wrong Rect size was passed to the OnDrawItem method. I was able
to partially deal with that but then when you clicked on an item the wrong
one
would be highlighted and focus rects are being drawn in the wrong place and
in
the wrong size.
Is there a way to easily control the colors that a list box selection is
draw in?
Db |
|
| Back to top |
|
 |
Db Guest
|
Posted: Sun Feb 19, 2006 7:03 pm Post subject: Re: TListBox - Controling Selection Color |
|
|
Thanks but never mind. Altering my sizing object to set the ItemHeight
property to the scaled height did the trick.
Db
"Db" <drbsname_at_aol.com> wrote in message
news:43f8b644$1 (AT) newsgroups (DOT) borland.com...
| Quote: | When A list box item is selected and focused I want it to be in a
different color than when it is just selected. I used the owner draw
style to accomplish this and it worked great at 800x600 screen size.
When my resizing object came into play though it didn't work properly.
The wrong Rect size was passed to the OnDrawItem method. I was able
to partially deal with that but then when you clicked on an item the wrong
one
would be highlighted and focus rects are being drawn in the wrong place
and in
the wrong size.
Is there a way to easily control the colors that a list box selection is
draw in?
Db
|
|
|
| Back to top |
|
 |
HF Guest
|
Posted: Sun Feb 19, 2006 8:03 pm Post subject: Re: TListBox - Controling Selection Color |
|
|
Set the Style of ListBox to Owner-draw
the following example may help you
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
TListBox* lb = reinterpret_cast<TListBox*>(Control);
TBrush* bb = lb->Canvas->Brush;
TFont* tmpFont = lb->Canvas->Font;
if (State.Contains(odSelected)){
lb->Canvas->Brush->Color = clYellow;
lb->Canvas->FillRect(Rect);
lb->Canvas->Font->Color =clBlue;
lb->Canvas->TextOut(Rect.Left,Rect.Top,lb->Items->Strings[Index]);
}
else{
lb->Canvas->Brush = bb;
lb->Canvas->FillRect(Rect);
lb->Canvas->Font = tmpFont;
lb->Canvas->TextOut(Rect.Left,Rect.Top,lb->Items->Strings[Index]);
}
}
//---------------------------------------------------------------------------
-minas
Ο "Db" <drbsname_at_aol.com> έγραψε στο μήνυμα
news:43f8b644$1 (AT) newsgroups (DOT) borland.com...
| Quote: | When A list box item is selected and focused I want it to be in a
different color than when it is just selected. I used the owner draw
style to accomplish this and it worked great at 800x600 screen size.
When my resizing object came into play though it didn't work properly.
The wrong Rect size was passed to the OnDrawItem method. I was able
to partially deal with that but then when you clicked on an item the wrong
one
would be highlighted and focus rects are being drawn in the wrong place
and in
the wrong size.
Is there a way to easily control the colors that a list box selection is
draw in?
Db
|
|
|
| Back to top |
|
 |
HF Guest
|
Posted: Sun Feb 19, 2006 8:03 pm Post subject: Re: TListBox - Controling Selection Color |
|
|
Ah,sorry dynamic_cast<TListBox*>(Control).
-minas |
|
| Back to top |
|
 |
JD Guest
|
Posted: Mon Feb 20, 2006 2:12 am Post subject: Re: TListBox - Controling Selection Color |
|
|
"HF" <(min_charATyahooPUTADOTgr)(HellenicFire)> wrote:
| Quote: |
Ah,sorry dynamic_cast<TListBox*>(Control).
|
That is incorrect as well. While both reinterpret_cast and
dynamic_cast will work as long as the event is always assigned
to a TListBox, it is also true that static_cast will work as
well and static_cast is the correct cast to use in this case.
The reason is that reinterpret_cast has not type checking at
all and dynamic_cast performs run-time checking while static_cast
performs compile-time checking.
The only reason to use dynamic_cast would be if the event was
assigned to multiple type objects and in that case, one should
be checking the result of dynamic_cast because it can fail and
return NULL.
~ JD |
|
| Back to top |
|
 |
Michel Leunen Guest
|
Posted: Mon Feb 20, 2006 9:03 pm Post subject: Re: TListBox - Controling Selection Color |
|
|
JD wrote:
| Quote: | Ah,sorry dynamic_cast<TListBox*>(Control).
That is incorrect as well.
|
If a static_cast is the prefered way to go, using dynamic_cast is
certainly *NOT* incorrect. As you stated, it is used when you are not
sure that it will be always a TListBox.
Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
---------------------------------------- |
|
| Back to top |
|
 |
Rudy Velthuis [TeamB] Guest
|
Posted: Mon Feb 20, 2006 11:03 pm Post subject: Re: TListBox - Controling Selection Color |
|
|
At 01:54:57, 20.02.2006, JD wrote:
| Quote: | The reason is that reinterpret_cast has not type checking at
all and dynamic_cast performs run-time checking while static_cast
performs compile-time checking.
|
Don't you think this would be more maintainable and reusable if
dynamic_cast is used? If one day, this handler is assigned (perhaps
inadvertently) to something that is not a TListBox, the error should at
least be caught at runtime, instead of producing erroneous results.
Note that at compile time of the handler, there is no way to ensure that
only a TListBox* is passed. I think that in such cases, static_cast is
simply wrong.
IOW, IMO, if there is the slightest possibility that (someday) something
else than a TListBox* is passed (and that posibility exists, since the
formal argument is after all a TControl*), one should use dynamic_cast.
--
Rudy Velthuis [TeamB] http://rvelthuis.de/
"Defining and analyzing humor is a pastime of humorless people."
-- Robert Benchley (1889 - 1945) |
|
| Back to top |
|
 |
JD Guest
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Tue Feb 21, 2006 2:03 am Post subject: Re: TListBox - Controling Selection Color |
|
|
Michel Leunen <nospam (AT) noreply (DOT) please> wrote:
| Quote: | JD wrote:
Ah,sorry dynamic_cast<TListBox*>(Control).
That is incorrect as well.
If a static_cast is the prefered way to go, using dynamic_cast is
certainly *NOT* incorrect. As you stated, it is used when you are not
sure that it will be always a TListBox.
|
C'mon. That's a poor argument because events don't get
assigned on their own. One knows when one assigns events
so one is always 'sure' of what it's been assigned to.
Besides, even if dynamic_cast is purposefully used, it's still
incorrect. Using your argument, that the programmer is not
'sure', as I also stated, dynamic_cast can fail and the
result of the cast *must* be checked which the OP did not do.
~ JD |
|
| Back to top |
|
 |
Rudy Velthuis [TeamB] Guest
|
Posted: Tue Feb 21, 2006 11:03 am Post subject: Re: TListBox - Controling Selection Color |
|
|
At 01:02:39, 21.02.2006, JD wrote:
"He does it too"? <g>
--
Rudy Velthuis [TeamB] http://rvelthuis.de/
"Deliver yesterday, code today, think tomorrow." -- unknown |
|
| Back to top |
|
 |
JD Guest
|
Posted: Tue Feb 21, 2006 8:03 pm Post subject: Re: TListBox - Controling Selection Color |
|
|
"Rudy Velthuis [TeamB]" <velthuis (AT) gmail (DOT) com> wrote:
| Quote: |
"He does it too"? <g
|
Yes, and I strongly agree with him on this point.
If we consider your reasoning for using it, then one can use
the same argument against using it. If, at some future date
the event is assigned to a different type object and the use
of dynamic_cast was correctly coded as such:
TListBox *pBox = dynamic_cast<TListBox*>( Sender );
if( pBox )
{
//
}
the net result is that the programmer is left with no clues at
all as to why the event isn't doing anything. To fix this,
one would need to add an else block that displayed some kind
of message or threw something and IMO, this is excessive
because of all of the events that one encounters in a normal
VCL application.
OTOH, if one sticks with static_cast, you're gonna get an
exception thrown your way which should alert you immediately
that there is a problem and point you to it as well.
~ JD |
|
| Back to top |
|
 |
Rudy Velthuis [TeamB] Guest
|
Posted: Tue Feb 21, 2006 9:03 pm Post subject: Re: TListBox - Controling Selection Color |
|
|
At 19:11:10, 21.02.2006, JD wrote:
| Quote: |
"Rudy Velthuis [TeamB]" <velthuis (AT) gmail (DOT) com> wrote:
"He does it too"? <g
Yes, and I strongly agree with him on this point.
|
I don't. IMO, this is unsafe design.
--
Rudy Velthuis [TeamB] http://rvelthuis.de/
"Shoot, you should've told me about your humorectomy; I'd've
sent flowers."
-- Ignacio Vazquez in borland.public-off-topic |
|
| Back to top |
|
 |
Michel Leunen Guest
|
Posted: Tue Feb 21, 2006 9:03 pm Post subject: Re: TListBox - Controling Selection Color |
|
|
JD wrote:
| Quote: | C'mon. That's a poor argument because events don't get
assigned on their own. One knows when one assigns events
so one is always 'sure' of what it's been assigned to.
|
That's not defensive programming. The writer and the user are not always
the same person. But you're right in a sense and that's not my point. I
just said that using dynamic_cast is not incorrect. If you want to avoid
the overhead of using dynamic_cast then use static_cast. Ok. But in this
case dynamic_cast is not wrong.
But...
| Quote: |
Besides, even if dynamic_cast is purposefully used, it's still
incorrect. Using your argument, that the programmer is not
'sure', as I also stated, dynamic_cast can fail and the
result of the cast *must* be checked which the OP did not do.
|
....yes, the result of the cast must be checked. Of course.
Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
---------------------------------------- |
|
| Back to top |
|
 |
Rudy Velthuis [TeamB] Guest
|
Posted: Tue Feb 21, 2006 10:03 pm Post subject: Re: TListBox - Controling Selection Color |
|
|
At 19:11:10, 21.02.2006, JD wrote:
| Quote: | OTOH, if one sticks with static_cast, you're gonna get an
exception thrown your way which should alert you immediately
that there is a problem and point you to it as well.
|
You get an exception? I didn't know static_cast did runtime checks?
Or did you mean a compiler error? At the time of compilation, the event
handler can not know what kind of class will be passed. static_cast can
not diagnose anything at compile time (since a TListBox is a TControl).
--
Rudy Velthuis [TeamB] http://rvelthuis.de/
"I just bought a Mac to help me design the next Cray."
-- Seymoure Cray (1925-1996) when was informed that Apple Inc. had
recently bought a Cray supercomputer to help them design the next Mac. |
|
| Back to top |
|
 |
Rudy Velthuis [TeamB] Guest
|
Posted: Tue Feb 21, 2006 11:03 pm Post subject: Re: TListBox - Controling Selection Color |
|
|
At 22:08:34, 21.02.2006, JD wrote:
| Quote: | No. What I was refering to was your example of assigning the
event to a different type object. The moment that the resulting
pointer is used (when Control is static_cast to the wrong
type), it's going to throw.
|
Control is a TWinControl. And TComboBox and TOutline have the same event,
of the same type TDrawItemEvent, so the same handler could be legally
assigned to a TComboBox or a TOutline. No static cast could ever catch
this.
A dynamic cast would, however, at runtime.
--
Rudy Velthuis [TeamB] http://rvelthuis.de/
"Diplomacy is the art of saying 'Nice doggie!'... 'til you can find a
rock." -- bumper sticker |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|