哦。不好意思,终于明白你的意思了,刚才由点气,语气有点不好,请见谅。 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进行出来就行了
我不想争论“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; 如果消息内容重复,并且重复内容的后条信息已读,则实际删除的是前一条
//对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;
剩余的代码自己添加
吵一下比较好:反正只对技术吵,越吵水平越高 我用delphi才一个月,请前辈们多多指教
改一下 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;
按tcombobox和tlistbox等类的说法,当前itemindex值就是当前所选定的item项在items中的顺序号。
对于TStringlist,更本就没有当前所选值,怎么来的itemindex.
TStringlist.indexof('字符串')只是查询语句,返回的是符合'字符串'的顺序号。这和itemindex根本是牛头不对马嘴的事情。
要注意提问的方式。
如何获取当前的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;
问题刚提出。就引来了一群骚。其实你要序号上面同志方法是正确的。
还有什么问题。一个F1搞定。
不知你还想达到什么功能。I=indexof('substring');
如果知道'下一条'的位置i是多少--------(I+1)
:当然就可以取到值了-------------------那就OK了
0:'ab'
1:'abc'
2:'ab'
3:'abcd'
tcombobox和tlistbox的items的确是TStrings
但他们调用itemindex是怎么调用的?
是
TCombobox.items.itemindex;
还是
TCombobox.itemindex;是后者呢。
这说明ItemIndex根本就是不会TStrings的东西,而是TCombobox为配合使用Items所添加上去的。。
如果要使TStrings或TStringlist拥有ItemIndex,从TStrings或TStringlist
继承一个类就行了。很好实现ItemIndex的 hehou(嘿呵) 都问你了:你到底想达到什么目的?查询目的还是记录当前所使用的substring
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进行出来就行了
procedure TMessageFrm.DelMsg;
var
i:integer;
begin
for i:=0 to ItemOld.Count-1 do
begin
MsgStrLst.Delete(MsgStrLst.IndexOf(ItemOld[i]));
end;
end;
如果消息内容重复,并且重复内容的后条信息已读,则实际删除的是前一条
结束这个“无聊”的话题吧!
我不赞同你这个设计:
被显示的信息作为已读信息,专门存储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;
剩余的代码自己添加
我用delphi才一个月,请前辈们多多指教
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;