题目:程序里有个LISTBOX1和一个BUTTON1,这个LISTBOX1里随时都有几十条数据,而且listbox1里的每条数据,都是5秒更新一次的,我现在想按下BUTTON1来清除超过5秒没有变动的数据
-----------
不知道要怎么写,要添加TIMER控件吗?希望能给出详细的button1.click事件的代码,有同学说在添加listbox 数据时做上记号,老师说不可以,一定要在在button1的click 事件里处理,但可以自己写函数,可以加别的控件
-----------
不知道要怎么写,要添加TIMER控件吗?希望能给出详细的button1.click事件的代码,有同学说在添加listbox 数据时做上记号,老师说不可以,一定要在在button1的click 事件里处理,但可以自己写函数,可以加别的控件
当button按下时,依次检查两个listbox中的内容,如果内容不同,就说明有过1个5秒未更新,就删除之
然后在BUTTONCLICK时候来比较不就得了
我刚学DELPHI 2个星期,可人家都学了很久了
和人家一起做这个题,我真是吃亏啊
不是全部一起更新的
加一个Timer控件,再加一个listbox控件.可以在Timer控件下写代码的话就是
button1click;
listbox2.Items:=listbox1.Items; button1click事件是:
for i:=0 to listbox1.Count-1 do
begin
if listbox1.items[i]<>listbox2.items[i] then
listbox1.Items.Delete[i];
end;
中与临时表对应的数据。
然后在BUTTON的CLICK的时候将ITEMINDEX里的相应条目删掉就可以了
在数据存储的时候,使用GetTickCount保存下数据插入的时间(STrings的附加结构不会不知道怎么用吧)
在点击按钮的时候在调用GetTickCount获取当前的时间,两个相减,大于5秒就删除。
难啊难真是难真是TNND的难啊能作出来的都不是人啊
请不要以为会放两个button,写几个循环语句就是程序员
这些都跟ASP里的sesstion 超时一样的道理。
加一个Timer控件,再加一个listbox控件.可以在Timer控件下写代码的话就是
button1click;
listbox2.Items:=listbox1.Items; button1click事件是:
j:=0;
for i:=0 to listbox1.Count-1 do
begin
if listbox1.items[j]<>listbox2.items[i] then
begin
listbox1.Items.Delete[j];
end else
inc(j);
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
OrgTime: TDateTime; //上次比较的时间
CurrTime: TDateTime; //本次操作的时间
TmpLstBx: TListBox; Day,hour,minute,second:integer; //时间 function DateTimetoTime(DateTime:TDatetime):string;
procedure CopyLstBxItem(CurrLstbx, OrgLstbx: TListBox);
procedure DelListBoxItem(CurrLstbx, OrgLstBox: TListBox);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
/////////////////////////////////////////////////////////////////////
// 功能: 删除两个listbox中不同的item
// 参数: CurrLstbx,当前操作的;OrgLstBox:保存原来的项
//
////////////////////////////////////////////////////////////////////////
procedure TForm1.DelListBoxItem(CurrLstbx, OrgLstBox: TListBox);
var
i: Integer;
begin
for i := CurrLstbx.Count - 1 downto 0 do
begin
if CurrLstbx.Items[i] <> OrgLstBox.Items[i] then
begin
CurrLstbx.Items.Delete(i);
end;
end;
OrgLstBox.Clear;
CopyLstBxItem(CurrLstbx,OrgLstBox)
end;/////////////////////////////////////////////////////////////////////
//
// 两个listbox之间拷贝
//
///////////////////////////////////////////////////////////////////
procedure TForm1.CopyLstBxItem(CurrLstbx,OrgLstbx: TListBox);
var
i: Integer;
begin
for i := 0 to CurrLstbx.Count - 1 do
begin
OrgLstBx.Items[i] := CurrLstbx.Items[i];
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
TmpLstBx := TListBox.Create(self);
TmpLstBx.Parent := Form1;
TmpLstBx.Visible := false;
CopyLstBxItem(ListBox1,TmpLstBx);
OrgTime := Time;
end;procedure TForm1.Button1Click(Sender: TObject);
var
d : TDateTime; //两次间隔时间
begin
CurrTime := Time;
d := CurrTime - OrgTime;
DateTimetoTime(d);
OrgTime := CurrTime;
if (day > 0) or (hour > 0) or (minute > 0) or (second > 1 ) then
begin
DelListBoxItem(ListBox1,TmpLstBx)
end;
end;//////////////////////////////////////////////////////////////////////////
///
/// 获得时间中的天,小时,分,秒数
///
///////////////////////////////////////////////////////////////////////////
function TForm1.DateTimetoTime(DateTime:TDatetime):string;
begin
day:=Trunc(datetime); //取得日期数
hour:=day*24 + strtoint(formatdatetime('h',datetime));
minute := StrToInt(formatdatetime('nn',datetime));
second := StrToInt(formatdatetime('ss',datetime));
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
TmpLstBx.Free;
end;end.
总的思想同上, 比较备份, 如果数据不很大的话, 可以转换成集合, 用求补函数更方便.
我不相信有这样的老师。今天心情不好,tnnd,fuck chinaren。狗屎留言板,帖子居然不是以时间排序的。再也不去了。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
templist:Tstringlist;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
templist:=Tstringlist.Create;
end;procedure TForm1.Timer1Timer(Sender: TObject);
var i:integer;
begin
for i:=listbox1.Count-1 downto 0 do
if (i<=templist.Count-1) and (listbox1.Items[i]=templist.Strings[i]) then listbox1.Items.Delete(i);
templist.Clear;
templist.AddStrings(listbox1.Items);
end;end.====================================================================像和尚的唐僧====================================================================
ListBox的Items是StringList来的,使用AddObject就可以很容易解决问题
AddObject中包含有时间信息就可以了,你添加的是对象视觉上看不见的
使用Data可以访问那个相关的对象
用html通过object调用listbox控件加一段javascript就可以写出来!
你们老师没问题吧?
B Listbox(新)
C Listbox(全新)
对比
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Timer1: TTimer;
Label1: TLabel;
Label2: TLabel;
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
Lastdatarows:integer;
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Timer1Timer(Sender: TObject);
var
i,j:integer;
begin
lastdatarows:=listbox1.Items.Count;
label2.Caption:=inttostr(lastdatarows);
for i:=0 to 9 do
begin
j:=random(1000);
listbox1.Items.Add(inttostr(j));
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
if lastdatarows<>0 then
begin
for i:=0 to lastdatarows-1 do
listbox1.Items.Delete(i);
Lastdatarows:=0;
end;
label2.Caption:=inttostr(lastdatarows);
label1.Caption:=inttostr( listbox1.Items.count);
end;end.
很Easy的
ListBox的Items是StringList来的,使用AddObject就可以很容易解决问题
AddObject中包含有时间信息就可以了,你添加的是对象视觉上看不见的
使用Data可以访问那个相关的对象
///////////////////////////////////////////
这方法不错,我喜欢~
建立一个动态数组专门存储每条记录的存活时间,这个时间可以用用Timer控件循环写入
Button事件是当发现一个记录是超过5秒中没有更新就删除了
如果更新一个记录,该记录对应的数组就将时间置零!!!重新计算时间!!
不知道对不对!;)