I know how to load and view one picture in delphi. However I would like to add a 'next image' button that brings up the next image in the file. I have 5 images in a file and i would like to scroll through them easily using a next button! I have tried to make the next button, But have no idea what code to put in!
Please help thanks.
Gpath is a global string variable.
procedure TPropertyForm.FormCreate(Sender: TObject);
begin
GPath := getcurrentdir + '\EmployeePhotos\';
EmployeeOpenPictureDialog.InitialDir := getcurrentdir + '\EmployeePhotos';
end;
procedure TPropertyForm.AttatchButtonClick(Sender: TObject);
var
st: string;
fsize, psize: integer;
begin
if EmployeeOpenPictureDialog.execute then
begin
st := EmployeeOpenPictureDialog.FileName;
psize := length(GPath);
fsize := length(st);
Properties.Photo := copy(st, psize + +1, fsize - psize)
end { endif };
PhotoImage.Hide;
if Properties.Photo <> '' then
begin
st := GPath + Properties.Photo;
if FileExists(st) then
begin
PhotoImage.Picture.LoadFromFile(st);
PhotoImage.Proportional := true;
PhotoImage.Show;
end
{ endif }
end; { endif }
end
procedure TPropertyForm.NextImageButtonClick(Sender: TObject);
begin
PhotoImage.Picture.LoadFromFile(st + 1);
end;
i think you want to load images from "Folder" and switch between them, if so try this code
place 2 TButtons and 1 TImage
uses jpeg;
public
{ Public declarations }
var
SL:TStringList;
ImgIndex:integer;
GPath:String;
procedure ListFileDir(Path: string; FileList: TStrings);
var
SR: TSearchRec;
begin
if FindFirst(Path + '*.jpg', faAnyFile, SR) = 0 then
begin
repeat
if (SR.Attr <> faDirectory) then
begin
FileList.Add(SR.Name);
end;
until FindNext(SR) <> 0;
FindClose(SR);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
GPath:= getcurrentdir + '\EmployeePhotos\';
SL:=TStringList.Create;
ListFileDir(GPath,SL);
Image1.Picture.LoadFromFile(GPath + SL.Strings[ImgIndex] );
end;
procedure TForm1.btnNextClick(Sender: TObject);
begin
ImgIndex:=ImgIndex+1;
if ImgIndex=SL.Count then ImgIndex :=0;
Image1.Picture.LoadFromFile(GPath + SL.Strings[ImgIndex] );
end;
procedure TForm1.btnPrevClick(Sender: TObject);
begin
ImgIndex:=ImgIndex-1;
if ImgIndex=-1 then ImgIndex :=SL.Count-1;
Image1.Picture.LoadFromFile(GPath + SL.Strings[ImgIndex] );
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
SL.Free;
end;
Related
This is a continuation of my previous question: Can I use a variable created for a component with an on click event?
I have the following code:
procedure TForm1.btnTest1Click(Sender: TObject);
var
i, j: Integer;
sCorrectpanel : string;
function FindPanel(iChar, iNum: Integer): TPanel;
var
cChar: Char;
sPanelName: string;
begin
cChar := Char(Ord('A') + iChar);
sPanelName := 'pnl' + cChar + IntToStr(iNum);
Result := FindComponent(sPanelName) as TPanel;
end;
begin
// reset the OnClick events of the panels first...
for i := 0 to 7 do
begin
for j := 1 to 7 do
FindPanel(i, j).OnClick := pnlWrongClick;
end;
// now, pick a random panel and assign its OnClick event...
FindPanel(Random(8), Random(7) + 1).OnClick := pnlCorrectClick;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
end;
procedure TForm1.pnlCorrectClick(Sender: TObject);
begin
ShowMessage('Correct panel');
end;
procedure TForm1.pnlWrongClick(Sender: TObject);
begin
ShowMessage('Wrong panel');
end;
The code randomly selects a panel, but I want to know what panel is selected by assigning the chosen panel's name to a variable.
Someone suggested to:
simply declare your own variable to save the pointer that FindPanel() returns. Also, inside the OnClick handler, you can use the Sender parameter, too"
I just don't know how to do what he recommended, any help please?
You already have a variable, you just need to assign a value to it. For example:
procedure TForm1.btnTest1Click(Sender: TObject);
var
i, j: Integer;
sCorrectPanel : string;
pCurrentPanel : TPanel;
function FindPanel(iChar, iNum: Integer): TPanel;
var
cChar: Char;
sPanelName: string;
begin
cChar := Char(Ord('A') + iChar);
sPanelName := 'pnl' + cChar + IntToStr(iNum);
Result := FindComponent(sPanelName) as TPanel;
end;
begin
// reset the OnClick events of the panels first...
for i := 0 to 7 do
begin
for j := 1 to 7 do
FindPanel(i, j).OnClick := pnlWrongClick;
end;
// now, pick a random panel and assign its OnClick event...
pCorrectPanel := FindPanel(Random(8), Random(7) + 1);
pCorrectPanel.OnClick := pnlCorrectClick;
sCorrectPanel := pCorrectPanel.Name; // <-- here
// use sCorrectPanel as needed...
end;
...
procedure TForm1.pnlCorrectClick(Sender: TObject);
begin
ShowMessage('Correct panel: ' + TPanel(Sender).Name);
end;
procedure TForm1.pnlWrongClick(Sender: TObject);
begin
ShowMessage('Wrong panel: ' + TPanel(Sender).Name);
end;
I made a directory TreeView in Delphi FMX, but when I expand 'C:\Windows\WinSxS' it contains 15000 folders. It took a lot of time, but it doesn't expand. On the other hand, when I tried to do it with a VCL TreeView, it worked fine, as it should. Is there any way to make it fast?
Here is my code:
function SlashSep(const Path, S: String): String;
begin
{$IF DEFINED(CLR)}
if Path[Length(Path)] <> '\' then
{$ELSE}
if AnsiLastChar(Path)^ <> '\' then
{$ENDIF}
Result := Path + '\' + S
else
Result := Path + S;
end;
procedure GetDir(const ParentDirectory: string; ParentItem: TTreeViewItem);
var
Status: Integer;
SearchRec: TSearchRec;
Node: TTreeViewItem;
begin
Status := FindFirst(SlashSep(ParentDirectory, '*.*'), faDirectory, SearchRec);
try
while Status = 0 do
begin
if (SearchRec.Attr and faDirectory = faDirectory) then
begin
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
begin
Node := AddChild(ParentItem,ExtractFileName(SearchRec.Name));
Node.HasChildren := True;
end;
end;
Status := FindNext(SearchRec);
end;
finally
FindClose(SearchRec);
end;
end;
procedure TForm1.ItemOnExpanding(Sender: TObject; Node: TTreeViewItem);
var
i: Integer;
begin
for i := Node.Count - 1 downto 0 do
TreeView1.RemoveObject(Node.Items[i]);
Node.BeginUpdate;
GetDir(GetPathItem(node),node);
Node.EndUpdate;
end;
It's expanding from here:
procedure TCustomTreeView.ItemExpanded(const Item: TTreeViewItem);
var
I: Integer;
Child: TTreeViewItem;
AllowExpansion: Boolean;
begin
InvalidateGlobalList;
if Item.IsExpanded then
for I := 0 to Item.Count - 1 do
begin
Child := Item.Items[I];
if not Child.IsInflated then
Child.Inflate;
end;
RealignContent;
//end;
if Assigned(FOnExpanding) then
if Item.IsExpanded then
FOnExpanding(Self, Item, AllowExpansion)
else
if Assigned(FOnCollapsing) then
if not Item.IsExpanded then
FOnCollapsing(Self, Item, AllowExpansion)
end;
Well I'm stumped.
I'm writing an installation support add-on for Smart Install Maker that will install some components for me- AlphaControls :)
And the add-on is a console application. But for some reason [down in the code] for adding packages to the "Known Packages" registry location, it wants to add an additional registry entry, even though the array size is only setup for 3. It's trying to add a .DPK file, even though the array is setup for .BPL. Soo... what the hell????
It works and all, except for that last nagging bit it tries to add.
Compile size is about 97/98k, after optimizing and compressing shrinks down to about 48k
So I guess my question is, can anyone spot the error I seem to be overlooking?
YES I KNOW- INNO SETUP, but... I've already spent the money on Smart Install Maker so I gotta use it.
No compile errors, justs adds an extra non .bpl file to registry
Code is as follows...
{Smart Install Maker installation support for components}
{for Delphi 7.0 environment only}
program pakghlp;
{$APPTYPE CONSOLE}
uses
Windows,
SysUtils,
Classes,
Registry;
var SPath,
BPLPath,
IDERoot,
DPKName: string;
const
BaseName = 'AlphaControls';
PackageRoot = 'AlphaControls\';
DPKFiles: array[1..5]
of string = ('acntD7_R.dpk',
'acntD7.dpk',
'aceD7_R.dpk',
'aceD7.dpk',
'AlphaDB7.dpk');
DPKArraySize = 5;
BPLFiles: array[1..3]
of string = ('aceD7.bpl',
'acntD7.bpl',
'AlphaDB7.bpl');
BPLDetails: array[1..3]
of string = ('AlphaControls extra',
'AlphaControls',
'AlphaControls DB-aware pack');
BPLFileQty = 3;
LookFor: array[1..2] of string = ('*.dcp','*.bpl');
LookForQty = 2;
RegPath = ';$(DELPHI)\Components\AlphaControls';
procedure InitVariables;
var
RegKey: TRegistry;
TargetKey: string;
LibPath: string;
begin
RegKey:= TRegistry.Create;
try
RegKey.RootKey := HKEY_CURRENT_USER;
TargetKey:= 'Software\Borland\Delphi\7.0';
if RegKey.OpenKeyReadOnly(TargetKey) then
begin
IDERoot:= RegKey.ReadString('RootDir');
RegKey.CloseKey;
TargetKey:= 'Software\Borland\Delphi\7.0\Library';
RegKey.OpenKeyReadOnly(TargetKey);
SPath:= RegKey.ReadString('Search Path');
LibPath:= RegKey.ReadString('Package DPL Output');
RegKey.CloseKey;
LibPath:= StringReplace(LibPath,'$(DELPHI)','',[rfIgnoreCase]);
BPLPath:= IDERoot + LibPath + '\';
end;
finally
RegKey.Free;
end;
end;
procedure GetListing(const SearchFor: String; ImportList:TStringList);
var SrchResult : TSearchRec;
begin
if FindFirst(SearchFor, faAnyFile, SrchResult) = 0 then
begin
repeat
ImportList.Add(SrchResult.name);
until FindNext(SrchResult) <> 0;
FindClose(SrchResult);
end;
end;
procedure GetBaseNames(Listing: TStringList);
var TempList: TStringList;
i: integer;
BaseName: string;
begin
TempList:= TStringList.Create;
TempList.Delimiter:= ';';
TempList.DelimitedText:= SPath;
Listing.Clear;
for i:= 0 to TempList.Count - 1 do
begin
BaseName:= TempList[i];
StringReplace(BaseName,'$(DELPHI)','X:\Dummy\Folder',[rfIgnoreCase]);
Listing.Add(ExtractFileName(BaseName));
end;
TempList.Free;
end;
function AlreadyExists: boolean;
var CheckList: TStringList;
i: integer;
Installed: boolean;
begin
CheckList:= TStringList.Create;
GetBaseNames(CheckList);
for i:= 0 to CheckList.Count -1 do
begin
if CheckList[i] = BaseName
then Installed:= true;
if Installed = true then break;
Installed:= false;
end;
CheckList.Free;
Result:= Installed;
end;
procedure ProcessIDE(InstallType: integer);
var RegKey: TRegistry;
TempList: TStringList;
i,j: integer;
NewSPath,
RegName,
RegValue,
DelEntry: string;
begin
RegKey:= TRegistry.Create;
case InstallType of
0: begin {-uninstall}
TempList:= TStringList.Create;
TempList.Delimiter:= ';';
TempList.DelimitedText:= SPath;
DelEntry:= copy(RegPath,2,Length(RegPath));
for i:= 0 to TempList.Count - 1 do
begin
if TempList[i] = DelEntry
then
begin
Templist.BeginUpdate;
Templist.Delete(i);
TempList.EndUpdate;
end;
end;
NewSPath:= TempList.DelimitedText;
try
RegKey.RootKey:= HKEY_CURRENT_USER;
RegKey.OpenKey('Software\Borland\Delphi\7.0\Library',false);
RegKey.WriteString('Search Path',NewSPath);
RegKey.CloseKey;
RegKey.OpenKey('Software\Borland\Delphi\7.0\Known Packages',false);
for i:= 0 to BPLFileQty do
begin
RegName:= BPLPath + BPLFiles[i];
RegKey.DeleteValue(RegName);
end;
finally
RegKey.CloseKey;
end;
TempList.Free;
end;
1: begin {-install}
SPath:= SPath + RegPath;
try
RegKey.RootKey:= HKEY_CURRENT_USER;
RegKey.OpenKey('Software\Borland\Delphi\7.0\Library',false);
RegKey.WriteString('Search Path',SPath);
RegKey.CloseKey;
RegKey.OpenKey('Software\Borland\Delphi\7.0\Known Packages',false);
for j:= 0 to BPLFileQty do
begin
RegName:= BPLPath + BPLFiles[j];
RegValue:= BPLDetails[j];
RegKey.WriteString(RegName,RegValue);
end;
finally
RegKey.CloseKey;
end;
end;
end;
RegKey.Free;
end;
procedure CompilePackage(PackageName: String; Wait: Boolean);
var
StartInfo : TStartupInfo;
ProcInfo : TProcessInformation;
CreateOK : Boolean;
begin
FillChar(StartInfo,SizeOf(TStartupInfo),#0);
FillChar(ProcInfo,SizeOf(TProcessInformation),#0);
StartInfo.cb := SizeOf(TStartupInfo);
CreateOK := CreateProcess(nil, PChar(PackageName), nil, nil,False,
CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS,
nil, nil, StartInfo, ProcInfo);
if CreateOK then
begin
if Wait then
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end
else
begin
WriteLN('Unable to compile: ' + DPKName);
end;
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
procedure ProcessPackages;
var Package: string;
i: integer;
const DCC32 = 'DCC32 ';
begin
for i:= 1 to DPKArraySize do
begin
DPKName:= ExpandFileName(GetCurrentDir + '\..')
+ '\' + PackageRoot + DPKFiles[i];
Package:= DCC32 + '"' + DPKName + '"';
CompilePackage(Package,true);
Sleep(500);
end;
end;
procedure ProcessFiles(InstallType: integer);
var TempList: TStringList;
MoveList: TextFile;
i,j: integer;
FileFrom,
FileTo,
ParentDir,
SearchType: string;
begin
case InstallType of
0: begin {-uninstall}
AssignFile(MoveList,'pakghlp.dat');
Reset(MoveList);
while not eof(MoveList) do
begin
readLn(MoveList,FileFrom);
if FileExists(FileFrom)
then DeleteFile(PChar(FileFrom));
end;
CloseFile(MoveList);
DeleteFile(PChar('pakghlp.dat'));
end;
1: begin {-install}
ProcessPackages;
if FileExists('pakghlp.dat') then DeleteFile(PChar('pakghlp.dat'));
AssignFile(MoveList,'pakghlp.dat');
Rewrite(MoveList);
ParentDir:= ExpandFileName(GetCurrentDir + '\..') + '\';
TempList:= TStringList.Create;
for i:= 1 to LookForQty do // file extension types
begin
SearchType:= ParentDir + PackageRoot + LookFor[i];
GetListing(SearchType,TempList);
for j:= 0 to Templist.Count - 1 do
begin
FileFrom:= ParentDir + PackageRoot + TempList[j];
FileTo:= BPLPath + TempList[j];
CopyFile(PChar(FileFrom),PChar(FileTo),False);
DeleteFile(PChar(FileFrom));
WriteLn(MoveList,FileTo);
end;
end;
CloseFile(MoveList);
end;
end;
TempList.Free;
end;
procedure InstallComponents;
begin
InitVariables;
if AlreadyExists then ProcessFiles(1) // refresh corrupt .dcu's.
else
begin // didn't previously exist
ProcessFiles(1);
ProcessIDE(1);
end;
end;
procedure RemoveComponents;
begin
InitVariables;
ProcessFiles(0);
ProcessIDE(0);
end;
{ ----- Console Application Begins Here ------- }
begin
if ParamCount = 0 then exit;
if ParamStr(1) = '-install'
then InstallComponents;
if ParamStr(1) = '-uninstall'
then RemoveComponents
else exit; // garbage trap
end.
You issue it seems related to the index used to iterate over the BPLFiles array. which is 1 index based and you are using a 0 index based.
const
BPLFiles: array[1..3]
of string = ('aceD7.bpl',
'acntD7.bpl',
'AlphaDB7.bpl');
Chage this code
for i:= 0 to BPLFileQty do
begin
RegName:= BPLPath + BPLFiles[i];
RegKey.DeleteValue(RegName);
end;
To
for i:= 1 to BPLFileQty do
begin
RegName:= BPLPath + BPLFiles[i];
RegKey.DeleteValue(RegName);
end;
And this code
for j:= 0 to BPLFileQty do
begin
RegName:= BPLPath + BPLFiles[j];
RegValue:= BPLDetails[j];
RegKey.WriteString(RegName,RegValue);
end;
To
for j:= 1 to BPLFileQty do
begin
RegName:= BPLPath + BPLFiles[j];
RegValue:= BPLDetails[j];
RegKey.WriteString(RegName,RegValue);
end;
First part of the code works OK while the second (commented) does not.
It overwrites my A1 file although it should write to A2.
procedure TForm1.AdvGlowButton12Click(Sender: TObject);
var
i,j: Integer;
Seznam: TStrings;
ApplicationPath: string;
begin
if (cxRadiogroup3.ItemIndex and cxRadiogroup2.ItemIndex) = 0 then begin
ApplicationPath:= ExtractFileDir(Application.ExeName);
Seznam:= TStringList.Create;
try
for i:=0 to advStringGrid2.ColCount-1 do
Seznam.AddStrings(advStringGrid2.Cols [i]);
for i:=0 to advStringGrid2.rowCount-1 do
Seznam.AddStrings(advStringGrid2.rows [j]);
Seznam.SaveToFile(ApplicationPath+'\A1.txt');
finally
seznam.free;
end;
end ;
//if cxRadiogroup3.ItemIndex = 1 and cxRadiogroup2.ItemIndex = 0 then begin
// ApplicationPath:= ExtractFileDir(Application.ExeName);
// Seznam:= TStringList.Create;
// try
// for i:=0 to advStringGrid2.ColCount-1 do
// Seznam.AddStrings(advStringGrid2.Cols [i]);
// for i:=0 to advStringGrid2.rowCount-1 do
// Seznam.AddStrings(advStringGrid2.rows [j]);
// Seznam.SaveToFile(ApplicationPath+'\A2.txt');
// finally
// seznam.free;
// end ;
//end
end;
What am I doing wrong ?
Also why is the stringgrid giving listindex out of bounds when I try to load into it contents from an empty text file? If I save empty stringgrid to that file,later ,though it has nothing in the file,it does not complain? Strange...
This is how I load A1 and A2 into the stringgrid.
procedure TForm1.cxRadioGroup2Click(Sender: TObject);
Var
I,j,k: Integer;
Seznam: TStrings;
ApplicationPath: string;
begin
case cxradioGroup2.ItemIndex of
0: begin
if cxradioGroup3.ItemIndex = 0 then begin
Seznam:= TStringList.Create;
AdvStringgrid2.ClearAll;
try
Seznam.LoadFromFile('A1.txt');
k:= 0;
for i:=0 to advStringGrid2.ColCount-1 do
for j:=0 to advStringGrid2.RowCount-1 do begin
advstringGrid2.Cells [i,j]:= Seznam.Strings [k];
Inc(k);
end;
finally
seznam.free;
end;
end;
if cxradioGroup3.ItemIndex = 1 then begin
Seznam:= TStringList.Create;
AdvStringgrid2.ClearAll;
try
Seznam.LoadFromFile('A2.txt');
k:=0;
for i:=0 to advStringGrid2.ColCount-1 do
for j:=0 to advStringGrid2.RowCount-1 do begin
advstringGrid2.Cells [i,j]:= Seznam.Strings [k];
Inc(k);
end;
finally
seznam.free;
end;
end;
end;
end;
end;
here is an old tipp from SwissDelphiCenter that could help you
// Save StringGrid1 to 'c:\temp.txt':
procedure TForm1.Button1Click(Sender: TObject);
begin
SaveStringGrid(StringGrid1, 'c:\temp.txt');
end;
// Load StringGrid1 from 'c:\temp.txt':
procedure TForm1.Button2Click(Sender: TObject);
begin
LoadStringGrid(StringGrid1, 'c:\temp.txt');
end;
// Save a TStringGrid to a file
procedure SaveStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
var
f: TextFile;
i, k: Integer;
begin
AssignFile(f, FileName);
Rewrite(f);
with StringGrid do
begin
// Write number of Columns/Rows
Writeln(f, ColCount);
Writeln(f, RowCount);
// loop through cells
for i := 0 to ColCount - 1 do
for k := 0 to RowCount - 1 do
Writeln(F, Cells[i, k]);
end;
CloseFile(F);
end;
// Load a TStringGrid from a file
procedure LoadStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
var
f: TextFile;
iTmp, i, k: Integer;
strTemp: String;
begin
AssignFile(f, FileName);
Reset(f);
with StringGrid do
begin
// Get number of columns
Readln(f, iTmp);
ColCount := iTmp;
// Get number of rows
Readln(f, iTmp);
RowCount := iTmp;
// loop through cells & fill in values
for i := 0 to ColCount - 1 do
for k := 0 to RowCount - 1 do
begin
Readln(f, strTemp);
Cells[i, k] := strTemp;
end;
end;
CloseFile(f);
end;
I'm trying to understand your code and tried him as good as it is possible for me to rewrite. (it's not tested)
procedure TForm1.AdvGlowButton12Click(Sender: TObject);
var
i, j: Integer;
Seznam: TStrings;
ApplicationPath: string;
fileName: string;
line: string;
begin
if (cxRadiogroup2.ItemIndex = 0) then begin
if (cxRadiogroup3.ItemIndex = 0) then
fileName:= 'A1.txt'
else
fileName:= 'A2.txt'
ApplicationPath:= ExtractFileDir(Application.ExeName);
Seznam:= TStringList.Create;
try
for k:=0 to advStringGrid2.RowCount-1 do begin
line:= '';
for i:=0 to advStringGrid2.ColCount-1 do
line = line + '|' + advStringGrid2.Cells[i, k];
Seznam.AddStrings(line);
end;
Seznam.SaveToFile(ApplicationPath + '\' + fileName);
finally
seznam.Free;
end;
end;
end;
procedure TForm1.cxRadioGroup2Click(Sender: TObject);
var
splitList: TStringList;
i, j: Integer;
Seznam: TStrings;
ApplicationPath: string;
fileName: string;
line: string;
sepIndex: integer;
begin
if (cxRadiogroup2.ItemIndex = 0) then begin
if (cxRadiogroup3.ItemIndex = 0) then
fileName:= 'A1.txt'
else
fileName:= 'A2.txt'
AdvStringgrid2.ClearAll; // don't know what this does
ApplicationPath:= ExtractFileDir(Application.ExeName);
Seznam:= TStringList.Create;
try
Seznam.LoadFromFile(fileName);
advstringGrid2.RowCount:= Seznam.Count;
splitList:= TStringList.Create;
for i:=0 to Seznam.Count-1 do begin
line:= Seznam.Strings [i];
Split('|', line, splitList);
advStringGrid2.ColCount:= Max(advStringGrid2.ColCount, splitList.Count);
for k:=0 to splitList.Count-1 do
advStringGrid2.Cells[i, k]:= splitList[k];
end;
finally
splitList.Free;
seznam.Free;
end;
end;
end;
procedure Split (const Delimiter: Char; Input: string; const Strings: TStrings);
begin
Assert(Assigned(Strings));
Strings.Clear;
Strings.Delimiter:= Delimiter;
Strings.DelimitedText:= Input;
end;
hope that helps
How do you know it is overwriting A1.txt? You are saving the exact same contents in both cases.
Founded and adapted to my needs. Then shared :-)
procedure LoadStringGrid(const AFileName: TFileName; AGrid: TStringGrid);
var
slRows: TStringList;
i: integer;
begin
slRows:= TStringList.Create;
try
slRows.LoadFromFile(AFileName);
for i:= 0 to slRows.Count -1 do
AGrid.Rows[i +1].CommaText:= slRows[i];
finally
slRows.Free;
end;
end;// LoadStringGrid
procedure SaveStringGrid(const AFileName: TFileName; AGrid: TStringGrid);
var
slRows: TStringList;
i: integer;
begin
slRows:= TStringList.Create;
try
for i:= 1 to AGrid.RowCount -1 do
slRows.Add(AGrid.Rows[i].CommaText);
slRows.SaveToFile(AFileName);
finally
slRows.Free;
end;
end;// SaveStringGrid
I found this code over the net. This puts background color to the selected texts on Trichedit:
uses
RichEdit;
procedure RE_SetSelBgColor(RichEdit: TRichEdit; AColor: TColor);
var
Format: CHARFORMAT2;
begin
FillChar(Format, SizeOf(Format), 0);
with Format do
begin
cbSize := SizeOf(Format);
dwMask := CFM_BACKCOLOR;
crBackColor := AColor;
Richedit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(#Format));
end;
end;
// Example: Set clYellow background color for the selected text.
procedure TForm1.Button1Click(Sender: TObject);
begin
RE_SetSelBgColor(RichEdit1, clYellow);
end;
However, what I need is to exclude space characters. Can someone help me? Any idea would be helpful?
My idea would be to select all space characters and then format it but then I don't know how to select them.
By the way, I am using delphi 2009.
#junmats, with this code you can select any word in a richedit control.
tested in Delphi 2010 and windows 7
uses
RichEdit;
procedure SetWordBackGroundColor(RichEdit : TRichEdit; aWord : String;AColor: TColor);
var
Format: CHARFORMAT2;
Index : Integer;
Len : Integer;
begin
FillChar(Format, SizeOf(Format), 0);
Format.cbSize := SizeOf(Format);
Format.dwMask := CFM_BACKCOLOR;
Format.crBackColor := AColor;
Index := 0;
Len := Length(RichEdit.Lines.Text) ;
Index := RichEdit.FindText(aWord, Index, Len, []);
while Index <> -1 do
begin
RichEdit.SelStart := Index;
RichEdit.SelLength := Length(aWord) ;
RichEdit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(#Format));
Index := RichEdit.FindText(aWord,Index + Length(aWord),Len, []) ;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetWordBackGroundColor(RichEdit1,' ',clYellow);// will mark all spaces
end;
if you wanna select all words except the spaces, you can do something like this
Procedure GetListofWords(Text : String; var ListofWords : TStringList);
var
DummyStr : String;
FoundWord : String;
begin
DummyStr := Text;
FoundWord := '';
if (Length(Text) = 0) then exit;
while (Pos(' ', DummyStr) > 0) do
begin
FoundWord := Copy(DummyStr, 1, Pos(' ', DummyStr) - 1);
ListofWords.Add(FoundWord);
DummyStr := Copy(DummyStr, Pos(' ', DummyStr) + 1, Length(DummyStr) - Length(FoundWord) + 1);
end;
if (Length(DummyStr) > 0) then
ListofWords.Add(DummyStr);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ListofWords : TStringList;
i : integer;
begin
ListofWords:=TStringList.Create;
try
GetListofWords(RichEdit1.Lines.Text,ListofWords);
if ListofWords.Count>0 then
for i:=0 to ListofWords.Count - 1 do
SetWordBackGroundColor(RichEdit1,ListofWords[i],clYellow);
finally
ListofWords.Clear;
ListofWords.Free;
end;
end;