Link to home
Start Free TrialLog in
Avatar of MerlaP83
MerlaP83

asked on

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

Avatar of BdLm
BdLm
Flag of Germany image

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;
ASKER CERTIFIED SOLUTION
Avatar of fjocke
fjocke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Geert G
i don't think your going for this the right way, can you post a screenshot of the application to better understand ?
Avatar of MerlaP83
MerlaP83

ASKER

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
Then my code should work, you just need to specify which ones that should become active.
Got it sorted now. Works as expected. Thanks a lot.
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