I need a help to transfer a text to a scrollbox creating edits and labes

Hello guys,

I have a text in a memoedit and I have scrollbox. I would like to transfer my text to scrollbox where
each word will turn into in a label or edit inside of my scrollbox.  

For exemple

Well, to create all words as label or edit in a scrollbox it is ok for me, I developed the code as you can see over here but I am having  a problem, if I change the scrollbox font size, some labels is over to another.

I don't know how to workaround with it, that is, how to consider the font size of my scrollbox and to give
the right space between labels and edits. Esq is a variable where is passed the value of left space.

Take a look at part of my code where my function create the labels and edits

  Function CriaObj(Obj: TWinControl; Esq, Topo, idx :Integer; Word, Tipo: String):Integer;
  var
    L   : TsLabel;
    Tam :Integer;
  begin

    if Tipo = 'Label' then
    begin
      L:=TsLabel.Create(Self);
      L.Parent  := Obj;
      L.Caption := UpperCase(Word);
      If ckDica.Checked Then
      Begin
        L.Hint := BuscaPalavraDicionario(IdiomaAtivo, Word);
      End;

      L.Tag := 1;
      L.Name := 'L' + IntToStr(idx);
      // L.Font.Style := [fsBold];
      L.SetBounds(Esq,Topo,100,15);
      // L.OnDblClick := TraduzPalavra;
      // L.OnMouseDown := LabelExpressao;
      Result:=Esq+Canvas.TextWidth(L.Caption)+10;
    end
    else
    begin
      SetLength(Edits,ContEdits+1);
      Edits[ContEdits]:=TsEdit.Create(Obj);
      Edits[ContEdits].Parent:=Obj;
      Edits[ContEdits].BorderStyle:=bsSingle;
      Edits[ContEdits].Text:='';
      // Edits[ContEdits].Font.Style := [fsBold];
      Edits[ContEdits].OnEnter := OnEnter;

      If ckDica.Checked Then
      Begin
        Edits[ContEdits].Hint := BuscaPalavraDicionario(IdiomaAtivo, Palavra);
        Edits[ContEdits].ShowHint := false;
      End;

      Edits[ContEdits].Ctl3D := False;
      Edits[ContEdits].Tag   := ContEdits;
      Edits[ContEdits].MaxLength:=Length(Palavra);
      Tam:=Canvas.TextWidth(Palavra)+10;
      Edits[ContEdits].SetBounds(Esq+3,Topo,Tam,15);
      Inc(ContEdits);
      Result:=Esq+Tam+10;
    End
  End;

Open in new window


I'd like a help on this
Thanks
Alex
LVL 1
hidrauAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sinisa VukSoftware architectCommented:
for start.... to avoid child component to reuse font settings from parent - set property of Label.ParentFont to False.
0
hidrauAuthor Commented:
ok, I will try it.
0
hidrauAuthor Commented:
Sinisav, your answer it is not what I need to do.

When I create the labes and edit in the scrollbox on the fontsize 8, all the labes and edits go on fine.

but when I change the scrollbox fontsize to 10, it starts the error, some labes is being created over another.

if I increase the fontsize, the problem increase.

I need a way to take into account the fontsize of my scrollbox. Please, check the images that is small part of my screen where is the scrollbox with labels.
image1.fw.png
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

hidrauAuthor Commented:
see the other images
image2.fw.png
image3.fw.png
0
Geert GOracle dbaCommented:
what  version of delphi are you using ?
some versions have a TFlowPanel
just add components to that and they are automatically aligned next to each ... no calculations required.

here is a sample:
unit uMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    FlowPanel1: TFlowPanel;
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    fCompId: integer;
    function ExtractWord(var aText: string): string;
  public
    constructor Create(aOwner: TComponent); override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

constructor TForm1.Create(aOwner: TComponent);
begin
  inherited Create(aOwner);
  fCompId := 1;
end;

function TForm1.ExtractWord(var aText: string): string;
var n: Integer;
begin
  Result := '';
  aText := Trim(aText);
  if aText <> '' then
  begin
    n := Pos(' ', aText);
    if n > 0 then
    begin
      Result := Copy(aText, 1, n-1);
      Delete(aText, 1, n);
    end
      else
    begin
      Result := aText;
      aText := '';
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  Temp, aWord: string;
  P: TPanel;
  L: TLabel;

begin
  // Use regular expressions to find each word ???
  // this example will just find words up to the next space.
  for I := 0 to Memo1.Lines.Count-1 do
  begin
    Temp := Memo1.Lines[I];
    while Temp <> '' do
    begin
      aWord := ExtractWord(Temp);
      if aWord <> '' then
      begin
        //P := TPanel.Create(FlowPanel1);
        //P.Caption := aWord;
        //P.BevelWidth := 5;
        //P.Parent := FlowPanel1;
        L := TLabel.Create(FlowPanel1);
        L.Caption := aWord;
        //L.Color := clWhite;
        L.Font.Size := 16 + Random(10);
        L.Margins.Left := 5;
        L.Margins.Top := 5;
        L.Margins.Bottom := 5;
        L.Margins.Right := 5;
        L.AlignWithMargins := true;
        L.Transparent := False;
        L.Color := clGreen;
        L.Parent := FlowPanel1;
      end;
    end;
  end;

end;

end.

Open in new window


The Margins help with aligning ... :)
form1 screenshot flowpanel
0
hidrauAuthor Commented:
Hello Geert Gruwez,

I use Delphi XE4.

I tried your example, it worked as you told but there was a problem, when my text is long, the floatpanel doesn't

allow scrollbar to scroll the text, please, see the image:

The problem
0
Geert GOracle dbaCommented:
ugh ... so you need a flowpanel with a scrollbar ?
drat ... it's not descending from TScrollingWinControl
... i'll figure that out tomorrow
0
hidrauAuthor Commented:
Well, I was creating all labels and edits directly on the scrollbox, but flowPanel gives me a right and easy way to create the labels and edits without any calculations. The problem is that I have some long text when created the labels and edits they will go beyond the button of my FlowPanel. With a scrollbox, it creates automatically the vertical scroll-bar.

Did you get it?
0
Sinisa VukSoftware architectCommented:
have you think about let say panels aligned to top. Each panel can hold only one sentence/row from memo.
Each label and edit component is aligned to left on same panel and both are set to autosize.
When you change font - labels resizes and move second one to right.
0
hidrauAuthor Commented:
hummm, I didn't think on this possibility. I will try something, let me see.
0
Geert GOracle dbaCommented:
i was thinking of adding scrollbars to the flowpanel
and optionally resize ... but if you set this to align = alClient and use the form resize the flowpanel should resize accordingly
0
Geert GOracle dbaCommented:
it works with adding a flowpanel on a scrollbox and autosizing the flowpanel

for code ... i added this line to the button1click routine at the bottom
// might want to set it on autoresize of the scrollbox too
  ScrollBox1.VertScrollBar.Range := FlowPanel1.Height;

here is the dfm code:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 588
  ClientWidth = 1126
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Panel3: TPanel
    Left = 0
    Top = 193
    Width = 1126
    Height = 395
    Align = alClient
    BevelWidth = 5
    Caption = 'Panel3'
    TabOrder = 1
    ExplicitLeft = 9
    ExplicitTop = 9
    ExplicitWidth = 185
    ExplicitHeight = 41
    object ScrollBox1: TScrollBox
      Left = 5
      Top = 5
      Width = 1116
      Height = 385
      HorzScrollBar.Smooth = True
      VertScrollBar.Smooth = True
      Align = alClient
      TabOrder = 0
      object FlowPanel1: TFlowPanel
        Left = 0
        Top = 0
        Width = 1112
        Height = 381
        Align = alTop
        AutoSize = True
        Caption = 'FlowPanel1'
        DoubleBuffered = True
        ParentDoubleBuffered = False
        TabOrder = 0
        ExplicitLeft = 2
        ExplicitTop = 2
        ExplicitWidth = 1124
        ExplicitHeight = 393
      end
    end
  end
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 1126
    Height = 193
    Align = alTop
    Caption = 'Panel1'
    TabOrder = 0
    object Memo1: TMemo
      Left = 1
      Top = 1
      Width = 1124
      Height = 134
      Align = alClient
      Lines.Strings = (
        'but when I change the scrollbox fontsize')
      TabOrder = 0
      ExplicitLeft = 64
      ExplicitTop = -96
      ExplicitWidth = 393
      ExplicitHeight = 289
    end
    object Panel2: TPanel
      Left = 1
      Top = 135
      Width = 1124
      Height = 57
      Align = alBottom
      Caption = 'Panel2'
      TabOrder = 1
      ExplicitLeft = 552
      ExplicitTop = 104
      ExplicitWidth = 473
      object Button1: TButton
        Left = 16
        Top = 6
        Width = 75
        Height = 25
        Caption = 'Button1'
        TabOrder = 0
        OnClick = Button1Click
      end
    end
  end
end

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert GOracle dbaCommented:
and here is an image
without scrollbar
no scrollbar
and with scrollbar
with scrollbar
0
hidrauAuthor Commented:
Hello Geert Gruwez,

Yeah, your idea is valid, but I need to adjust it in my program.

Only one flowPanel is not a good idea because the text isn't displayed as

it is in my memoedit. If you see my text, you will notice that is a music and I need

to display it the way it is, that is, Line by Line.
0
hidrauAuthor Commented:
thanks very much,

all of you helped me to reach the result :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.