Jump to content


Photo

Listbox Font doesn't seem to work for some of the properties

Listbox W3Listbox Font

  • Please log in to reply
3 replies to this topic

#1 ielite

ielite
  • Members
  • 704 posts

Posted 04 October 2016 - 02:31 AM

rocedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
 
  W3ListBox1.Add('One');
  W3ListBox1.Add('Two');
  W3ListBox1.Add('Three');
 
  W3ListBox1.Font.Name:= 'sans-serif'; //does not work
  W3ListBox1.Font.Size:= 8;            //does not work
  //how do you center text?
  W3ListBox1.Font.Weight:= 'Bold';
  W3ListBox1.Font.Color:= clRed;
 
  W3ListBox1.Styles.HighlightedColor:= clCyan;
  W3ListBox1.Styles.SelectedColor:= clYellow;
 
end;


#2 Nico Wouterse

Nico Wouterse
  • Moderators
  • 255 posts
  • LocationAustralia

Posted 04 October 2016 - 04:49 AM

The listbox component doesn't let you change all of the various font attributes directly

so, you could derive a new component based on tw3listbox yourself ...

 

One possible way of doing this is to move the GetTextChild method from protected to public, which would give you access to the TW3Label component which makes up the text in the listbox, and then manipulate the TW3Label.Container... etc to set font name, size, spacing etc

Or just add the behaviour / procedures you need in your new component

 

alternatively ...

a simpler way is to add a label or editbox to the listbox item yourself

See the 'ControlsListBox' project from the 'A Smart Book' demos

I presume you have the 'A Smart Book' publication from Primoz, www.smartprogrammer.org and all of the demo code ?

If not make sure you buy it, worth every penny

unit Form1;
 
interface
 
uses 
  System.Colors, SmartCL.System, SmartCL.Graphics, SmartCL.Components,
  SmartCL.Forms, SmartCL.Fonts, SmartCL.Borders, SmartCL.Application,
  SmartCL.Controls;
 
type
  TForm1 = class(TW3form)
  private
    {$I 'Form1:intf'}
  protected
    procedure InitializeObject; override;
    procedure FinalizeObject; override;
    procedure StyleTagObject; reintroduce; virtual;
    procedure Resize; override;
    procedure AddToListbox2;
    procedure DeleteFrom(listbox: TW3ListBox);
  end;
 
implementation
 
{ TForm1 }
 
procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
  W3listbox1.Styles.SelectedColor := clLime;
  W3listbox1.Styles.HighlightedColor := clAquamarine;
  W3listbox1.Styles.MovingColor := clBurlyWood;
  W3listbox1.AllowMoving := true;
  W3btnLB1Add.OnClick := lambda W3listbox1.Add(IntToStr(W3listbox1.Count)); end;
  W3btnLB1Delete.OnClick := lambda DeleteFrom(W3listbox1); end;
  W3btnLB2Add.OnClick := lambda AddToListbox2; end;
  W3btnLB2Delete.OnClick := lambda DeleteFrom(W3listbox2); end;
end;
 
procedure TForm1.AddToListbox2;
begin
  var item := W3listbox2.Items[W3listbox2.Add];
  var edit := TW3EditBox.Create(item);
  edit.Left := 8;
  edit.Top := 5;
  edit.Width := item.ClientWidth - 16;
  edit.Font.Name := "'Arial Black', arial, sans-serif";                <====================
end;
 
procedure TForm1.DeleteFrom(listbox: TW3ListBox);
begin
  if listbox.SelectedIndex >= 0 then
    listbox.Delete(listbox.SelectedIndex);
end;
 
procedure TForm1.FinalizeObject;
begin
  inherited;
end;
 
procedure TForm1.Resize;
begin
  inherited;
end;
 
procedure TForm1.StyleTagObject;
begin
  // Custom styling
end;
 
end.

The label or editbox component then lets you control font, size etc directly


Nico Wouterse

#3 Nico Wouterse

Nico Wouterse
  • Moderators
  • 255 posts
  • LocationAustralia

Posted 05 October 2016 - 11:16 AM

Code for 2 listboxes, the first one based on a new component, the second one a normal TW3Listbox with a modified formbased GetTextChild method

unit Form1;
 
interface
 
uses 
  System.Colors, SmartCL.System, SmartCL.Graphics, SmartCL.Components,
  SmartCL.Forms, SmartCL.Fonts, SmartCL.Borders, SmartCL.Application,
  SmartCL.Controls, MyListBox;
 
type
  TForm1 = class(TW3form)
  private
    {$I 'Form1:intf'}
  protected
    procedure InitializeObject; override;
    procedure FinalizeObject; override;
    procedure StyleTagObject; reintroduce; virtual;
    procedure Resize; override;
    procedure AddToListbox2;
    procedure DeleteFrom(listbox: TW3ListBox);
    MyListBox1 : TMyListBox;
    MyListBox2 : TW3ListBox;
    function GetTextChild(lb: TW3ListBox; idx: integer): TW3Label;
  end;
 
implementation
 
{ TForm1 }
 
procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
  MyListBox1 := TMyListBox.Create(self);
  MyListBox1.SetBounds(8,8,304,176);
  MyListBox1.Styles.SelectedColor := clLime;
  MyListBox1.Styles.HighlightedColor := clAquamarine;
  MyListBox1.Styles.MovingColor := clBurlyWood;
  MyListBox1.AllowMoving := true;
  W3btnLB1Add.OnClick := procedure(sender: TObject)
  begin
    MyListBox1.Add('- ' + IntToStr(MyListBox1.Count) + ' -');
    MyListBox1.GetTextChild(MyListBox1.Count-1).Container.Font.Name :=
      "'Arial Black', arial, sans-serif";
    MyListBox1.GetTextChild(MyListBox1.Count-1).AlignText := taCenter;
    MyListBox1.GetTextChild(MyListBox1.Count-1).Container.Font.Color := clBlue;
  end;
  W3btnLB1Delete.OnClick := lambda DeleteFrom(MyListBox1); end;
  W3btnLB2Add.OnClick := lambda AddToListbox2; end;
  W3btnLB2Delete.OnClick := lambda DeleteFrom(MyListBox2); end;
//
  MyListBox2 := TW3ListBox.Create(self);
  MyListBox2.SetBounds(8,240,304,176);
end;
 
procedure TForm1.AddToListbox2;
begin
  var item := Mylistbox2.Items[Mylistbox2.Add];
  var lbl := TW3Label.Create(item);
  lbl.SetBounds(8, 0, item.ClientWidth - 16, item.ClientHeight);
  lbl.Container.Font.Name := 'Arial Black';
  lbl.Caption := '- ' + IntToStr(MyListBox2.Count) + ' -';
  GetTextChild(MyListBox2,MyListBox2.Count-1).Container.Font.Color := clRed;
end;
 
procedure TForm1.DeleteFrom(listbox: TW3ListBox);
begin
  if listbox.SelectedIndex >= 0 then
    listbox.Delete(listbox.SelectedIndex);
end;
 
function TForm1.GetTextChild(lb: TW3ListBox; idx: integer): TW3Label;
begin
  Result := nil;
  var item := lb.Items[idx];
  for var iChild := 0 to item.GetChildCount - 1 do begin
    var obj := item.GetChildObject(iChild);
    if Assigned(obj) and (obj is TW3Label) then
      Exit(TW3Label(obj));
  end;
end;
 
procedure TForm1.FinalizeObject;
begin
  inherited;
end;
 
procedure TForm1.Resize;
begin
  inherited;
end;
 
procedure TForm1.StyleTagObject;
begin
  // Custom styling
end;
 
end.

and the new component unit :

unit MyListBox;
 
interface
 
uses 
  SmartCL.System, SmartCL.Controls.ListBox, SmartCL.Controls.Label;
 
type
 
  TMyListBox = class(TW3ListBox)
  public
    function GetTextChild(idx: integer): TW3Label;
  end;
 
implementation
 
function TMyListBox.GetTextChild(idx: integer): TW3Label;
begin
  Result := nil;
  var item := Items[idx];
  for var iChild := 0 to item.GetChildCount - 1 do begin
    var obj := item.GetChildObject(iChild);
    if Assigned(obj) and (obj is TW3Label) then
      Exit(TW3Label(obj));
  end;
end;
 
end.

Nico Wouterse

#4 ielite

ielite
  • Members
  • 704 posts

Posted 06 October 2016 - 02:19 AM

Hinico,

 

Thanks! I will play with and try some of - your suggestions here.

 

Its interesting to see how many different ways things can be done.

 

Thanks again







Also tagged with one or more of these keywords: Listbox, W3Listbox, Font

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users