Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Display tooltips for dropped-down TComboBox items Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
29-Jul-04
Category
VCL-General
Language
Delphi 3.x
Views
138
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Peter Below

Is it possible to display tooltips for a combobox in dropped down state if the 
items are wider than the combo box? Or better: is there a way to (auto) adjust the 
size of the drop down listbox to ensure all items fit?

Answer:
1   
2   function CalcMaxWidthOfStrings(Strings: TStrings; Font: TFont): Integer;
3   var
4     I: Integer;
5     Canvas: TCanvas;
6   begin
7     Assert(Assigned(Strings));
8     Assert(Assigned(Font));
9     Canvas := TCanvas.Create;
10    try
11      Canvas.Handle := GetDC(0);
12      try
13        Canvas.Font := Font;
14        Result := 0;
15        for I := 0 to Strings.Count - 1 do
16          Result := Max2Int(Result, Canvas.TextWidth(Strings[I]));
17      finally
18        ReleaseDC(0, Canvas.Handle);
19        Canvas.Handle := 0;
20      end;
21    finally
22      Canvas.free;
23    end;
24  end;
25  
26  procedure AutoSizeComboDropDown(Combo: TComboBox; Offset: Integer = 0);
27  var
28    Max, Cx: Integer;
29  begin
30    Cx := GetSystemMetrics(SM_CXVSCROLL) * Ord(Combo.Items.count > 
31  Combo.DropDownCount);
32    {Adding 6 here rather than in the next statement assures there's enough space 
33    for the left and right margin and borders, too. I also added an offset that 
34    allows me to use the same function in custom draw combos for example to 
35    display an icon in the left margin.}
36    Max := CalcMaxWidthOfStrings(Combo.Items, Combo.Font) + 6 + Offset;
37    if Max > (Combo.ClientWidth - Cx) then
38      Combo.Perform(CB_SETDROPPEDWIDTH, Max + Cx, 0)
39    else
40      Combo.Perform(CB_SETDROPPEDWIDTH, Combo.Width, 0);
41  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC