Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Pintar fila de Listview (https://www.clubdelphi.com/foros/showthread.php?t=91368)

LuKa$ 18-01-2017 02:57:15

Pintar fila de Listview
 
Hola,en un boton tengo el siguiente código :

Código Delphi [-]
  
procedure TForm1.Button2Click(Sender: TObject);
for i := 0 to ListView1.Items.Count - 1 do
  begin
    if ListView1.Items[i].Subitems[0] = Edit1.Text then // Comparo si son iguales 
    begin
      ListView1.color =clRed; ????      // --> Pinto la linea del listview de la fila encontrada    
    end;
  end;
end;

mi duda es como puedo pintar la fila encontrada.

Gracias.

AgustinOrtu 18-01-2017 04:47:32

Voy a asumir bastantes cosas ya que no das muchos detalles

El codigo que te propongo funciona para el ListView de la Vcl. No he cambiado ninguna propiedad de las que vienen por defecto al soltar el componente TListView en el form. Todas las cambio en el evento FormCreate del Form para que quede bien claro

Para que te funcione el codigo necesitas un TListView llamado ListView1 y un TEdit llamado Edit1. El Edit le indica al ListView que debe "repintarse" cuando se presiona ENTER, obviamente con el foco sobre el Edit. De eso se encarga el evento OnKeyDown del TEdit

Cuando el ListView recibe la orden de pintarse, el evento CustomDrawItem se dispara y te permite realizar "ajustes" sobre el item que actualmente se esta pintando. Podes consultar las propiedades del Item que recibio el evento en los argumentos del evento (el parametro Item). Tambien tenes un parametro State que indica en que "fase" del pintado esta el TListView actualmente. Esto lo explica mejor la documentacion que yo

Es un evento un poco "raro" porque en este el parametro Sender no es TObject como siempre, es un TCustomListView, que nos viene muy bien porque necesitamos el Canvas para poder realizar el pintado personalizado

Hay muchos mas eventos de pintado. Algunos requieren que actives la propiedad OwnerDraw del TListView para que se disparen. Esto implica que uno mismo debe hacerse cargo del pintado completo del Listview, en todos sus posibles estados. Es un trabajo algo pesado pero tenes flexibilidad absoluta

Sin mas, el codigo

Código Delphi [-]
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_RETURN then
    ListView1.Repaint;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
  Col: TListColumn;
  Item: TListItem;
begin
  // inicializo el TListView con 2 columnas y 10 items
  ListView1.ViewStyle := TViewStyle.vsReport;
  for I := 1 to 2 do
  begin
    Col := ListView1.Columns.Add;
    Col.Caption := 'Columna ' + I.ToString;
    Col.Width := 150;
  end;

  for I := 1 to 10 do
  begin
    Item := ListView1.Items.Add;
    Item.Caption := 'Item ' + I.ToString;
    Item.SubItems.Add('Item ' + I.ToString);
  end;
end;

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState;
  var DefaultDraw: Boolean);
begin
  // encontrado
  if Edit1.Text = Item.Caption then
    Sender.Canvas.Brush.Color := clRed;
end;

LuKa$ 19-01-2017 00:02:21

Hola,gracias tu ejemplo me ayudado pinta la fila ingresado texto en Edit1 pero cuando ingreso otro texto la fila que estaba pintada pierde el color.lo que necesito es a medida que voy ingresando (va comparando con la fila con la caja de texto) quede pintada la filas encontradas.como podria hacer esa parte.gracias.


imagen gif:
gifyu.com/images/GIF78a8c.gif

Código Delphi [-]
var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = vk_return then
    ListView1.Repaint;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
  col: tlistcolumn;
  Item: TListItem;
begin
  ListView1.ViewStyle := TViewStyle.vsReport;
  for i := 1 to 2 do
  begin
    col := ListView1.Columns.Add;
    col.Caption := 'column' + i.ToString;
    col.Width := 150;
  end;
  for i := 1 to 10 do
  begin
    Item := ListView1.items.Add;
    Item.Caption := 'n' + i.ToString;
    Item.SubItems.Add('item' + i.ToString);
  end;
end;

procedure TForm1.ListView1AdvancedCustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
  var DefaultDraw: Boolean);
begin

  if Edit1.Text = Item.SubItems[0] then
    Sender.Canvas.Brush.Color := clred;
end;

AgustinOrtu 19-01-2017 04:17:39

Tendrias que ir almacenando en alguna otra estructura las filas que fuiste encontrando. En el evento del pintado deberias pintar todas las filas que esten dentro de esa estructura

En pseudocodigo:

Código Delphi [-]
procedure TForm1.ListView1AdvancedCustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
  var DefaultDraw: Boolean);
begin
  if ItemEnEstructuraAdicional(Item) then
    PintarItemConUnColor;
end;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then
  begin
    AgregarItemEnEstructuraAdicional(Edit1.Text);
    ListView1.Repaint;
  end;
end;

Como estructura adicional podrias usar una lista, un diccionario, un arbol binario.. el que mas comodo te resulte. La idea es la misma, en esa estructura adicional tenes que poder asociar un elemento del TListView (por ejemplo, el Indice) con el texto que vas ingresando en el Edit

LuKa$ 20-01-2017 21:24:12

gracias,implementaré como lo mencionas.


La franja horaria es GMT +2. Ahora son las 02:13:34.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi