Re: Change the color of a TDBText field on TDBCtrlGrid? - Final implementation
eduard iglesias torres <[email protected]> Mon, 13 Feb 2023 02:33:53 -0800 (PST)
| Newsgroups | alt.comp.lang.borland-delphi |
|---|---|
| Message-ID | <[email protected]> |
El dia dimecres, 6 d’octubre de 1999 a les 9:00:00 UTC+2, Johan Boshoff va escriure: > This is the final implementation. I added a check to make sure that the > field displayed is not NULL. > procedure TViewCallDlg.DBCtrlGrid1PaintPanel(DBCtrlGrid: TDBCtrlGrid; > Index: Integer); > begin > WITH DBTPriority DO BEGIN > IF NOT Field.IsNull THEN BEGIN > CASE Field.AsString[1] OF > 'N' : BEGIN > Color := clLime; > Font.Color := clBlack; > END; (* N *) > 'U' : BEGIN > Color := clBlue; > Font.Color := clWhite; > END; (* U *) > 'V' : BEGIN > Color := clRed; > Font.Color := clBlack; > END; (* V *) > END; (* case *) > END; (* if *) > END; (* with *) > end; > AlanGLLoyd wrote in message > <[email protected]>... > >In article <[email protected]>, "Johan Boshoff" > <[email protected]> > >writes: > > > >>I am writing a database application that have to display the different > >>priorities for a call system in a DBCtrlGrid. I want each line of the > >>DBCtrlGrid to display the priority field in a colour according the value > of > >>the field being displayed e.g. > >>Priority Normal = Grey > >>Urgent = Blue > >>Very Urgent = Red > >> > >>I am using a TDBText field to display the value of the priority field, but > >>is unable to change the colour displayed as the user scroll through the > >>list. > >> > > > >You will have to make a descendant class to TDBText in order to override > its > >protected virtual Paint procedure. In that Paint procedue you do your own > >drawing of the background and the text. > > > >You also have to access the Panel protected property of the TDBCtrlGrid in > >order to allocate the TDBCtrlGrid.Panel as the parent of the TDBColorText. > You > >do this by type-casting your TDBCtrlGrid to a descendant class of > TDBCtrlGrid. > > > >As follows :- > > > >In the form's interface type clause :- > > > >type > > TDBColorText = class(TDBText) > > procedure Paint; override; // to specify & expose the TDBText.Paint > >method > > end; > > > > TDBColorCtrlGrid = class(TDBCtrlGrid); // to expose the TDBCtrl.Panel > > > >Specify a variable for the TDBColorText :- > > > >var > > MyDBColorText : TDBColorText; > > > >Specify the drawing in the paint procedure :- > > > >procedure TDBColorText.Paint; // note the class prefix > >var > > InitChar : char; > >begin > > inherited Paint; > > {paint the TDBText appropriately as you wish > > I've just coloured them as the initial character} > > InitChar := Self.Field.AsString[1]; > > with Self.Canvas do begin > > case InitChar of > > 'a' : Brush.Color := clRed; > > 'e' : begin > > Brush.Color := clBlue; > > Font.Color := clWhite; // white text on blue is clearer > > end; > > else > > Brush.Color := clBtnFace; // standard grey background > > end; > > FillRect(ClipRect); // paint the background color ... > > TextOut(2, 2, Self.Field.AsString); // ... and the text > > end; {with Self.Canvas} > >end; > > > >Then in your form's FormCreate, create an instance of the new DBColorText, > >allocating the panel of the DBCtrlGrid as the parent :- > > > >procedure TForm1.FormCreate(Sender: TObject); > >{this is FormCreate of the form the DBCtrlGrid is on} > >begin > > MyDBColorText := TDBColorText.Create(Self); > > with MyDBColorText do begin > > Parent := TDBPanelCtrlGrid(DBCtrlGrid1).Panel; // allocate the parent > >panel > > SetBounds(24, 20, 65, 17); // this is the position & size of the > TDBText > >on the panel > > DataSource := DataSource1; // whatever your datasource is ... > > DataField := 'FILE_NAME'; // ... and your data field name > > end; {with MyDBColorEdit} > >end; > > > >Thats it <g> > > > >Alan Lloyd > >[email protected] Perfecte!!!! googd!!!