题目:程序里有个LISTBOX1和一个BUTTON1,这个LISTBOX1里随时都有几十条数据,而且listbox1里的每条数据,都是5秒更新一次的,我现在想按下BUTTON1来清除超过5秒没有变动的数据
-----------
不知道要怎么写,要添加TIMER控件吗?希望能给出详细的button1.click事件的代码,有同学说在添加listbox 数据时做上记号,老师说不可以,一定要在在button1的click 事件里处理,但可以自己写函数,可以加别的控件

解决方案 »

  1.   

    用两个listbox,其中一个保存显示的数据,一个用来保存临时的数据timer  没 5 秒 对两个listbox中的数据同步一次
    当button按下时,依次检查两个listbox中的内容,如果内容不同,就说明有过1个5秒未更新,就删除之
      

  2.   

    用一个全局变量来放最初的LISTBOX1中的ITENMS,
    然后在BUTTONCLICK时候来比较不就得了
      

  3.   

    能给出详细代码让我学习吗
    我刚学DELPHI 2个星期,可人家都学了很久了
    和人家一起做这个题,我真是吃亏啊
      

  4.   

    对了,LISTBOX里的几十条数据是一条条更新的
    不是全部一起更新的
      

  5.   

    按我对你题目的理解是:
     加一个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;
      

  6.   

    呵呵,这种小Case,骗人玩儿呢吧。
      

  7.   

    一个临时表,在LISTBOX1更新的时候保存没更新的数据,Click是删除LISTBOX1
    中与临时表对应的数据。
      

  8.   

    我想也可以在LISTBOX更新的时候写些东西,因为是一条一条的更新,那么在更新前可以比较新值是否与旧值相等,如果相等的话返回ITEMINDEX(可以定义为数组),
    然后在BUTTON的CLICK的时候将ITEMINDEX里的相应条目删掉就可以了
      

  9.   

    给你一个简单的,不需要什么复杂的附加结构。
    在数据存储的时候,使用GetTickCount保存下数据插入的时间(STrings的附加结构不会不知道怎么用吧)
    在点击按钮的时候在调用GetTickCount获取当前的时间,两个相减,大于5秒就删除。
      

  10.   

    光题中的需求来看,太简单了.....定义一个二维字符数组和LISTBOX的string对应即可需要timer控件:)
      

  11.   

    用个TIMER控件,加2个数组,每在TIMER触发后,把LIST中的值给它然后在点击按钮后进行判断,就可以实现了!
      

  12.   

    "listbox1里的每条数据,都是5秒更新一次的,我现在想按下BUTTON1来清除超过5秒没有变动的数据"不用写任何代码,请注意“每条数据,都是5秒更新一次的”
      

  13.   

    厉害厉害
    难啊难真是难真是TNND的难啊能作出来的都不是人啊
      

  14.   

    大概只对学生难吧,不要把学生和程序员混为一谈
    请不要以为会放两个button,写几个循环语句就是程序员
      

  15.   

    真是不值一提的问题
    这些都跟ASP里的sesstion 超时一样的道理。
      

  16.   

    改一下按我对你题目的理解是:
     加一个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;
      

  17.   

    KAO,没想到还真有‘践人’给他答题,受你不了。
      

  18.   

    unit Unit1;interfaceuses
      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.
      

  19.   

    用Timer也行,大不了也就是开个线程去监视ListBox,不是很难嘛。
      

  20.   

    别听你teacher鬼扯, 用Data属性最好了
      

  21.   

    首先我决得大家不要这么过激, 不要嘲笑人家, 我们都有过新手的经历, 现在谁也不能说"我在Delphi领域无敌了!". 既然这样, 大家就不妨帮帮人家, 即便没有时间, 不屑一顾也就罢了, 何必费时间打字冷嘲热讽呢?
    总的思想同上, 比较备份, 如果数据不很大的话, 可以转换成集合, 用求补函数更方便.
      

  22.   

    呵呵难倒大多数程序员?
    我不相信有这样的老师。今天心情不好,tnnd,fuck chinaren。狗屎留言板,帖子居然不是以时间排序的。再也不去了。
      

  23.   

    unit Unit1;interfaceuses
      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.====================================================================像和尚的唐僧====================================================================
      

  24.   

    很Easy的
    ListBox的Items是StringList来的,使用AddObject就可以很容易解决问题
    AddObject中包含有时间信息就可以了,你添加的是对象视觉上看不见的
    使用Data可以访问那个相关的对象
      

  25.   

    我覺得這裡的某些人你們何必對人家冷嘲熱諷呢,你們難道開始學delphi的時候就什麼都會嗎?我是剛剛開始學delphi的,有好多東西都不會,看見你們的回復心都涼了,再說了,如果沒有我們這種菜鳥來問,你們的專家分怎麼漲?
      

  26.   

    这种问题还需要delphi写????
    用html通过object调用listbox控件加一段javascript就可以写出来!
    你们老师没问题吧?
      

  27.   

    A Listbox(原)
    B Listbox(新)
    C Listbox(全新)
    对比
      

  28.   

    现在的老师都很弱,我们计算机学院的一个教授,会点儿VBScript就已经很了不起了,有的是教授、副教授连编程都不会
      

  29.   

    好像大家都想着用两个listbox呢,一个listbox不能实现吗?
      

  30.   

    unit Unit1;interfaceuses
      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.
      

  31.   

    好象不复杂吧?gdp()搞定了呀
      

  32.   

    ly_liuyang(Liu Yang) :
    很Easy的
    ListBox的Items是StringList来的,使用AddObject就可以很容易解决问题
    AddObject中包含有时间信息就可以了,你添加的是对象视觉上看不见的
    使用Data可以访问那个相关的对象
    ///////////////////////////////////////////
    这方法不错,我喜欢~ 
      

  33.   


    建立一个动态数组专门存储每条记录的存活时间,这个时间可以用用Timer控件循环写入
    Button事件是当发现一个记录是超过5秒中没有更新就删除了
    如果更新一个记录,该记录对应的数组就将时间置零!!!重新计算时间!!
    不知道对不对!;)