Recent

Author Topic: How to have a combo box that expands to show all of the selected text width wise  (Read 1735 times)

formslash

  • Newbie
  • Posts: 1
How to have a combo box that expands to show all of the selected text width wise?

Let us say the combo box only shows

"Now is the time" because it is not wide enough

How do you get it to show the full text of the line when the mouse is over the index or line is selected?

Yes I know I could make it wider on the form, but I do not want to do that as some of the lines are pretty long.

wp

  • Hero Member
  • *****
  • Posts: 11922
https://forum.lazarus.freepascal.org/index.php/topic,39420.msg270512.html#msg270512

Why is it that the title of your post is exactly the same as the one which gives you the answer? Are you a troll?


ASerge

  • Hero Member
  • *****
  • Posts: 2246
How do you get it to show the full text of the line when the mouse is over the index or line is selected?
Yes I know I could make it wider on the form, but I do not want to do that as some of the lines are pretty long.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, StdCtrls, Types;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     ComboBox1: TComboBox;
  13.     procedure ComboBox1CloseUp(Sender: TObject);
  14.     procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  15.       ARect: TRect; State: TOwnerDrawState);
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.     FHintWindow: THintWindow;
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. uses LclType;
  27.  
  28. {$R *.lfm}
  29.  
  30. procedure TForm1.ComboBox1CloseUp(Sender: TObject);
  31. begin
  32.   FHintWindow.ReleaseHandle;
  33. end;
  34.  
  35. procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  36.   ARect: TRect; State: TOwnerDrawState);
  37. var
  38.   LCanvas: TCanvas;
  39.   LText: string;
  40.   LStyle: TTextStyle;
  41. begin
  42.   LCanvas := (Control as TComboBox).Canvas;
  43.   LText := TComboBox(Control).Items[Index];
  44.   LStyle := LCanvas.TextStyle;
  45.   LStyle.Wordbreak := False;
  46.   LStyle.EndEllipsis := True;
  47.   LStyle.Opaque := True;
  48.   LCanvas.TextRect(ARect, ARect.Left + 2, ARect.Top, LText, LStyle);
  49.   if odSelected in State then
  50.     if LCanvas.TextWidth(LText) >= (ARect.Width - 2) then
  51.     begin
  52.       FHintWindow.HintRect := FHintWindow.CalcHintRect(ClientWidth - ARect.Left, LText, nil);
  53.       FHintWindow.OffsetHintRect(Mouse.CursorPos);
  54.       FHintWindow.ActivateHint(LText);
  55.     end
  56.     else
  57.       FHintWindow.ReleaseHandle;
  58. end;
  59.  
  60. procedure TForm1.FormCreate(Sender: TObject);
  61. begin
  62.   ComboBox1.Style := csOwnerDrawFixed;
  63.   FHintWindow := HintWindowClass.Create(Self);
  64.   FHintWindow.AutoHide := True;
  65. end;
  66.  
  67. end.

 

TinyPortal © 2005-2018