
Technical Information Database
TI531D.txt - Enabling a horizontal scroll bar for a list box.
Category :General Programming
Platform :All Windows
Product :All32Bit,
Description:
To enable horizontal scroll bars in list boxes you have send the listbox a LB_SETHORIZONTALEXTENT message. The sample
code illustrates how to do this in the OnCreate of a form. The form simply has one listbox on it and a button. Clicking on
the button removes the horizontal scroll bar.
-- Source code
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var
LongestLength: Integer;
LongestString: String;
PixelLength: Integer;
i: Integer;
begin
Listbox1.Items.Add('Line 1');
Listbox1.Items.Add('Line 2 A very long string that is past the size of the listbox');
Listbox1.Items.Add('Line 3');
if Listbox1.Items.Count > 1 then
begin
LongestLength := 0;
LongestString := '';
// go through all the items to find the longest one
for i := 0 to Listbox1.Items.Count - 1 do
begin
if Length(Listbox1.Items[i]) > LongestLength then
begin
LongestString := Listbox1.Items[i];
LongestLength := Length(Listbox1.Items[i]);
end;
end;
// Now convert the longest string length to pixel size
PixelLength := Listbox1.Canvas.TextWidth(LongestString);
// Add a little extra to it to make it look nice
PixelLength := PixelLength + Listbox1.Canvas.TextWidth('W');
// Send an LB_SETHORIZONTALEXTENT to the list box to apply
// the horizontal scrollbar
SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT,
PixelLength, 0);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
{ This hides the listbox scrollbar by sending a
string length of 0 message }
begin
// Send a lb_SetHorizontalExtent to the list box with
// 0 as the length of the string
SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT,
0, 0);
end;
end.
Reference:
3/30/99 12:23:29 PM
|