Mega Code Archive

 
Categories / Delphi / OOP
 

How to change a property of all components in one time

Title: How to change a property of all components in one time Question: Do you know how to change a speacial property of all components own this property at one time Answer: The best way is to make a separate unit. On top of the unit you have to make a new type of TControl... type TCheckClass = class(TControl); Now the little procedure can call by other units...for example here we change the font name and the char set... procedure TurnFontCharset(NewFontName:TFontName;NewCharSet:TFontCharset); var n:Integer; procedure CheckComponent(C:TComponent); var ClassRef:TClass; ChangeClass:TCheckClass; x:Integer; begin ClassRef:=C.ClassType; while ClassRefnil do begin if ClassRef.ClassName='TControl' then begin ChangeClass:=TCheckClass(C); ChangeClass.Font.Name:=NewFontName; ChangeClass.Font.CharSet:=NewCharSet; break; end; ClassRef:=ClassRef.ClassParent; end; for x:=0 to C.ComponentCount-1 do CheckComponent(C.Components[x]); end; begin for n:=0 to Form1.ComponentCount-1 do CheckComponent(Form1.Components[n]); end; What we doing: We take the base form and looking for all child components of this form and call the sub procedure CheckComponent with the pointer to the component. In the sub procedure we first ask for the class type. Is this class type a TControl, we transfer the pointer to our on declaration. Because of the own declaration, we can get properties also if they are protected. Now we change the property and break. If the component is not a TControl, we ask for the control parent. Any time it will be a TControl or not, so we end the while circle by break or if there is no more parent. Now we look if this component has children. If happen this, we call the procedure by our self, means recursive. Thats it.