Jump to content

Creation TUniLabel in runtime and getting Width property


alasoft

Recommended Posts

Pardon my ignorance .. :-)

 

But if I create a TUniLabel at runtime over a TUniForm like this:

 

TMyForm.OnCreate(Sender:  TObject);

var

   oMyLabel: TUniLabel;

begin

oMyLabel:=TUniLabel.Create(Self);

with oMyLabel do

begin

Parent:=Self;

Caption:='Hi World;

Top:=10;

Left:=10;

AutoSize:=True;

end;

end;

 

The control displasy fine .. but, while the Height property was setted .. that does not happen with the Width property, wich remains in 0 (zero)

 

I need to 'calculate' somehow the width - in pixels - of that label created like the sample, in runtime, depending on the Caption property.

 

Help !!

 

:-)

 

Thanks 

 

Rober

Link to comment
Share on other sites

Hi Roberto.

Try, can will help you...
 

type
   TMyForm = class (TUniForm)
    ......
    ......
procedure MyAjaxEvent (Sender: TComponent; EventName: string;
   Params: TStrings);


   ......

TMyForm.MyAjaxEvent procedure (Sender: TComponent; EventName: string;
   Params: TStrings);
begin
   if EventName = 'myAjaxEvent' then
   begin
     (Sender as TUniLabel). Width: = StrToInt (Params.Values ​​['Width']);
   end;
end;


procedure TMyForm.UniFormCreate (Sender: TObject);
var
   oMyLabel: TUniLabel;

begin
   oMyLabel := TUniLabel.Create (Self);
   with oMyLabel do begin
     Parent := Self;
     Name := 'XoMyLabel';
     OnAjaxEvent := MyAjaxEvent;
     oMyLabel.ClientEvents.Enabled := True;
     oMyLabel.ClientEvents.ExtEvents.Values ​​['onresize'] := 'function (sender, adjWidth, adjHeight, rawWidth, rawHeight) {ajaxRequest (MyForm.XoMyLabel,'' myAjaxEvent'', ['' Width ='' + adjWidth] )}';
     AutoSize := True;
     oMyLabel.Caption := 'Hi World!';
     Top := 10;
     Left := 10;
   end;
   // Right here to get the value of width can not succeed ...
end;


procedure TMyForm.UniButton1Click (Sender: TObject);
begin
   ShowMessage (IntToStr ((Self.FindComponent ('XoMyLabel') as TUniLabel). Width));
end;

Sincerely

Link to comment
Share on other sites

Thanks a lot both of you ! .. I will try your samples assp. 

 

I'm in fault with some .. 'conceptual understanding' of those 'ajax + js events' .. it seems that not 'everything' is solved within Delphi in this wonderful framewor - I mean UniGUI, of course - 

 

Greetings

 

Rober

Link to comment
Share on other sites

  • Administrators

Pardon my ignorance .. :-)

 

But if I create a TUniLabel at runtime over a TUniForm like this:

 

TMyForm.OnCreate(Sender:  TObject);

var

   oMyLabel: TUniLabel;

begin

oMyLabel:=TUniLabel.Create(Self);

with oMyLabel do

begin

Parent:=Self;

Caption:='Hi World;

Top:=10;

Left:=10;

AutoSize:=True;

end;

end;

 

The control displasy fine .. but, while the Height property was setted .. that does not happen with the Width property, wich remains in 0 (zero)

 

I need to 'calculate' somehow the width - in pixels - of that label created like the sample, in runtime, depending on the Caption property.

 

Help !!

 

:-)

 

Thanks 

 

Rober

 

Why do you need to access width in the first place? Width is automatically managed in web side.

Link to comment
Share on other sites

  • 2 years later...

Because sometimes we need to put a label inside a panel and we need to set the width of that panel with an image width plus label width, but we can't use AutoSize of UniPanel because you removed it in UniGUI project :( .

Link to comment
Share on other sites

Hi Friends,

 

I too need to change my controls width a lot, and a automatic solution would be very nice.

We understand that sometimes we want to make better looking screens, so the controls must respond better to user commands and to run-time changes we made in the code.

I found a way that is working very well to me.

After you made any change that requires other controls to rearrange themselves, just put the code above:

Self.With := Self.With +1;
Self.With := Self.With -1;
Refresh;

Example of use: we put 2 panels and a splitter between then. They move fine, but if we have other controls inside those panels, and those controls are aligned with right, left or client, they won't resize unless you use the code I just mention.

Farshad Mohajeri did a great job with his framework, but he can't understand that simple issue is real.

Link to comment
Share on other sites

  • 2 weeks later...
function TPaginaPrincipal.TextWidth(s: TUniLabel): Integer;

var

  c: TBitmap;

  f: TFont;

begin

{ uma forma de calcular o tamanho de um texto na janela se o canvas não atualiza via ajax }

  c := TBitmap.Create;

  f := TFont.Create;

  Result := 8 * Length(s.Caption);

  f.Height := s.Font.Height;

  f.Size := s.Font.Size;

  f.Name := s.Font.Name;

  f.Style := s.Font.Style;

  try

    c.Canvas.Font.Assign(f);

    Result := c.Canvas.TextWidth(s.Caption);

  finally

    c.Free;

  end;

end;

Link to comment
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...