Einzelnen Beitrag anzeigen

marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#4

Re: TDateTimePicker, OnChange soll zeitverzögert feuern

  Alt 9. Mai 2008, 07:26
Moin Heiko,

reagierst du auf das Ereignis OnChange() des DateTimePickers? Das geht auch anders:

Delphi-Quellcode:
type
  TDemoForm = class(TForm)
    DateTimePicker: TDateTimePicker;
    ListView: TListView;
    ApplicationEvents: TApplicationEvents;
    procedure DateTimePickerChanged(Sender: TObject);
    procedure DateTimePickerKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure DateTimePickerChange(Sender: TObject);
    procedure ApplicationEventsIdle(Sender: TObject; var Done: Boolean);
  private
    LastChanged: Cardinal;
  end;

var
  DemoForm: TDemoForm;

implementation

{$R *.dfm}

const
  CHANGE_DELAY = 500; // ticks = msecs

procedure LoadItems(items: TListItems; dt: TDateTime);
var
  i: Integer;
  item: TListItem;
begin
  items.BeginUpdate;
  items.Clear;
  for i := 0 to Random(8) do
  begin
    item := items.Add;
    item.Caption := IntToStr(i);
    item.SubItems.Add(DateToStr(dt));
  end;
  items.EndUpdate;
end;

procedure TDemoForm.DateTimePickerChanged(Sender: TObject);
begin
  with Sender as TDateTimePicker do
    LoadItems(ListView.Items, Date);
  LastChanged := 0;
end;

procedure TDemoForm.DateTimePickerKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    DateTimePickerChanged(Sender);
  end;
end;

procedure TDemoForm.FormCreate(Sender: TObject);
begin
  with DateTimePicker do
  begin
    OnCloseUp := DateTimePickerChanged;
    OnExit := DateTimePickerChanged;
  end;
end;

procedure TDemoForm.DateTimePickerChange(Sender: TObject);
begin
  LastChanged := GetTickCount;
end;

procedure TDemoForm.ApplicationEventsIdle(Sender: TObject;
  var Done: Boolean);
begin
  Done := LastChanged = 0;
  if Done
    then Exit
    else Done := (GetTickCount - LastChanged) > CHANGE_DELAY;
  if Done then
    DateTimePickerChanged(DateTimePicker);
end;
Freundliche Grüße
  Mit Zitat antworten Zitat