Recent

Author Topic: Call TEdit from procedure  (Read 1545 times)

nimmer

  • Newbie
  • Posts: 6
Call TEdit from procedure
« on: January 16, 2020, 03:33:47 pm »
Hi,

I have a form with a few buttons, which when clicked are launching another application. The applications launched are defined in another form in TEdit boxes.

To avoid writing the same code over an over again, i would like to create a procedure calling SettingsForm.TEditX.Text, where X are the number of the TEdit box corresponding to the button pressed.

How do I do that?

/nimmer

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Call TEdit from procedure
« Reply #1 on: January 16, 2020, 04:36:08 pm »
Something like this should do the trick:

Code: Pascal  [Select][+][-]
  1. function TMainForm.FindEditByNum(const Index: Integer): TEdit;
  2. var
  3.   edName: String;
  4. begin
  5.   edName := Format('Edit%d',[Index]);
  6.   Result := TEdit(SettingsForm.FindChildControl(edName);
  7. end;

You can then do, for example:

Code: Pascal  [Select][+][-]
  1. AnEdit := FindEditByNum(SomeInt);
  2. if Assigned(AnEdit) then begin
  3.   {Do something with AnEdit.Text}
  4. end;


I would, however do this somewhat differently; for example by adding in your settings form a TStringArray/TStringList field/property to store the texts of your edit controls as soon as they are entered. You could then access them by an index, as with any array, which I would get, say, from each button's Tag property.
« Last Edit: January 16, 2020, 04:54:36 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Call TEdit from procedure
« Reply #2 on: January 16, 2020, 10:47:21 pm »
Set the TAG property of each button 0...?

Create a single OnClick event for all the Tbuttons and direct the OnClick event to call this single procedure...

 So
Code: Pascal  [Select][+][-]
  1.  MyForm.OnButtonClick(Sender:Tobject);
  2.  
  3.  Begin
  4.    With TButton(Sender) do
  5.     Begin
  6.        Case TAG of
  7.          1 : CallThis;
  8.          2 : CallThat.
  9.          3 : Etc.
  10.        Else
  11.          SomeFailureMessage;
  12. End;
  13. end;
  14.  
The only true wisdom is knowing you know nothing

nimmer

  • Newbie
  • Posts: 6
Re: Call TEdit from procedure
« Reply #3 on: January 20, 2020, 03:14:11 pm »
Hi,

Won't I have to write the entire code for each button doing it this way?

/nimmer

Set the TAG property of each button 0...?

Create a single OnClick event for all the Tbuttons and direct the OnClick event to call this single procedure...

 So
Code: Pascal  [Select][+][-]
  1.  MyForm.OnButtonClick(Sender:Tobject);
  2.  
  3.  Begin
  4.    With TButton(Sender) do
  5.     Begin
  6.        Case TAG of
  7.          1 : CallThis;
  8.          2 : CallThat.
  9.          3 : Etc.
  10.        Else
  11.          SomeFailureMessage;
  12. End;
  13. end;
  14.  

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Call TEdit from procedure
« Reply #4 on: January 20, 2020, 04:45:25 pm »
In your settings form you want something like this (simplified)


Code: Pascal  [Select][+][-]
  1.   TSettingsForm = class(TForm)
  2.     Edit1: TEdit;
  3.     Edit2: TEdit;
  4.     Edit3: TEdit;
  5.   public
  6.     function GetEditText(anID: PtrInt): String;
  7.   end;
  8.  
  9. var
  10.   SettingsForm: TSettingsForm;
  11.  
  12. implementation
  13.  
  14. function TSettingsForm.GetEditText(anID: PtrInt): String;
  15. var
  16.   c: Integer;
  17. begin
  18.   for c := 0 to ControlCount-1 do
  19.     if (Controls[c] is TEdit) and (Controls[c].Tag = anID) then
  20.       Exit(TEdit(Controls[c]).Text);
  21.   Result := '';
  22. end;


In your main form, you want a single buttonclick handler assigned to all the form's buttons something along these lines (again simplified to the essentials):

Code: Pascal  [Select][+][-]
  1.   TFormMain = class(TForm)
  2.     Button1: TButton;
  3.     Button2: TButton;
  4.     Button3: TButton;
  5.     procedure ButtonClick(Sender: TObject);
  6.     procedure FormShow(Sender: TObject);
  7.   end;
  8.  
  9.   procedure LaunchApp(anAppName: String);
  10.  
  11. var
  12.   FormMain: TFormMain;
  13.  
  14. implementation
  15.  
  16. uses
  17.   uSettingsForm;
  18.  
  19. procedure LaunchApp(anAppName: String);
  20. begin
  21.   case LowerCase(anAppName) of
  22.     'explorer': ; // your code goes here
  23.     'word': ;
  24.     'doom': ;
  25.     else ShowMessageFmt('The application "%s" is invalid', [anAppName]);
  26.   end;
  27. end;
  28.  
  29. procedure TFormMain.ButtonClick(Sender: TObject);
  30. var
  31.   appName: String;
  32.   btn: TButton absolute Sender;
  33. begin
  34.   if (Sender is TButton) then
  35.     begin
  36.       appName := Trim(SettingsForm.GetEditText(btn.Tag));
  37.       if appName <> '' then
  38.         LaunchApp(appName);
  39.     end;
  40. end;
  41.  
  42. procedure TFormMain.FormShow(Sender: TObject);
  43. begin
  44.   SettingsForm.Show;
  45. end;
                                   


You have to ensure, of course, that the each button's Tag corresponds to the correct TEdit.Tag on the settings form, and that each Tag is unique.
A more complex way to ensure such correct correspondence would be to create both buttons and edits dynamically using dynamic arrays of components, when you can forget the tag and just use an array index. But this is hardly justified for a simple app, since it means writing quite a lot more code, and using the Tag property works well. For some reason there are programmers who frown on using Tag for this. I've never understood why, since it was clearly one reason why Borland introduced such a universal component property.
« Last Edit: January 20, 2020, 04:54:43 pm by howardpc »

 

TinyPortal © 2005-2018