Home > Back-end >  LISTBOX contrast variable consult!
LISTBOX contrast variable consult!

Time:10-24

For example LISTBOX1 list box has the following characters: 3, 1112111, 3112, 2112, contrast list box characters is in line with the variable is 1123456} {set variables, we can realize the if (Form1. ListBox3. Items. IndexOf (STR) & gt; 0) then {but list box characters with variables must be fully consistent & gt; 0} now I want to achieve is a part list box characters as long as the same condition is established for 11 can & gt; 0, that is, like POS function should accord with the parent list which part can be greater than zero, please greatly under the instruction, to the best code

CodePudding user response:

TStrings no more functionality, you have to from 0 to cycle Count - 1 to compare, a few lines of code.

CodePudding user response:

refer to the original poster czp9601 response:
say LISTBOX1 list box has the following characters: 3, 1112111, 3112, 2112, contrast list box characters is in line with the variable is 1123456} {set variables, we can realize the if (Form1. ListBox3. Items. IndexOf (STR) & gt; 0) then {but list box characters with variables must be fully consistent & gt; 0} now I want to achieve is a part list box characters as long as the same condition is established for 11 can & gt; 0, that is, like POS function that...


 unit Unit11; 

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

Type
TForm11=class (TForm)
Lst1: TListBox;
Btn1: TButton;
EdtSourceStr: TEdit;
EdtLength: TEdit;
Procedure btn1Click (Sender: TObject);
Procedure FormCreate (Sender: TObject);
Private
Procedure SelItems (const ASourceStr: string; Const Len: Integer);
{Private declarations}
Public
{Public declarations}
end;

Var
Form11: TForm11;

Implementation

{$R *. DFM}

Procedure TForm11. SelItems (const ASourceStr: string; Const Len: Integer);
Var
I: integer;
STR: string;
The begin
For I:=0 to lst1. Items. Do the Count - 1
The begin
STR:=Copy (lst1. Items [I], 0, Len);
If Pos (STR, ASourceStr) & gt; 0 then
Lst1. Selected: [I]=True;
end;
end;

Procedure TForm11. Btn1Click (Sender: TObject);
The begin
Lst1. ClearSelection;
SelItems (edtSourceStr. Text, StrToInt (edtLength. Text));
end;

Procedure TForm11. FormCreate (Sender: TObject);
The begin
Lst1. The Clear;
Lst1. Items. The Add (' 1112 ');
Lst1. Items. The Add (' 1113 ');
Lst1. Items. The Add (' 1122 ');
Lst1. Items. The Add (' 1123 ');
Lst1. MultiSelect:=True;

EdtSourceStr. Text:='1123456';
EdtLength. Text:='4';
end;

End.


CodePudding user response:

Help you write a generic function, put to find a string and ListBox. The Items as a parameter, to return to S Index value, otherwise return 1,

The function PartOfStrings (const S: a string; The Items: TStrings) : Integer;
Var
I: Integer;
The begin
Result:=1;
If (S=' ') or (Items=nil) then the Exit;

For I:=0 to the Items. Do the begin the Count - 1
If Pos (S, Items [I]) & gt; 0 then the begin
Result:=I;
The Exit;
end;
end;
end;

CodePudding user response:

reference jinghai1776 reply: 3/f
help you write a generic function, put to find a string and ListBox. The Items as a parameter, to return to S Index value, otherwise return 1,

The function PartOfStrings (const S: a string; The Items: TStrings) : Integer;
Var
I: Integer;
The begin
Result:=1;
if...


Well, can get this done in pos

CodePudding user response:

reference jinghai1776 reply: 3/f
help you write a generic function, put to find a string and ListBox. The Items as a parameter, to return to S Index value, otherwise return 1,

The function PartOfStrings (const S: a string; The Items: TStrings) : Integer;
Var
I: Integer;
The begin
Result:=1;
If (S=' ') or...


Thank you, but too much data in a list box, use the FOR slow, wait 10 seconds is as a result, the fastest way

CodePudding user response:

If you compare begins with the string left, that is, like SQL like 'XXX %'
Can be sorted again after binary search positioning, such meetings are many times faster,,,,,,,

CodePudding user response:

Can also use regular expressions, this program more concise, efficient, and

CodePudding user response:

Building Lord: there is unknown, since the size of the amount of data, in the listbox let user operation is difficult, maybe you don't need the user action, only with the help of a listbox to store data, if so, is better to create a TStringList object to operate, perhaps is quicker than the listbox,

In addition, a CompareStrings TStrings method, compare strings for the use of Delphi internal size, derived object can build an override method instead of the internal operation, so that it can realize you need some more, without having to write loops, can be done directly with the IndexOf,
  • Related