TStringList
更本就没有itemIndex
如果你实在想用,从TStringList派生出自己的类,在里面添加一个itemindex.

解决方案 »

  1.   

    是没有itemIndex但可以MsgStrLst.IndexOf('xxx');得到,问题是有相同的'xxx'
      

  2.   

    何为当前的itemindex值?
    按tcombobox和tlistbox等类的说法,当前itemindex值就是当前所选定的item项在items中的顺序号。
    对于TStringlist,更本就没有当前所选值,怎么来的itemindex.
    TStringlist.indexof('字符串')只是查询语句,返回的是符合'字符串'的顺序号。这和itemindex根本是牛头不对马嘴的事情。
    要注意提问的方式。
    如何获取当前的itemindex值?
      

  3.   

    不知道楼上这位大侠有没有查过TComboBox、TListBox的Item到底是什么,我想就不会说“和itemindex根本是牛头不对马嘴的事情”,实际上,追其根源,他们都隶属于TStrings和TStringList,怎么能说无关呢?只不过TComboBox、TListBox的Items被发布了ItemIndex属性而已。
      

  4.   

    下面是TStringList 和TStrings的class define部分,请你说说TStringList的itemIndex是那个?
      TStringList = class(TStrings)
      private
        FList: PStringItemList;
        FCount: Integer;
        FCapacity: Integer;
        FSorted: Boolean;
        FDuplicates: TDuplicates;
        FCaseSensitive: Boolean;
        FOnChange: TNotifyEvent;
        FOnChanging: TNotifyEvent;
        procedure ExchangeItems(Index1, Index2: Integer);
        procedure Grow;
        procedure QuickSort(L, R: Integer; SCompare: TStringListSortCompare);
        procedure SetSorted(Value: Boolean);
        procedure SetCaseSensitive(const Value: Boolean);
      protected
        procedure Changed; virtual;
        procedure Changing; virtual;
        function Get(Index: Integer): string; override;
        function GetCapacity: Integer; override;
        function GetCount: Integer; override;
        function GetObject(Index: Integer): TObject; override;
        procedure Put(Index: Integer; const S: string); override;
        procedure PutObject(Index: Integer; AObject: TObject); override;
        procedure SetCapacity(NewCapacity: Integer); override;
        procedure SetUpdateState(Updating: Boolean); override;
        function CompareStrings(const S1, S2: string): Integer; override;
        procedure InsertItem(Index: Integer; const S: string; AObject: TObject); virtual;
      public
        destructor Destroy; override;
        function Add(const S: string): Integer; override;
        function AddObject(const S: string; AObject: TObject): Integer; override;
        procedure Clear; override;
        procedure Delete(Index: Integer); override;
        procedure Exchange(Index1, Index2: Integer); override;
        function Find(const S: string; var Index: Integer): Boolean; virtual;
        function IndexOf(const S: string): Integer; override;
        procedure Insert(Index: Integer; const S: string); override;
        procedure InsertObject(Index: Integer; const S: string;
          AObject: TObject); override;
        procedure Sort; virtual;
        procedure CustomSort(Compare: TStringListSortCompare); virtual;
        property Duplicates: TDuplicates read FDuplicates write FDuplicates;
        property Sorted: Boolean read FSorted write SetSorted;
        property CaseSensitive: Boolean read FCaseSensitive write SetCaseSensitive;
        property OnChange: TNotifyEvent read FOnChange write FOnChange;
        property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
      end;
      TStrings = class(TPersistent)
      private
        FDefined: TStringsDefined;
        FDelimiter: Char;
        FQuoteChar: Char;
        FUpdateCount: Integer;
        FAdapter: IStringsAdapter;
        function GetCommaText: string;
        function GetDelimitedText: string;
        function GetName(Index: Integer): string;
        function GetValue(const Name: string): string;
        procedure ReadData(Reader: TReader);
        procedure SetCommaText(const Value: string);
        procedure SetDelimitedText(const Value: string);
        procedure SetStringsAdapter(const Value: IStringsAdapter);
        procedure SetValue(const Name, Value: string);
        procedure WriteData(Writer: TWriter);
        function GetDelimiter: Char;
        procedure SetDelimiter(const Value: Char);
        function GetQuoteChar: Char;
        procedure SetQuoteChar(const Value: Char);
      protected
        procedure DefineProperties(Filer: TFiler); override;
        procedure Error(const Msg: string; Data: Integer); overload;
        procedure Error(Msg: PResStringRec; Data: Integer); overload;
        function ExtractName(const S: string): string;
        function Get(Index: Integer): string; virtual; abstract;
        function GetCapacity: Integer; virtual;
        function GetCount: Integer; virtual; abstract;
        function GetObject(Index: Integer): TObject; virtual;
        function GetTextStr: string; virtual;
        procedure Put(Index: Integer; const S: string); virtual;
        procedure PutObject(Index: Integer; AObject: TObject); virtual;
        procedure SetCapacity(NewCapacity: Integer); virtual;
        procedure SetTextStr(const Value: string); virtual;
        procedure SetUpdateState(Updating: Boolean); virtual;
        property UpdateCount: Integer read FUpdateCount;
        function CompareStrings(const S1, S2: string): Integer; virtual;
      public
        destructor Destroy; override;
        function Add(const S: string): Integer; virtual;
        function AddObject(const S: string; AObject: TObject): Integer; virtual;
        procedure Append(const S: string);
        procedure AddStrings(Strings: TStrings); virtual;
        procedure Assign(Source: TPersistent); override;
        procedure BeginUpdate;
        procedure Clear; virtual; abstract;
        procedure Delete(Index: Integer); virtual; abstract;
        procedure EndUpdate;
        function Equals(Strings: TStrings): Boolean;
        procedure Exchange(Index1, Index2: Integer); virtual;
        function GetText: PChar; virtual;
        function IndexOf(const S: string): Integer; virtual;
        function IndexOfName(const Name: string): Integer; virtual;
        function IndexOfObject(AObject: TObject): Integer; virtual;
        procedure Insert(Index: Integer; const S: string); virtual; abstract;
        procedure InsertObject(Index: Integer; const S: string;
          AObject: TObject); virtual;
        procedure LoadFromFile(const FileName: string); virtual;
        procedure LoadFromStream(Stream: TStream); virtual;
        procedure Move(CurIndex, NewIndex: Integer); virtual;
        procedure SaveToFile(const FileName: string); virtual;
        procedure SaveToStream(Stream: TStream); virtual;
        procedure SetText(Text: PChar); virtual;
        property Capacity: Integer read GetCapacity write SetCapacity;
        property CommaText: string read GetCommaText write SetCommaText;
        property Count: Integer read GetCount;
        property Delimiter: Char read GetDelimiter write SetDelimiter;
        property DelimitedText: string read GetDelimitedText write SetDelimitedText;
        property Names[Index: Integer]: string read GetName;
        property Objects[Index: Integer]: TObject read GetObject write PutObject;
        property QuoteChar: Char read GetQuoteChar write SetQuoteChar;
        property Values[const Name: string]: string read GetValue write SetValue;
        property Strings[Index: Integer]: string read Get write Put; default;
        property Text: string read GetTextStr write SetTextStr;
        property StringsAdapter: IStringsAdapter read FAdapter write SetStringsAdapter;
      end;
      

  5.   

    我想大家不要误会,我并不是说TStringList有ItemIndex属性,但它和TComboBox一样都有位置顺序号,TComboBox可以通过ItemIndex加1得到下个Item,但TStringList不能,我不正寻求这个方式嘛!
      

  6.   

    问题就在这儿:  其实我是想实现下移一Item的方法,我不知道当前位置是多少ComboBox可以实现:  ComboBox.ItemIndex:=ComboBox.ItemIndex+1但TStringList不可以,如果知道下一条的位置i是多少,当然就可以取到值了
      

  7.   

    renzhm(戴尔飞)哈哈哈,还想考小鸟,去。
    问题刚提出。就引来了一群骚。其实你要序号上面同志方法是正确的。
    还有什么问题。一个F1搞定。
      

  8.   

    ???
    不知你还想达到什么功能。I=indexof('substring');
    如果知道'下一条'的位置i是多少--------(I+1)
    :当然就可以取到值了-------------------那就OK了
      

  9.   

    这个我知道,请注意:item里有重复内容indexof('substring');取到的是符合条件的首条记录!如:
    0:'ab'
    1:'abc'
    2:'ab'
    3:'abcd'
      

  10.   

    老大,你自己有没有搞错。
    tcombobox和tlistbox的items的确是TStrings
    但他们调用itemindex是怎么调用的?

    TCombobox.items.itemindex;
    还是
    TCombobox.itemindex;是后者呢。
    这说明ItemIndex根本就是不会TStrings的东西,而是TCombobox为配合使用Items所添加上去的。。
    如果要使TStrings或TStringlist拥有ItemIndex,从TStrings或TStringlist
    继承一个类就行了。很好实现ItemIndex的 hehou(嘿呵) 都问你了:你到底想达到什么目的?查询目的还是记录当前所使用的substring
      

  11.   

    哦。不好意思,终于明白你的意思了,刚才由点气,语气有点不好,请见谅。
    type
    TMyStrings=class ( TStringlist)private 
          nItemIndex;Integer;
    public
          property ItemIndex:integer read nItemIndex write nItemIndex;
          constructor create;
    end;
    constructor TMyStrings.create;
    begin 
       nItemindex:=-1;
       inherited;
    end;以后对ItemIndex进行出来就行了
      

  12.   

    我不想争论“ItemIndex”:我问题的由来:MsgStrLst:TStringList;//全局变量,存储各类网络消息(由UDP发送)做界面显示这些消息,若消息已读,则界面退出时删掉我做了“第一条”“前一条”“下一条”“末一条”的功能,被显示的信息作为已读信息,专门存储ItemOld:TStringList;//私有变量界面退出时我要删除已读信息问题出在删除功能上:由于消息内容有可能重复,所以删除动作有可能删错,我只能这样做删除动作:
    procedure TMessageFrm.DelMsg;
    var
      i:integer;
    begin
      for i:=0 to ItemOld.Count-1 do
      begin
        MsgStrLst.Delete(MsgStrLst.IndexOf(ItemOld[i]));
      end;
    end;
    如果消息内容重复,并且重复内容的后条信息已读,则实际删除的是前一条
      

  13.   

    其实这个问题在发出后不久我就解决了,只是想探讨一下TStringList变量能不能象TComboBox那样在任何时候都可以得到当前的位置,没想到会惹出“众怒”,这个帖子并非无聊无用,如果真的实现了,我们没必要专门设变量来存储TStringList变量的当前位置。------------------------------------------------------------------我用Delphi四年,不可能菜的连TStringList变量有没有‘ItemIndex’属性都不知道,我感觉在这上面争论有何意义呢?正因为“缺少”我们才会去“寻找”,不要按部就班想某些事情,有的东西需要创造!同时感谢回答问题和与我“争论”的网友们,谢谢!-------------------------------------------------------------------
    结束这个“无聊”的话题吧!
      

  14.   

    ft,你程序的设计就不好。
    我不赞同你这个设计:
    被显示的信息作为已读信息,专门存储ItemOld:TStringList;//私有变量改成:
    type
      TMyStringList=class (TStringList)
      private:
         Itemold:TList;//记录所有被使用的string,
         
         procedure delete (Index: Integer):override;
         procedure deleteOld();//删除所有的被读记录。
         
         //对insert等会改变原有的TStringlist排列顺序的函数,也要重载。
      end;procedure TMyStringlist.delete (index:integer);
    var i:integer;
    begin
        for i:=0 to Itemold.count-1 do
        begin
            if index<Integer(Itemold[i]) then
               Itemold[i]:=pointer(Integer(Itemold[i])-1));//insert是+1.   add,不改变原有顺序,可以不处理。
        end;
        inherited;
    end;procedure TMyStringlist.deleteold;
    var i,j:integer;
    begin
       j:=0;
       for i:=0 to Itemold.count-1 do
       begin
         delete(integer(Itemold[i])-j);
         j++;
        end;
    end;
     
    剩余的代码自己添加
      

  15.   

    吵一下比较好:反正只对技术吵,越吵水平越高
    我用delphi才一个月,请前辈们多多指教
      

  16.   

    改一下
    procedure TMyStringlist.delete (index:integer);
    var i,j:integer;
    begin
        for i:=0 to Itemold.count-1 do
        begin
           if index=Integer(Itemold[i-j]) then
           begin
              Itemold.delete[i];
              j++;
           end 
           else if index<Integer(Itemold[i-j]) then
               Itemold[i-j]:=pointer(Integer(Itemold[i-j])-1));
    //insert是+1.   add,不改变原有顺序,可以不处理。
           end;
        
        inherited;
    end;