Ir al contenido


Foto

Cambiar el color de un ítem dentro de un TCheckListBox.


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

#1 TiammatMX

TiammatMX

    Advanced Member

  • Miembros
  • PipPipPip
  • 1.750 mensajes
  • LocationUniverso Curvo\Vía Láctea\Sistema Solar\Planeta Tierra\América\México\Ciudad de México\Xochimilco\San Gregorio Atlapulco\Home

Escrito 20 marzo 2012 - 01:25

¡¡Éjele que pensaron "TiammatMX no puso pregunta rara ésta semana"!!..., pues les falló, jóvenes.

Tengo un TCheckListBox que lleno con ésta rutinita:



delphi
  1. procedure TfrmContenedor.LlenaDocumentos(sConsPac: string);
  2. var
  3.   iIndice : integer;
  4. begin
  5.   with qryDocumentos do
  6.   begin
  7.     Close;
  8.     SQL.Clear;
  9.     SQL.Add('Select C42_Documento_STR, '+
  10.             'case when C42_Documento_ID In (Select C42_Documento_ID from HY1_Documentos_Pac where (E2_Ctrl_Pac = '+sConsPac+')) then 1 else 0 end as Existe, '+
  11.             'case when C42_Documento_ID In (Select C42_Documento_ID from HY11_DocumentosPacElec where (E2_Ctrl_Pac = '+sConsPac+')) then 1 else 0 end as Guardado '+
  12.             'from C42_TSDocumentos;');
  13.     Open;
  14.     if RecordCount > 0 then
  15.     begin
  16.       chklstDocumentos.Items.Clear;
  17.       chklstDocumentos.Enabled := True;
  18.       First;
  19.       while NOT Eof do
  20.       begin
  21.         with chklstDocumentos do
  22.         begin
  23.           Items.Add(FieldValues['C42_Documento_STR']);
  24.           iIndice := Items.Count -1;
  25.           if FieldValues['Existe'] then
  26.             Checked[iIndice] := True;
  27.  
  28.         end;
  29.         Next;
  30.       end;
  31.     end;
  32.   end;
  33. end;



Ahora, el problema es que cuando el valor del campo "Guardado" sea igual a 1, debo cambiar el color de éste ítem por cualquier otro que me apetezca..., ¿alguna idea? ¿Sugerencias?

Como siempre, agradecido de antemano.
  • 0

#2 cadetill

cadetill

    Advanced Member

  • Moderadores
  • PipPipPip
  • 994 mensajes
  • LocationEspaña

Escrito 20 marzo 2012 - 01:44

Buenas,

Es sencillo. Sólo establece la propiedad Style a lbOwnerDrawVariable y programa el evento OnDrawItem con algo así



delphi
  1. procedure TForm1.CheckListBox1DrawItem(Control: TWinControl; Index: Integer;
  2.   Rect: TRect; State: TOwnerDrawState);
  3. begin
  4.   if CheckListBox1.Checked[Index] then
  5.     CheckListBox1.Canvas.Brush.Color := clRed
  6.   else
  7.     CheckListBox1.Canvas.Brush.Color := clWhite;
  8.   CheckListBox1.Canvas.FillRect(Rect);
  9.   CheckListBox1.Canvas.TextOut(Rect.Left, Rect.Top, CheckListBox1.Items[Index]);
  10. end;



Lo suyo es tener en cuenta si el Item está o no seleccionado para darle el color pertinente, pero eso ya te lo dejo a ti ;-)

Nos leemos

  • 0

#3 TiammatMX

TiammatMX

    Advanced Member

  • Miembros
  • PipPipPip
  • 1.750 mensajes
  • LocationUniverso Curvo\Vía Láctea\Sistema Solar\Planeta Tierra\América\México\Ciudad de México\Xochimilco\San Gregorio Atlapulco\Home

Escrito 20 marzo 2012 - 04:17

Buenas,

Es sencillo. Sólo establece la propiedad Style a lbOwnerDrawVariable y programa el evento OnDrawItem con algo así



delphi
  1. procedure TForm1.CheckListBox1DrawItem(Control: TWinControl; Index: Integer;
  2.   Rect: TRect; State: TOwnerDrawState);
  3. begin
  4.   if CheckListBox1.Checked[Index] then
  5.     CheckListBox1.Canvas.Brush.Color := clRed
  6.   else
  7.     CheckListBox1.Canvas.Brush.Color := clWhite;
  8.   CheckListBox1.Canvas.FillRect(Rect);
  9.   CheckListBox1.Canvas.TextOut(Rect.Left, Rect.Top, CheckListBox1.Items[Index]);
  10. end;



Lo suyo es tener en cuenta si el Item está o no seleccionado para darle el color pertinente, pero eso ya te lo dejo a ti ;-)

Nos leemos


Pues es como la Sección Amarilla: Sí funciona y funciona muy bien. Gracias, Cadetill...



delphi
  1. procedure TfrmContenedor.chklstDocumentosDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
  2. begin
  3.   with chklstDocumentos do
  4.   begin
  5.     if lblExpediente.Caption <> '' then
  6.     begin
  7.       if RevisaContenido(Texto2ID(Items.Strings[Index]),lblExpediente.Caption) then
  8.         Canvas.Font.Color := clGrayText
  9.       else
  10.         Canvas.Font.Color := clWindowText;
  11.  
  12.       Canvas.FillRect(Rect);
  13.       Canvas.TextOut(Rect.Left, Rect.Top, Items[Index]);
  14.     end;
  15.   end;
  16. end;


  • 0




IP.Board spam blocked by CleanTalk.