function SplitStr(const AString: string; ADelim: Char; out AString1, AString2: string): boolean;
// Splits the string S at the first occurence of delimiter character Delim and
// sets AString1 to the sub-string before Delim and AString2 to substring following Delim. If
// Delim is found in string True is returned, while if Delim is not in string
// False is returned, AString1 is set to AString and AString2 is set to ''.
var
DelimPos: integer; // position of delimiter in source string
begin
// Find position of first occurence of delimter in string
DelimPos := SysUtils.AnsiPos(ADelim, AString);
if DelimPos > 0 then
begin
// Delimiter found: do split and return True
AString1 := Copy(AString, 1, DelimPos - 1);
AString2 := Copy(AString, DelimPos + 1, MaxInt);
Result := True;
end
else
begin
// Delimeter not found: return false and set S1 to whole string
AString1 := AString;
AString2 := '';
Result := False;
end;
end;
function ExplodeStr(AString: string; const ADelim: Char; const AList: Classes.TStrings;
const AAllowEmpty: boolean = True; const ATrim: boolean = False): integer;
// Splits the string S into a list of strings, separated by Delim, and returns
// the number of strings in the list. If AllowEmpty is true then any empty
// strings are added to the list, while they are ignored if AllowEmpty is false.
// If Trim is true strings have leading and trailing spaces removed.
var
Item: string; // current delimted text
Remainder: string; // remaining unconsumed part of string
// ---------------------------------------------------------------------------
procedure AddItem;
begin
// Adds optionally trimmed item to list if required
if (ATrim) then
Item := SysUtils.Trim(Item);
if (Item <> '') or AAllowEmpty then
AList.Add(Item);
end;
// ---------------------------------------------------------------------------
begin
// Clear the list
AList.Clear;
// Check we have some entries in the string
if AString <> '' then
begin
// Repeatedly split string until we have no more entries
while SplitStr(AString, ADelim, Item, Remainder) do
begin
AddItem;
AString := Remainder;
end;
// Add any remaining item
AddItem;
end;
Result := AList.Count;
end;
object FormSelectImages: TFormSelectImages
Left = 0
Top = 0
BorderStyle = bsDialog
Caption = 'Select Images'
ClientHeight = 107
ClientWidth = 421
Color = clWindow
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poOwnerFormCenter
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 0
Top = 75
Width = 421
Height = 32
Align = alBottom
BevelOuter = bvNone
Caption = 'Panel1'
ParentBackground = False
ShowCaption = False
TabOrder = 0
ExplicitTop = 457
ExplicitWidth = 601
object Ok1: TButton
Left = 6
Top = 4
Width = 75
Height = 25
Caption = 'Ok'
ModalResult = 1
TabOrder = 0
end
object Cancel1: TButton
Left = 87
Top = 4
Width = 75
Height = 25
Caption = 'Cancel'
ModalResult = 2
TabOrder = 1
end
end
object GroupBox1: TGroupBox
Left = 8
Top = 8
Width = 395
Height = 61
Caption = 'Selections'
TabOrder = 1
object Selections1: TEdit
Left = 18
Top = 24
Width = 365
Height = 21
TabOrder = 0
Text = '1,3,5'
end
end
end
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
iStringList: TStringList;
iCount: integer;
s: integer;
begin
FormSelectImages := TFormSelectImages.Create(self);
try
if FormSelectImages.ShowModal = mrOk then
begin
ImageEnMView1.DeSelect;
ImageEnMView1.BeginSelectImages;
iStringList := TStringList.Create;
iCount := ExplodeStr(FormSelectImages.Selections1.Text, ',', iStringList, false, true);
for i := 0 to iCount - 1 do
begin
s := StrToInt(iStringList[i]);
ImageEnMView1.SelectedImage := s;
end;
iStringList.Free;
ImageEnMView1.EndSelectImages;
end;
finally
FormSelectImages.Free;
end;
end;
The string in FormSelectImages.Selections1 (a TEdit) in the test was 1,3,5. This selects images 1,3 and 5 in the ImageEmMView.
William Miller
Adirondack Software & Graphics
Email: w2m@frontiernet.net
EBook: http://www.imageen.com/ebook/
Apprehend: http://www.frontiernet.net/~w2m/index.html