Emmanuel Lamy wrote: > Thanks, Vahan! Thanks, David! Thanks, Rob! Thank you all for your > suggestion. I think I have picked a lousy example like David said. > One of the actions I want to accomplish is to change tha shade of > each one of a multitude of edit controls on a form, every time it > receives focus. But instead of coding the "OnEnter" event of each > individual control, I am trying to find a way to iterate through all > the controls at runtime and define the behavior wanted, with a > routine like this: > > .. for i:=0 to ControlCount-1 do > if Controls[i] is TEdit then > begin > (Controls[i] as TEDit).Color:=clWhite; > (Controls[i] as TEdit).OnEnter:=(Controls[i] asTEdit).Color:=clCream; > (Controls[i] as TEdit).OnExit:=(Controls[i] as Tedit).Color:=clWhite; > end; > > I am not saying that this the proper approach. It's just an > illustration to show what I am trying to accomplish. So, you can > point me to the right direction. TIA.
The OnEnter property needs to have a pointer to a method taking one TObject parameter. It would be really cool if we could define a two-argument function like this: procedure TForm1.HandleFocusColor(Sender: TObject; Color: TColor); begin (Sender as TEdit).Color := Color; end; Then, when we set the OnEnter property of all the items in the Controls array, we could tell Delphi to call the HandleFocusColor method with a particular value already supplied for the second parameter, so when the edit box calls it's OnEnter handler, it provides the single remaining, just as it's already defined to do. Maybe something like this: Edit := TEdit(Controls[i]); Edit.OnEnter := HandleFocusColor(var, clCream); Edit.OnExit := HandleFocusColor(var, clWindow); This technique is know as "currying," after Haskell Curry. It would take a two-argument function and turn it into a one-argument function by binding one of the parameters in advance. Alas, Delphi doesn't support this. It's possible to generate functions at run time, but in your case I think it's much more trouble than it's worth. When Delphi supports .Net 2, we might be able to accomplish this using anonymous methods, also known as lambda functions. For your immediate needs, you don't need anything nearly so complicated as anything I've described above. Since you're assigning constant colors, you can simply define two functions: procedure TForm1.HandleOnEnter(Sender: TObject); begin HandleFocusColor(Sender, clCream); end; procedure TForm1.HandleOnExit(Sender: TObject); begin HandleFocusColor(Sender, clWindow); end; Then, assign those to the event handlers. If all your edit controls are already on your form now, at design time, then set the OnEnter and OnExit properties in the Object Inspector as I described in my previous message. Otherwise, assign them in your loop: Edit := TEdit(Controls[i]); Edit.OnEnter := HandleOnEnter; Edit.OnExit := HandleOnExit; -- Rob ----------------------------------------------------- Home page: http://groups.yahoo.com/group/delphi-en/ To unsubscribe: [EMAIL PROTECTED] Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/delphi-en/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/