Jump to content

Recommended Posts

I needed a component that made it easier to place a lot of labels and TEdit controls on a form. So, I wrote this simple version of a TLabelEdit component
 
it has the following public properties:
 
 property Label: TW3Label read fLabel;
 property Edit:  TW3EditBox read fEdit;
 property Spacing: Integer read fSpacing write fSpacing;
 property Margins: Integer read fMargins write fMargins;
Spacing - space between the Label and edit control only
 
Margins - space on outside boundary of both the label and edit controls (i.e. space on left, right of both Label and Edit, as well as top of label and bottom of edit).
 
example run-time creation code:
 
 fLabelEdit:= TLabelEdit.Create(self);
 fLabelEdit.Handle.style.setProperty('background-color', 'white');
 fLabelEdit.Label.Handle.style.setProperty('background-color', 'Red');
 fLabelEdit.Edit.Handle.style.setProperty('color', 'red');
 fLabelEdit.Left:= 10;
 fLabelEdit.Top:= 10;
 fLabelEdit.Width:= 128;
 fLabelEdit.Height:= 64;
 fLabelEdit.Margins:= 2;
 fLabelEdit.Spacing:= 2;

Full unit code below. Let me know if you see anything that can be improved.

 

Shane

 

 
unit LabelEdit;


interface


uses 
  SmartCL.System, System.Colors, SmartCL.Components, SmartCL.Controls;


type


 TLabelEdit = class(TW3CustomControl)
 private
  fLabel: TW3Label;
  fEdit: TW3EditBox;
  fSpacing: Integer;
  fMargins: Integer;
 protected
  procedure Resize; override;
  procedure InitializeObject; override;
  procedure FinalizeObject; override;
 public
  property Label: TW3Label read fLabel;
  property Edit:  TW3EditBox read fEdit;
  property Spacing: Integer read fSpacing write fSpacing;
  property Margins: Integer read fMargins write fMargins;
 end;




implementation




procedure TLabelEdit.InitializeObject;
begin
  inherited;
  Height:= 64;
  Width:= 128;
  Spacing:= 0;
  Margins:= 0;
  FLabel := TW3Label.Create(Self);
  FLabel.Font.Name := 'verdana';
  FLabel.Font.Color := RGBToColor($FF, $FF, $FF);
  FLabel.Font.Weight := 'bold';
  FLabel.Font.Size := 18;
  FLabel.Caption := ClassName;
  FLabel.AlignText := taLeft;
  FEdit := TW3EditBox.Create(Self);
end;


procedure TLabelEdit.FinalizeObject;
begin
  FLabel.Free;
  FEdit.Free;
  inherited;
end;


procedure TLabelEdit.Resize;
begin
 inherited;
  fLabel.SetBounds(fMargins, fMargins, clientWidth-(fMargins * 2), (clientHeight Div 2)-(fMargins * 2));
  fEdit.SetBounds(fMargins, (fLabel.Top + fLabel.Height) + fSpacing + fMargins, clientwidth-(fMargins * 2), (clientHeight Div 2) - (fMargins * 2) - (fSpacing));
end;




end.

 

Share this post


Link to post
Share on other sites

of course, you could modify this unit to add any component other than the TW3Editbox.  e.g., how about a TW3ComboBox ?

 

unit LabelCombo;


interface


uses
  SmartCL.System, System.Colors, SmartCL.Components, SmartCL.Controls,
  SmartCL.Layout;


type


 TLabelCombo = class(TW3CustomControl)
 private
  fLabel: TW3Label;
  fCombo: TW3ComboBox;
  fSpacing: Integer;
  fMargins: Integer;
 protected
  procedure Resize; override;
  procedure InitializeObject; override;
  procedure FinalizeObject; override;
 public
  property Label: TW3Label read fLabel;
  property ComboBox:  TW3ComboBox read fCombo;
  property Spacing: Integer read fSpacing write fSpacing;
  property Margins: Integer read fMargins write fMargins;
 end;




implementation




procedure TLabelCombo.InitializeObject;
begin
  inherited;
  Height:= 64;
  Width:= 128;
  Spacing:= 0;
  Margins:= 0;
  FLabel := TW3Label.Create(Self);
  FLabel.Font.Name := 'verdana';
  FLabel.Font.Color := RGBToColor($FF, $FF, $FF);
  FLabel.Font.Weight := 'bold';
  FLabel.Font.Size := 18;
  FLabel.Caption := ClassName;
  FLabel.AlignText := taLeft;
  FCombo := TW3ComboBox.Create(Self);
end;


procedure TLabelCombo.FinalizeObject;
begin
  FLabel.Free;
  FCombo.Free;
  inherited;
end;


procedure TLabelCombo.Resize;
begin
 inherited;
  fLabel.SetBounds(fMargins, fMargins, clientWidth-(fMargins * 2), (clientHeight Div 2)-(fMargins * 2));
  fCombo.SetBounds(fMargins, (fLabel.Top + fLabel.Height) + fSpacing + fMargins, clientwidth-(fMargins * 2), (clientHeight Div 2) - (fMargins * 2) - (fSpacing));
end;




end.

Run-Time code:

 

  
fLabelCombo:= TLabelCombo.Create(self);
  fLabelCombo.Handle.style.setProperty('background-color', 'white');
  fLabelCombo.Label.Handle.style.setProperty('background-color', 'Red');
  fLabelCombo.ComboBox.Handle.style.setProperty('color', 'red');
  fLabelCombo.Left:= 10;
  fLabelCombo.Top:= 10;
  fLabelCombo.Width:= 128;
  fLabelCombo.Height:= 64;
  fLabelCombo.Margins:= 2;
  fLabelCombo.Spacing:= 2;


 fLabelCombo.ComboBox.Add('Test1');
 fLabelCombo.ComboBox.Add('Test2');
 fLabelCombo.ComboBox.Add('Test3');

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...