-2

I wonder how caught a row of a listview and transform object.

I carry an .xml file and play in a listview , after loading this file you need to double-click in a row, take all of the data line and throw in a LabelEdit , as shown in the code below .

procedure TForm1.LstbxDadosDblClick(Sender: TObject);
begin
if Assigned(TMensagem(LstbxDados.Items.Objects[LstbxDados.ItemIndex])) then
begin
  with TMensagem(LstbxDados.Items.Objects[LstbxDados.ItemIndex]) do
  begin
  EdtPara.Text      := Para;
  EdtDe.Text        := De;
  EdtCabecalho.Text := Cabecalho;
  EdtCorpo.Text     := Corpo;
  end;
end;
end;

  TMensagem = class
  private
    FCorpo: String;
    FCabecalho: String;
    FPara: String;
    FDe: String;
  public
    property Para : String read FPara write FPara;
    property De : String read FDe write FDe;
    property Cabecalho: String read FCabecalho write FCabecalho;
    property Corpo : String read FCorpo write FCorpo;
  end;
abcd
  • 441
  • 6
  • 24

1 Answers1

1

Many ways to edit an object where the current object can change at any time (like with a double click). Here is one of the easiest: save when the current object changes and save at the very end. Here is a quick and dirty solution.

Add a member to the form or global in the implementation section FLastMensagem: TMensagem;

May want to initialize to nil on create or initialization (left to you). Now in the event save data when the TMensagem object changes

procedure TForm1.LstbxDadosDblClick(Sender: TObject);
var
  LNewMensagem: TMensagem;
begin
  LNewMensagem := TMensagem(LstbxDados.Items.Objects[LstbxDados.ItemIndex]));    
  if Assigned(LNewMensagem) then
  begin
    // When we switch, capture the dialog before updating it      
    if Assigned(FMensagem) and (LNewMensagem <> FLastMensagem) then
    begin
      FLastMensagem.Para := EdtPara.Text;
      FLastMensagem.De := EdtDe.Text;
      FLastMensagem.Cabecalho := EdtCabecalho.Text;
      FLastMensagem.Corpo := EdtCorpo.Text;
    end;

    EdtPara.Text      := LNewMensagem.Para;
    EdtDe.Text        := LNewMensagem.De;
    EdtCabecalho.Text := LNewMensagem.Cabecalho;
    EdtCorpo.Text     := LNewMensagem.Corpo;

    //Set the last dblclicked
    FLastMensagem := LNewMensagem
 end;
end;

Of course the very last edit needs to be saved, that you can do in say a form close (not sure what your full design is). For example

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Assigned(FLastMensagem) then
  begin
    FLastMensagem.Para := EdtPara.Text;
    FLastMensagem.De := EdtDe.Text;
    FLastMensagem.Cabecalho := EdtCabecalho.Text;
    FLastMensagem.Corpo := EdtCorpo.Text;
  end;

end;
Jasper Schellingerhout
  • 1,070
  • 1
  • 6
  • 24