Recent

Author Topic: How to print a String variable into a Tedit bar  (Read 1894 times)

AfootDesert41

  • New Member
  • *
  • Posts: 15
How to print a String variable into a Tedit bar
« on: December 26, 2018, 06:35:53 pm »
Hello! I'm working on a project where at some point I need data that is stored in public variables to be shown in a Tedit Bar. I did this:

Code: Pascal  [Select][+][-]
  1. procedure TSistemaDeCajaSupermercadoElCampo.NombreRazonSocialBarraChange(Sender: TObject);
  2. begin
  3.   NombreRazonSocialBarra.Text:=NombreORazonSocial_ing;
  4. end;    
  5.  

When I debug the project the text won't appear  :-\

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to print a String variable into a Tedit bar
« Reply #1 on: December 26, 2018, 06:46:02 pm »
Using an edit's OnChange event to alter its Text sets up an endless loop of changes. You're lucky the computer does not lock up.

AfootDesert41

  • New Member
  • *
  • Posts: 15
Re: How to print a String variable into a Tedit bar
« Reply #2 on: December 26, 2018, 06:52:14 pm »
Haha, sorry. I'm learning to program GUI's right now (I've only made console programs before) so I'm a noob, any advices?

Handoko

  • Hero Member
  • *****
  • Posts: 5436
  • My goal: build my own game engine using Lazarus
Re: How to print a String variable into a Tedit bar
« Reply #3 on: December 26, 2018, 07:40:06 pm »
Not sure what you really want to do. Here I wrote a simple example showing how to show the value of a global variable:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Dialogs, ComCtrls, StdCtrls, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Label1: TLabel;
  17.     Memo1: TMemo;
  18.     RadioGroup1: TRadioGroup;
  19.     StatusBar1: TStatusBar;
  20.     Timer1: TTimer;
  21.     procedure Button1Click(Sender: TObject);
  22.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  23.     procedure FormCreate(Sender: TObject);
  24.     procedure RadioGroup1Click(Sender: TObject);
  25.     procedure Timer1Timer(Sender: TObject);
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.   Data:  Integer = 0;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.FormCreate(Sender: TObject);
  39. begin
  40.   RadioGroup1.Visible := False;
  41.   Label1.Visible      := False;
  42.   Memo1.Visible       := False;
  43.   StatusBar1.Visible  := False;
  44.   Timer1.Enabled      := False;
  45.   Timer1.Interval     := 100;
  46. end;
  47.  
  48. procedure TForm1.Button1Click(Sender: TObject);
  49. begin
  50.   RadioGroup1.Visible := True;
  51.   Timer1.Enabled      := True;
  52.   Button1.Visible     := False;
  53. end;
  54.  
  55. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  56. begin
  57.   Timer1.Enabled := False;
  58. end;
  59.  
  60. procedure TForm1.RadioGroup1Click(Sender: TObject);
  61. begin
  62.   Label1.Visible     := RadioGroup1.ItemIndex = 0;
  63.   Memo1.Visible      := RadioGroup1.ItemIndex = 1;
  64.   StatusBar1.Visible := RadioGroup1.ItemIndex = 2;
  65.   if RadioGroup1.ItemIndex = 3 then
  66.   begin
  67.     RadioGroup1.ItemIndex := -1;
  68.     ShowMessage(Data.ToString);
  69.   end;
  70. end;
  71.  
  72. procedure TForm1.Timer1Timer(Sender: TObject);
  73. var
  74.   S: string;
  75. begin
  76.   Inc(Data);
  77.   S := Data.ToString;
  78.   case RadioGroup1.ItemIndex of
  79.     0: Label1.Caption := S;
  80.     1: begin
  81.          Memo1.Clear;
  82.          Memo1.Append(S);
  83.        end;
  84.     2: StatusBar1.SimpleText := S;
  85.   end;
  86. end;
  87.  
  88. end.

It uses a TTimer to change the value of the Data. Usually we use a TLabel to show data, not TEdit. But if you want to 'see' the data if it changed you can use a TTimer - not the best but it works for most cases. The 'better' solution is put the code on the location there it changed.

I saw you put it on the TEdit OnChanged and you show the data on the TEdit.Text. It is wrong. As mentioned by @howardpc, it can cause unexpected result.

What is the thing you really want to achieve? If it is for debugging purpose you may use ShowMessage, which is my preference. Sometimes, I use a statusbar or form's caption.

The example is a gui project, it won't work if you just copy paste the code. You can download the test.zip if you want to modify and test the code.

 

TinyPortal © 2005-2018