• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

Need help with custom component.

I'm having trouble with a custom listbox component(does not descend form tlistbox or tcustomlistbox), basically I want text normal when not selected and bold when the item is selected. However it is always bold for some reason.

I believe the problem lies in the component source as opposed to my code below but here it is anyway.

Basically umm in the OnDrawItem event I do this:

Procedure TFrmMain.MP3_ListDrawItem(Sender: TObject; ACanvas: TCanvas; Index: SmallInt; Const ARect: TRect; Selected: Boolean);
Begin
 If (Selected) Then ACanvas.Font.Style := [fsBold]
 Else ACanvas.Font.Style := [];
    ACanvas.Brush.Style := bsClear;
    ACanvas.TextOut(ARect.Left,ARect.Top,TCListBox(Sender).Items[Index]);
End;

But the text is always bold whether selected or not. Here isa link to the source of the component if anyone wants to check it out themselves and see what may be wrong. I must be missing something but all looks fine to me.

http://chessoscorner.com/files/CListBox.zip
0
Chesso
Asked:
Chesso
1 Solution
 
Pierre CorneliusCommented:
I tested and it worked fine.

Procedure TFrmMain.MP3_ListDrawItem(Sender: TObject; ACanvas: TCanvas; Index: SmallInt; Const ARect: TRect; Selected: Boolean);
Begin
 If (Selected) Then ACanvas.Font.Style := [fsBold]
 Else ACanvas.Font.Style := [];
    ACanvas.Brush.Style := bsClear;
//you don't need this line
//    ACanvas.TextOut(ARect.Left,ARect.Top,TCListBox(Sender).Items[Index]);
End;

You probably forgot to assign the event to the component, e.g.

  CListBox1.OnDrawItem:= MP3_ListDrawItem;

regards
Pierre
0
 
ChessoAuthor Commented:
Nope the event was assigned but removing the textout and placing the setting of bsClear in only the else statement fixes it up.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now