[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

Search and Replace/Change function for TEdit not working as I want

Hi all,

Some weeks or months ago I requested a good search/replace function for TEdits. Say I have 5 items in a listbox and 4 TEdits..

Edit1.Text := 'Johnson';
Edit2.Text := 'Samson';
Edit3.Text := 'Swift';
Edit4.Text := 'Anderson';

When a user choose, say Johnson, from the Listbox(which holds all names) and then clicks on Edit3, then Edit3.Text should be "Johnson" and Edit1.Text should be changed to "Swift" (ie switch place).

The code which I got last time has been working very well, but as I have added some things to my application it no longer works as I want it to.

At the moment when selecting a name from the Listbox, every TEdits become active - but the only ones I want to be "active" and to be changeable is, say Edit1 to Edit12 (the others Edit should not be involved at all).
procedure HighlightEdits(Form: TForm; EnableHighlight: Boolean);
var
  I : Integer;
begin
  For I := 0 To Form.ComponentCount - 1 Do
    begin
    If Form.Components[I].ClassType = TEdit Then
      begin
      Case EnableHighlight Of
        True: (Form.Components[I] as TEdit).Color := clLime;
        False: (Form.Components[I] as TEdit).Color := clWindow;
      end;
    end;
  end;
end;
 
 
procedure TForm1.EditClick(Sender: TObject);
var
  OldText,
  NewText,
  CompareText : PChar;
  I : Integer;
begin
  If (Sender as TEdit).Color = clLime Then
    begin
    OldText := PChar((Sender as TEdit).Text);
    NewText := PChar(TaktikValdSpelare.Items[0]);
    For I := 0 To Self.ComponentCount - 1 Do
      begin
      If Self.Components[I].ClassType = TEdit Then
        begin
        If (Self.Components[I] as TEdit) <> (Sender as TEdit) Then
          begin
          CompareText := PChar((Self.Components[I] as TEdit).Text);
          If StringCompare(CompareText,NewText) Then
            begin
            (Self.Components[I] as TEdit).Text := (Sender as TEdit).Text;
          end;
        end;
      end;
    end;
    (Sender as TEdit).Text := NewText;
    (Sender as TEdit).Color := clWindow;
    Label1.Caption := '';
    HighlightEdits(Self,False);
  end;
end;

Open in new window

0
MerlaP83
Asked:
MerlaP83
  • 2
  • 2
  • 2
  • +1
1 Solution
 
BdLmCommented:
here come the code for the listbox:
-----------------------------------------------

procedure TForm1.TaktikValdSpelareChange(Sender: TObject);
var   i  : Integer;
begin

       HighlightEdits(self, false);


       i := TaktikValdSpelare.ItemIndex;

       case i of

       1..2 :  Edit1.Color := clLime;
       3..4 :  Edit2.Color := clLime;

       // .as you like ....

       end;
end;
0
 
fjockeCommented:
Not sure if this is what you are asking, but if you only want Edit1 and Edit2 to become active

Just add this in the Component parse:
if (Form1.Components[I].Name = 'Edit1') or (Form1.Components[I].Name = 'Edit2') then

Open in new window

0
 
Geert GruwezOracle dbaCommented:
i don't think your going for this the right way, can you post a screenshot of the application to better understand ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
MerlaP83Author Commented:
Not getting it to work as wanted. Any ideas or suggestions on how to change the code (pasted in previous posts). Added screenshot as requested.
explain-editreplace.jpg
0
 
fjockeCommented:
Then my code should work, you just need to specify which ones that should become active.
0
 
MerlaP83Author Commented:
Got it sorted now. Works as expected. Thanks a lot.
0
 
BdLmCommented:
are you sure you called the disable all Tedits before you highlight a few of them again ?

unit Unit_ee_request;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    TaktikValdSpelare: TComboBox;
    Label1: TLabel;
    procedure Edit1Click(Sender: TObject);
    procedure Edit2Click(Sender: TObject);
    procedure Edit3Click(Sender: TObject);
    procedure Edit4Click(Sender: TObject);
    procedure TaktikValdSpelareChange(Sender: TObject);
  private
    procedure EditClick(Sender: TObject);
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure HighlightEdits(Form: TForm; EnableHighlight: Boolean);
var
  I : Integer;
begin
  For I := 0 To Form.ComponentCount - 1 Do
    begin
    If Form.Components[I].ClassType = TEdit Then
      begin
      Case EnableHighlight Of
        True: (Form.Components[I] as TEdit).Color := clLime;
        False: (Form.Components[I] as TEdit).Color := clWindow;
      end;
    end;
  end;
end;
 
 
 
 
 
 
procedure TForm1.EditClick(Sender: TObject);
var
  OldText,
  NewText,
  CompareText : PChar;
  I : Integer;
begin
  If (Sender as TEdit).Color = clLime Then
    begin
    OldText := PChar((Sender as TEdit).Text);
    NewText := PChar(TaktikValdSpelare.Items[0]);
    For I := 0 To Self.ComponentCount - 1 Do
      begin
      If Self.Components[I].ClassType = TEdit Then
        begin
        If (Self.Components[I] as TEdit) <> (Sender as TEdit) Then
          begin
          CompareText := PChar((Self.Components[I] as TEdit).Text);
          If (Strcomp(CompareText,NewText)=0) Then
            begin
            (Self.Components[I] as TEdit).Text := (Sender as TEdit).Text;
          end;
        end;
      end;
    end;
    (Sender as TEdit).Text := NewText;
    (Sender as TEdit).Color := clWindow;
    Label1.Caption := '';
    HighlightEdits(Self,False);
  end;
end;
 
procedure TForm1.Edit1Click(Sender: TObject);
begin
    EditClick(Sender);
end;
 
procedure TForm1.Edit2Click(Sender: TObject);
begin
   EditClick(Sender);
end;
 
procedure TForm1.Edit3Click(Sender: TObject);
begin
   EditClick(Sender);
end;
 
procedure TForm1.Edit4Click(Sender: TObject);
begin
   EditClick(Sender);
end;
 
 
{***************************************************
 *  If I change the drop down box all tedits  first unhighlight
 *  step 2 Hightligh relevant ones again
 *
 ****************************************************}
 
procedure TForm1.TaktikValdSpelareChange(Sender: TObject);
var   i  : Integer;
begin
 
 
       //  step 1
       HighlightEdits(self, false);
 
 
       //  step 2 : what did the user select
 
       i := TaktikValdSpelare.ItemIndex;
 
 
       //  step 2 b : now highlight again a few editd
       case i of
 
       1..2 :  Edit1.Color := clLime;
       3..4 :  Edit2.Color := clLime;
 
       // .as you like ....
 
       end;
end;
 
end.

Open in new window

ee-tedit-highlight.bmp
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now