Mega Code Archive

 
Categories / Delphi / Files
 

Creating Components Depending on Inifile Paramiters

Title: Creating Components Depending on Inifile Paramiters Question: If you want to create components at run time depending on paramiters which are read from inifile Do this Answer: I am reading inifile on formshow and starting to create database componensts depending on the paramiters which are in ini file. This paramiters are Left and Top and DataSource paramiters of the components. Chech this kode procedure TDataForm.FormShow(Sender: TObject); var AlimentIniFile:TIniFile; CreateCount, LabelNoInIni, EditNoInIni, MemoNoInIni:Integer; {$H-} LaCaption, LaTop, LaLeft, EdDataField, EdTop, EdLeft, MeDataField, MeTop, MeLeft:String; {$H+} begin DBNavigator1.DataSource:=DMod.GaraTableViewDS; if FileExists(AligmentFilName) then begin try AlimentIniFile:=TIniFile.Create(AligmentFilName); try LabelNoInIni:=AlimentIniFile.ReadInteger('ConCount','LabelNo',-1); EditNoInIni:=AlimentIniFile.ReadInteger('ConCount','EditNo',-1); MemoNoInIni:=AlimentIniFile.ReadInteger('ConCount','MemoNo',-1); //-------------------------Read Attiribute From Ini File-------------- //---------------Read Attrib Label----------------------- if LabelNoinIni-1 then begin lacount:=-1; for CreateCount:=0 to LabelNoInIni do begin lacount:=lacount+1; laCaption:=AlimentIniFile.ReadString('Label' +IntToStr(CreateCount),'Caption',''); laLeft:=AlimentIniFile.ReadString('Label' +IntToStr(CreateCount),'Left',''); LaTop:=AlimentIniFile.ReadString('Label' +IntToStr(CreateCount),'Top',''); la[CreateCount]:=TLabel.Create(self); la[CreateCount].Tag:=CreateCount; la[CreateCount].AutoSize:=true; la[CreateCount].Parent:=dataform; la[CreateCount].Caption:=LaCaption; la[CreateCount].Top:=StrToInt(LaTop); la[CreateCount].Left:=StrToInt(LaLeft); la[CreateCount].DragMode:=dmAutomatic; la[CreateCount].OnClick:=Label1Click; end; end; //-----------------------Read Atrib Edit-------------------- if EditNoinIni-1 then begin edcount:=-1; for CreateCount:=0 to EditNoInIni do begin edcount:=edcount+1; EdTop:=AlimentIniFile.ReadString('Edit' +IntToStr(CreateCount),'Top',''); EdLeft:=AlimentIniFile.ReadString('Edit' +IntToStr(CreateCount),'Left',''); EdDataField:=AlimentIniFile.ReadString('Edit' +IntToStr(CreateCount),'DataField',''); ed[CreateCount]:=TDBEdit.Create(self); ed[CreateCount].Tag:=CreateCount; ed[CreateCount].Top:=StrToInt(EdTop); ed[CreateCount].Left:=StrToInt(EdLeft); ed[CreateCount].Parent:=DataForm; ed[CreateCount].OnMouseDown:=Edit1MouseDown; ed[CreateCount].DataSource:=DMod.GaraTableViewDS; ed[CreateCount].DataField:=EdDataField; ed[CreateCount].OnClick:=Label1Click; end; end; //-----------------Read Memo Attrib-------------------- if MemoNoinIni-1 then begin mecount:=-1; for CreateCount:=0 to MemoNoInIni do begin mecount:=mecount+1; MeTop:=AlimentIniFile.ReadString('memo' +IntToStr(CreateCount),'Top',''); MeLeft:=AlimentIniFile.ReadString('memo' +IntToStr(CreateCount),'Left',''); MeDataField:=AlimentIniFile.ReadString('memo' +IntToStr(CreateCount),'DataField',''); me[CreateCount]:=TDBMemo.Create(self); me[CreateCount].Tag:=CreateCount; me[CreateCount].Top:=StrToInt(MeTop); me[CreateCount].Left:=StrToInt(MeLeft); me[CreateCount].Parent:=DataForm; me[CreateCount].OnMouseDown:=Edit1MouseDown; me[CreateCount].DataSource:=DMod.GaraTableViewDS; me[CreateCount].DataField:=MeDataField; me[CreateCount].OnClick:=Label1Click; end; end; except on Exception do ShowMessage('Component Create Error!'); end; finally try DMod.GaraTableView.Active:=false; DMod.GaraTableView.ReadOnly:=false; DMod.GaraTableView.TableName:=Form1.TableBox.Items[Form1.TableBox.itemindex]; DMod.GaraTableView.Active:=true; except on Exception do ShowMessage('Field Def Error'); end; AlimentIniFile.Free;//Free Allocated Memory end; end; end; I hope this will help you