Recent

Author Topic: Make a part of text of a TLabeledEdit to keep "in sync" with another one's Text  (Read 606 times)

vico

  • New Member
  • *
  • Posts: 16
Hello!

I'm making this form: <image in attachment>

the "caminho" TLabeledEdit will be composed of the user home dir + whatever is in "nome_pasta" TLabelEdit Text property.

The unit associated with the form:
Code: Pascal  [Select][+][-]
  1. unit u_novo_projeto;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { Tf_novo_projeto }
  13.  
  14.   Tf_novo_projeto = class(TForm)
  15.     btn_explorar: TButton;
  16.     btn_cancelar: TButton;
  17.     btn_ok: TButton;
  18.     caminho: TLabeledEdit;
  19.     dlg_pasta_projeto: TSelectDirectoryDialog;
  20.     titulo: TLabeledEdit;
  21.     nome_pasta: TLabeledEdit;
  22.     procedure btn_cancelarClick(Sender: TObject);
  23.     procedure btn_explorarClick(Sender: TObject);
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure nome_pastaKeyPress(Sender: TObject; var Key: char);
  26.   private
  27.  
  28.   public
  29.  
  30.   end;
  31.  
  32. var
  33.   f_novo_projeto: Tf_novo_projeto;
  34.  
  35. implementation
  36.  
  37. {$R *.lfm}
  38.  
  39. { Tf_novo_projeto }
  40.  
  41. procedure Tf_novo_projeto.btn_cancelarClick(Sender: TObject);
  42. begin
  43.   f_novo_projeto.Close;
  44. end;
  45.  
  46. procedure Tf_novo_projeto.btn_explorarClick(Sender: TObject);
  47. begin
  48.   if dlg_pasta_projeto.Execute then
  49.     caminho.Text := dlg_pasta_projeto.FileName + '\\' + nome_pasta.Text;
  50. end;
  51.  
  52. procedure Tf_novo_projeto.FormCreate(Sender: TObject);
  53. begin
  54.   caminho.Text:=GetUserDir() + nome_pasta.Text;
  55.   dlg_pasta_projeto.InitialDir:=GetUserDir();
  56.   dlg_pasta_projeto.FileName:=GetUserDir();
  57. end;
  58.  
  59. procedure Tf_novo_projeto.nome_pastaKeyPress(Sender: TObject; var Key: char);
  60. begin
  61.  
  62. end;
  63.  
  64. end.
  65.  

I want to know if is possible to, whenever the user changes the Text on "nome_pasta" (either adding more text, deleting, etc) to automatically "update" the Text of "caminho", replacing the earlier appended value of nome_pasta to the new contents (keeping the prefix, by default the home dir, intact)

jamie

  • Hero Member
  • *****
  • Posts: 6128
use the OnChange Event that gets invoked each time you change something in the text.

to change the other control's appearance, you can use the OtherControl.Caption := ????

The caption property is what you need to use.
The only true wisdom is knowing you know nothing

cdbc

  • Hero Member
  • *****
  • Posts: 1076
    • http://www.cdbc.dk
Hi
What jamie said and remember that all components implement IObserved, but maybe that's overkill  %)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2050
  • Fifty shades of code.
    • Delphi & FreePascal
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes , SysUtils , Forms , Controls , Graphics , Dialogs , StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Edit1: TEdit;
  16.     Edit2: TEdit;
  17.     procedure Edit1Change(Sender: TObject);
  18.     procedure FormCreate(Sender: TObject);
  19.   private
  20.     Edit1Text: string;
  21.     Edit2Text: string;
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.FormCreate(Sender: TObject);
  36. begin
  37.   // store original values
  38.   Edit1Text := Edit1.Caption;
  39.   Edit2Text := Edit2.Caption;
  40. end;
  41.  
  42. procedure TForm1.Edit1Change(Sender: TObject);
  43. begin
  44.   // simple append any changes
  45.   Edit1Text := Edit1.Caption;
  46.   Edit2.Text := Edit2Text + Edit1Text;
  47. end;
  48.  
  49. end.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
The attached project example uses a TLabeledEdit, so may be more immediately helpful.

Note that information retrieved by GetUserDir at program stsrt might well change thereafter without your knowing, because of what the user might do elsewhere. In other words don't rely on it always remaining as what you assume it to be.

vico

  • New Member
  • *
  • Posts: 16
I was able to solve my problem with the OnChange event. Thank you so much!

BTW, here's the end result in the unit for information, since i'm working on the project on Github.

 

TinyPortal © 2005-2018