Showing only the rightmost visible characters in a TStaticText? lazarus / 31-LCL ---------------------------------------------------------------------- CM630 · 2013-09-21 06:27 Here is my problem- I use a TStaticText. Sometimes the contents in it are longer and they cannot fit into the  TStaticText. In this case, it is more important to show only the rightmost part of the string. For example, if I have First name is John but in the box is not wide enough, I would like to have  name is John instead of  First name is I expected that right align would do, but it did not. ---------------------------------------------------------------------- wp · 2013-09-21 13:58 I would try a TEdit and set it's SetStart to the end of the Text. To avoid editing, set its ReadOnly to true to avoid editing. To turn off selecting set the TabStop to false, and to mimik the appearance of TStatictext turn off the Border (BorderStyle = bsNone), and set the Color to that of the Form (btnFace). You still can select text for copying it into the clipboard - which you can't do with TStaticText and Tlabel. You should pack everything into a procedure, therefore you won't have more effort than setting the caption of the StaticText in code. Something like this Code: [Select]procedure SetRightAlignedText(const AText: String; AEdit: TEdit); begin   Assert(AEdit <> nil);   AEdit.Text := AText;   AEdit.SelStart := Length(AText);   AEdit.ReadOnly := false;   AEdit.TabStop := false;   AEdit.BorderStyle := bsNone;   AEdit.Color := clBtnFace; end; ---------------------------------------------------------------------- howardpc · 2013-09-21 20:43 You can also write your own component, something like the following. The paint procedure can obviously be made far more sophisticated according to the display effect you want to achieve. Code: [Select]unit main_test_form; {$mode objfpc}{$H+} interface uses   Classes, Forms, Controls, Graphics, types; type   { TRClipText }   TRClipText = class(TGraphicControl)   protected     class function GetControlClassDefaultSize: TSize; override;     procedure Paint; override;   public     constructor Create(TheOwner: TComponent); override;   end;   { TForm1 }   TForm1 = class(TForm)     procedure FormCreate(Sender: TObject);   private     rct: TRClipText;   end; var   Form1: TForm1; implementation {$R *.lfm} class function TRClipText.GetControlClassDefaultSize: TSize; begin   Result.cx:=65;   Result.cy:=17; end; procedure TRClipText.Paint; var textWidth: integer;     s: string; begin   if (Text='') then Exit;   Canvas.Brush.Color:=Color;   Canvas.FillRect(ClientRect);   s:=Text;   textWidth:=Canvas.TextWidth(s);   if (textWidth > Width) then Repeat     Delete(s, 1, 1);     textWidth:=Canvas.TextWidth(s);   until (textWidth <= Width);   Canvas.TextOut(0, 0, s);   inherited Paint; end; constructor TRClipText.Create(TheOwner: TComponent); begin   inherited Create(TheOwner);   with GetControlClassDefaultSize do     SetInitialBounds(0, 0, cx, cy); end; { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin   rct:=TRClipText.Create(Self);   rct.Left:=10;   rct.Top:=10;   rct.Color := clBtnFace;   rct.Text:='First name is John';   rct.Parent:=Self; end; end. ---------------------------------------------------------------------- Bart · 2013-09-21 20:50 Quote from: paskal on September 21, 2013, 08:27:40 am... I expected that right align would do, but it did not. I think you have a valid point here? But what do the Greek (as in: Delphi) do in this case? Bart ---------------------------------------------------------------------- taazz · 2013-09-21 21:04 Delphi 2007 StaticText auto wraps the extra text and aligns it at the right as a result it shows the left most characters. ---------------------------------------------------------------------- ChrisF · 2013-09-21 21:58 2 possible simple workarounds (they are not constituting a real and full solution): 1/ Use a Label control instead of a StaticText control with these 2 properties: Alignment=taRightJustify WordWrap=False 2/ Or, Replace all the space characters with "no-break space" characters (i.e. Ansi 160) and with this property for your StaticText control : Alignment=taRightJustify The second solution may not work under all platforms. Plus, the "no- break space" character must be converted to UTF8 for Lazarus. Something like :Code: [Select]MyStaticText1.Caption:=StringReplace(OrginalCaption,' ',AnsiToUTF8(Chr(160)),[rfReplaceAll]); ---------------------------------------------------------------------- CM630 · 2013-09-25 09:58 1. I cannot use a Label, because for some reaons it cannot take focus, and I need it clickable. 2. I do not want right justify, I supposed it would be an easier solution. 3. If I would replace characters, I would remove the beginning of the string (which is actually a file path, I want to have the filename visible). But if I do, I will have to store the string in a separate variable, because when I click on the TStaticText, the file, whose name is listed is shown. So I will have to try howardpc's ot WP's solution. ---------------------------------------------------------------------- taazz · 2013-09-25 10:17 for file names take a look at filectrl unit function MinimizeName, it is not a right justify minimization but I think it is a better solution. ---------------------------------------------------------------------- howardpc · 2013-09-25 11:02 Quote from: paskal on September 25, 2013, 11:58:31 am1. I cannot use a Label, because for some reaons it cannot take focus, and I need it clickable. For a custom component to be clickable you will have to descend from TCustomControl. So adjust the declaration of the code I gave earlier to Code: [Select]TRClipText = class(TCustomControl)   protected     class function GetControlClassDefaultSize: TSize; override;     procedure Paint; override;   public     constructor Create(TheOwner: TComponent); override;   published     property OnClick; end; The implementation is identical to that given before. ---------------------------------------------------------------------- taazz · 2013-09-25 11:38 Actually a TLabel is clickable already, there is no need to be able to focus in order to handle onclick events. IT will not handle keyboard events though I think that tlabels can understand keyboard Accelerators (alt+letter). ---------------------------------------------------------------------- Martin_fr · 2013-09-25 13:05 Quote from: paskal on September 21, 2013, 08:27:40 amI expected that right align would do, but it did not. Align, or Alignment? I tested Alignment = taRightJustify in 1.3 (trunk), and in does show the rightmost part of the text. ---------------------------------------------------------------------- typo · 2013-09-25 13:13 Yes. Just set taRightJustify Alignment and, of course, AutoSize to False. ---------------------------------------------------------------------- ChrisF · 2013-09-25 15:08 Quote from: Martin_fr on September 25, 2013, 03:05:57 pmAlign, or Alignment? I tested Alignment = taRightJustify in 1.3 (trunk), and in does show the rightmost part of the text. No, it's not working if you have one or more space characters in your string; because text is always 'word wrapped' for a StaticText control. In this case that' the first part of the string which is displayed; or the first and the second it they both fit the text length, and so on... And paskal's first sample was including spaces (i.e. "First name is John"). That's why I've suggest to replace the space character by another one (BTW, as already written before, it was only a workaround, not a full solution). ---------------------------------------------------------------------- Martin_fr · 2013-09-25 15:23 Ok: - autosize the statictext - put it into a panel (remove border, caption, ...) - autosize the panel - set the panels MaxWidth constraints - set the statictext left anchor to the panel ---------------------------------------------------------------------- CM630 · 2013-09-27 11:29 Quote from: ChrisF on September 25, 2013, 05:08:24 pmNo, it's not working if you have one or more space characters in your string...It is not working, indeed.  Quote from: Martin_fr on September 25, 2013, 05:23:37 pmOk: - autosize the statictext - put it into a panel (remove border, caption, ...) - autosize the panel - set the panels MaxWidth constraints - set the statictext left anchor to the panel I am not sure that I undesratand you right, but if that is what you mean, it should do: I will put the TSaticText in a panel, and I will display only the rightmost part of the TSaticText, the left part will be invisible, since it will have a negative .Left value. ---------------------------------------------------------------------- wp · 2013-09-27 12:50 Since you are displaying file names: Did you read Taazz's posting on the function MinimizeName a few postings above? This is the most practical and user-friendly way for displaying too-long file names in a control: Code: [Select]  StaticText1.Caption := MinimizeName(Application.ExeName, Canvas, StaticText1.Width); Notes: The only critical parameter is the Canvas used in the MinimizeName function since TStaticText does not have this property. But a StaticText usually has them same font as its owner, and you can "borrow" the Canvas from it. Therefore, if you are calling above code from the event handler of a form it will work directly (i.e. "Canvas" refers to TForm.Canvas).If fonts are different you have to create a TControlCanvas, and assign the statictext to its Control property.Alternatively, use a TLabel instead of the TStaticText. ---------------------------------------------------------------------- CM630 · 2013-09-27 13:23 QuoteMinimizeName- Returns a shortened version of a file name, so that it fits on the given MaxWidthIf I do use it, I will have to store the filename in a var, that'is why I did it with a panel. ---------------------------------------------------------------------- wp · 2013-09-27 15:06 Very confusing answer: you were talking about TStaticText all the time, now it's a panel... And if the file name is a const you can pass it to MinimizeName anyway - so what is the problem? ---------------------------------------------------------------------- CM630 · 2013-09-29 05:57 I put the TStaticText in a Panel. I fixed the size of the panel, but I set the size of the TStaticText  to auto. Then I aligned right what I should, so now only the rightmost part of the  TStaticText is visible. Somehow I don't feel like using an extra var, but on the other hand, maybe I'll use your solution, since now the text is aligned right, even if it is not longer that the TStaticText.