Mega Code Archive

 
Categories / Delphi / Examples
 

Save positions of TCoolBar bands

Title: Save positions of TCoolBar bands Question: TCoolBar controls lack a function to restore the layout of TCoolBand objects at run-time. How do you have it's properties put on file and reset later? How to save the positions of TCoolBar bands? Answer: This was fun to do. Yesterday Delphi3000.com newsletter #76 dropped a line on a reader's request for functions to restore the positions of TCoolBand objects at run-time. Lacking a property to check how many band are in place (something like CoolBar.Bands.Count) I thought of another way to test if all bands are stored. I included the properties that tend to change mostly (index and width) but it's easy to add other properties to store and restore. Parse all available coolbands and write their properties to file: Store(CoolBar1, 'c:\coolbar1.ini'); Parse all available coolbands and check for their properties by matching IDs: Restore(CoolBar1, 'c:\coolbar1.ini'); procedure Store(aCoolBar: TCoolBar; aFilename: string); var I, ID: Integer; aFile: TStringList; begin I := 0; ID := 0; aFile := TStringList.Create; { Parse all available coolbands } while I=I do // Make this an endless loop begin try ID := aCoolBar.Bands.Items[I].ID; except { Raise an EAbort exception to break out of the endless loop when properties of all coolbands are written to file. We know all coolbands are parsed when EListError is raised if we try to collect properties of a coolband that does not exist } on EListError do begin { All done? Save! } aFile.SaveToFile(aFilename); aFile.Free; Abort; // Crack the loop with EAbort end; end; { Write it's properties to file - Set } aFile.Add(IntToStr(ID)+'Index='+IntToStr(aCoolBar.Bands.Items[I].Index)); aFile.Add(IntToStr(ID)+'Width='+IntToStr(aCoolBar.Bands.Items[I].Width)); I := I+1; end; end; procedure Restore(aCoolBar: TCoolBar; aFilename: string); var I, ID, J: Integer; aFile: TStringList; begin I := 0; ID := 0; aFile := TStringList.Create; aFile.LoadFromFile(aFileName); { Parse all available coolbands } while I=I do begin try ID := aCoolBar.Bands.Items[I].ID; except on EListError do Abort; end; { Restore properties from file - Get } aCoolBar.Bands.Items[I].Index := StrToInt(aFile.Values[IntToStr(ID)+'Index']); aCoolBar.Bands.Items[I].Width := StrToInt(aFile.Values[IntToStr(ID)+'Width']); I := I+1; end; // Result := True; end; Enjoy! Regards, Patrick de Kleijn The Netherlands