Ir al contenido


Foto

Borrando Items de un TListView


Mejor respuesta Agustin Ortu , 20 octubre 2016 - 12:19

No, el problema no es que te borra todos. Estas saliendo de tu procedimiento sin ejecutar Fotos.Items.EndUpdate;

 

Refactorizando: 


delphi
  1. procedure TFDatosVME.QuitaFoto(Lista: TStringList; Fotos: TListView);
  2. var
  3. i: Integer;
  4. begin
  5. Fotos.Items.BeginUpdate;
  6. try
  7. for i := Fotos.Items.Count - 1 downto 0 do
  8. begin
  9. if Fotos.Items[i].Selected then
  10. begin
  11. Lista.Add(TNodoFoto(Fotos.Items[i].Data).Ubicacion);
  12.  
  13. cdsFotos.First;
  14. while not cdsFotos.Eof do
  15. begin
  16. if cdsFotos.FieldByName('veh_fotos').AsString = TNodoFoto(Fotos.Items[i].Data).Nombre then
  17. begin
  18. Fotos.Items[i].Delete;
  19. cdsFotos.Delete;
  20. Exit;
  21. end;
  22.  
  23. cdsFotos.Next;
  24. end;
  25. end;
  26. end;
  27. finally
  28. Fotos.Items.EndUpdate;
  29. end;
  30. end;

Ir al mensaje completo


  • Por favor identifícate para responder
4 respuestas en este tema

#1 enecumene

enecumene

    Webmaster

  • Administrador
  • 7.419 mensajes
  • LocationRepública Dominicana

Escrito 20 octubre 2016 - 11:44

Estoy teniendo un pequeño problemita borrando los items seleccionados de un TListView y es que se me borra todos!, éste es el código:


delphi
  1. procedure TFDatosVME.QuitaFoto(Lista: TStringList; Fotos: TListView);
  2. var i: Integer;
  3. begin
  4. Fotos.Items.BeginUpdate;
  5. for I := Fotos.Items.Count - 1 downto 0 do begin
  6. if Fotos.Items[i].Selected then begin
  7. Lista.Add(TNodoFoto(Fotos.Items[i].Data).Ubicacion);
  8.  
  9. cdsFotos.First;
  10. while not cdsFotos.Eof do begin
  11. if cdsFotos.FieldByName('veh_fotos').AsString = TNodoFoto(Fotos.Items[i].Data).Nombre then begin
  12. cdsFotos.Delete;
  13. Exit;
  14. end;
  15.  
  16. cdsFotos.Next;
  17. end;
  18.  
  19. Fotos.Selected.Delete;
  20. end;
  21. end;
  22. Fotos.Items.EndUpdate;
  23. end;

Hasta ahora agrega a la lista (TStringList) correctamente todos los items seleccionado, pero al borrar me borra todos, he intentado usar las siguientes propiedades:


delphi
  1. Fotos.Items[i].Delete;
  2. Fotos.SelectedItems;
  3. Fotos.Items.Delete();

Todas con el mimo resultado.

 

¿Alguna idea?


  • 0

#2 Agustin Ortu

Agustin Ortu

    Advanced Member

  • Moderadores
  • PipPipPip
  • 831 mensajes
  • LocationArgentina

Escrito 20 octubre 2016 - 12:19   Mejor respuesta

No, el problema no es que te borra todos. Estas saliendo de tu procedimiento sin ejecutar Fotos.Items.EndUpdate;

 

Refactorizando: 


delphi
  1. procedure TFDatosVME.QuitaFoto(Lista: TStringList; Fotos: TListView);
  2. var
  3. i: Integer;
  4. begin
  5. Fotos.Items.BeginUpdate;
  6. try
  7. for i := Fotos.Items.Count - 1 downto 0 do
  8. begin
  9. if Fotos.Items[i].Selected then
  10. begin
  11. Lista.Add(TNodoFoto(Fotos.Items[i].Data).Ubicacion);
  12.  
  13. cdsFotos.First;
  14. while not cdsFotos.Eof do
  15. begin
  16. if cdsFotos.FieldByName('veh_fotos').AsString = TNodoFoto(Fotos.Items[i].Data).Nombre then
  17. begin
  18. Fotos.Items[i].Delete;
  19. cdsFotos.Delete;
  20. Exit;
  21. end;
  22.  
  23. cdsFotos.Next;
  24. end;
  25. end;
  26. end;
  27. finally
  28. Fotos.Items.EndUpdate;
  29. end;
  30. end;


  • 0

#3 Agustin Ortu

Agustin Ortu

    Advanced Member

  • Moderadores
  • PipPipPip
  • 831 mensajes
  • LocationArgentina

Escrito 20 octubre 2016 - 12:24

Aunque yo escribiria el mismo codigo asi:
 


delphi
  1. procedure TFDatosVME.QuitaFoto(Lista: TStringList; Fotos: TListView);
  2. var
  3. i: Integer;
  4. begin
  5. Fotos.Items.BeginUpdate;
  6. try
  7. for i := Fotos.Items.Count - 1 downto 0 do
  8. begin
  9. if not Fotos.Items[i].Selected then
  10. Continue;
  11.  
  12. Lista.Add(TNodoFoto(Fotos.Items[i].Data).Ubicacion);
  13.  
  14. if cdsFotos.Locate('veh_fotos', TNodoFoto(Fotos.Items[i].Data).Nombre, [loCaseInsensitive]) then
  15. begin
  16. Fotos.Items[i].Delete;
  17. cdsFotos.Delete;
  18. Exit;
  19. end;
  20. end;
  21. finally
  22. Fotos.Items.EndUpdate;
  23. end;
  24. end;


  • 0

#4 enecumene

enecumene

    Webmaster

  • Administrador
  • 7.419 mensajes
  • LocationRepública Dominicana

Escrito 20 octubre 2016 - 12:43

Ah Caray, me ha funcionado!, gracias amigo, tengo la manía de usar try..finally en otras situaciones específicas y me olvido que sirve para cualquier momento.

 

Saludos.


  • 1

#5 Agustin Ortu

Agustin Ortu

    Advanced Member

  • Moderadores
  • PipPipPip
  • 831 mensajes
  • LocationArgentina

Escrito 20 octubre 2016 - 12:51

El try-finally es una de las cosas que mas me gusta y mas he aprendido a explotar de Delphi

 

Recomiendo este video en donde se explica como simplificar loops para conseguir el mismo efecto y sin tanto anidamiento


  • 2




IP.Board spam blocked by CleanTalk.