Delphi - 根据当前记录值更改文本颜色

时间:2012-09-12 03:36:48

标签: database delphi firebird

使用连接到FireBird数据库的db控件。我有一个简单的dblabel我想根据当前记录的当前值更改文本颜色

用户使用dbnavigator导航,我在导航器按钮单击中编写代码。但是有一个问题,代码总是读取前一个记录值而不是当前记录值,所以颜色是错误的!!例如:

procedure <navigator button click>;
begin
  if table1.FieldByName('field1').AsString = 'val1' then
    <dblabel.textcolor> := red
  else
    <dblabel.textcolor> := green; 

end;

但正如我所说,价值是落后的一个记录。为什么这样,改变标签文字颜色的最佳方法是什么?

由于

3 个答案:

答案 0 :(得分:3)

导航器的

OnButtonClick事件将在更改数据集中的活动记录之前触发。其中一个可能的解决方案是将代码挂钩到DataSet的OnAfterScroll事件。

答案 1 :(得分:1)

使用DataSource.OnDataChange事件处理程序

答案 2 :(得分:1)

你可以使用一堆事件!

  • TDataSource.OnDataChange
  • TJvDataSource.OnDatasetScrolled
  • DataSet.AfterScroll plus DataSet.AfterOpen

演示EXE:http://rghost.ru/40321071(最左边的按钮是“下载”)

DFM:

object Form1: TForm1
  Left = 0
  Top = 0
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'Form1'
  ClientHeight = 301
  ClientWidth = 685
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnHide = FormHide
  PixelsPerInch = 120
  TextHeight = 16
  object dbgrd1: TDBGrid
    Left = 8
    Top = 8
    Width = 425
    Height = 277
    DataSource = ds1
    ReadOnly = True
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -13
    TitleFont.Name = 'Tahoma'
    TitleFont.Style = []
  end
  object edtL_DS_ODC: TLabeledEdit
    Left = 448
    Top = 24
    Width = 200
    Height = 24
    EditLabel.Width = 163
    EditLabel.Height = 16
    EditLabel.Caption = 'TDataSource.OnDataChange'
    ReadOnly = True
    TabOrder = 1
  end
  object edtL_JDS_ODS: TLabeledEdit
    Left = 448
    Top = 104
    Width = 200
    Height = 24
    EditLabel.Width = 194
    EditLabel.Height = 16
    EditLabel.Caption = 'TJvDataSource.OnDatasetScrolled'
    ReadOnly = True
    TabOrder = 2
  end
  object edtL_T_AS: TLabeledEdit
    Left = 448
    Top = 184
    Width = 200
    Height = 24
    EditLabel.Width = 117
    EditLabel.Height = 16
    EditLabel.Caption = 'TDataSet.AfterScroll'
    ReadOnly = True
    TabOrder = 3
  end
  object pnl1: TPanel
    Left = 480
    Top = 48
    Width = 105
    Height = 25
    BevelOuter = bvLowered
    Caption = 'pnl1'
    Color = clBlack
    ParentBackground = False
    TabOrder = 4
    object dbtxt1: TDBText
      Left = 24
      Top = 6
      Width = 65
      Height = 17
      DataField = 'Dummy'
      DataSource = ds1
    end
  end
  object pnl2: TPanel
    Left = 480
    Top = 128
    Width = 105
    Height = 25
    BevelOuter = bvLowered
    Caption = 'pnl1'
    Color = clBlack
    ParentBackground = False
    TabOrder = 5
    object dbtxt2: TDBText
      Left = 24
      Top = 6
      Width = 65
      Height = 17
      DataField = 'Dummy'
      DataSource = ds1
    end
  end
  object pnl3: TPanel
    Left = 480
    Top = 208
    Width = 105
    Height = 25
    BevelOuter = bvLowered
    Caption = 'pnl1'
    Color = clBlack
    ParentBackground = False
    TabOrder = 6
    object dbtxt3: TDBText
      Left = 24
      Top = 6
      Width = 65
      Height = 17
      DataField = 'Dummy'
      DataSource = ds1
    end
  end
  object dbnvgr1: TDBNavigator
    Left = 439
    Top = 260
    Width = 240
    Height = 25
    DataSource = ds1
    Kind = dbnHorizontal
    TabOrder = 7
  end
  object ds1: TDataSource
    DataSet = data
    OnDataChange = ds1DataChange
    Left = 24
    Top = 80
  end
  object ds2: TJvDataSource
    DataSet = data
    OnDataSetScrolled = ds2DataSetScrolled
    Left = 64
    Top = 80
  end
  object data: TClientDataSet
    Aggregates = <>
    Params = <>
    AfterScroll = dataAfterScroll
    Left = 24
    Top = 24
    object fldValue: TIntegerField
      FieldName = 'Value'
    end
    object fldDummy: TIntegerField
      FieldName = 'Dummy'
    end
  end
end

PAS:

type
  TForm1 = class(TForm)
    ds1: TDataSource;
    ds2: TJvDataSource;
    data: TClientDataSet;
    fldValue: TIntegerField;
    fldDummy: TIntegerField;
    dbgrd1: TDBGrid;
    edtL_DS_ODC: TLabeledEdit;
    edtL_JDS_ODS: TLabeledEdit;
    edtL_T_AS: TLabeledEdit;
    dbtxt1: TDBText;
    pnl1: TPanel;
    pnl2: TPanel;
    dbtxt2: TDBText;
    pnl3: TPanel;
    dbtxt3: TDBText;
    dbnvgr1: TDBNavigator;
    procedure FormCreate(Sender: TObject);
    procedure dataAfterScroll(DataSet: TDataSet);
    procedure ds1DataChange(Sender: TObject; Field: TField);
    procedure ds2DataSetScrolled(Sender: TObject);
    procedure FormHide(Sender: TObject);
  private
    { Private declarations }
    procedure ShowIt(const el: TLabeledEdit; Const color, value: integer); overload;
    procedure ShowIt(const el: TLabeledEdit); overload;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var i,j,k: integer;
begin
   data.CreateDataSet;

   for i := 1 to 20 do begin
       j := Random(100) - 50;
       k := Random(20);
       data.AppendRecord([j,k]);
   end;
end;

procedure TForm1.FormHide(Sender: TObject);
begin
  data.Close;
end;

procedure TForm1.ShowIt(const el: TLabeledEdit);
begin
  ShowIt(el, fldValue.AsInteger, fldDummy.AsInteger);
end;

procedure TForm1.ShowIt(const el: TLabeledEdit; const color, value: integer);
begin
  if el = nil then exit;

  if color < 0 then el.Color := clYellow
               else el.Color := clWhite;

  el.Text := IntToStr(color) + ' ==> ' + IntToStr(value);

  dbtxt1.Font.Color := edtL_DS_ODC.color;
  dbtxt2.Font.Color := edtL_JDS_ODS.color;
  dbtxt3.Font.Color := edtL_T_AS.color;
end;

procedure TForm1.dataAfterScroll(DataSet: TDataSet);
begin
 ShowIt(edtL_T_AS);
end;

procedure TForm1.ds1DataChange(Sender: TObject; Field: TField);
begin
 ShowIt(edtL_DS_ODC);
end;

procedure TForm1.ds2DataSetScrolled(Sender: TObject);
begin
 ShowIt(edtL_JDS_ODS);
end;