In my project a painfull & time consuming process is the ".setFocus" in case a validation fails.
I will try to explain what i mean...
All the time i write validations (usually on BeforePost) that may fire a MessageDlg or raise an exception, and in case of validation failure i want to focus to the related control, where user can edit it's value and retry...
The validations are performed usually with TFields from TSQLQuery instead of the db controls (yes i use db controls).
Now when a validation fails, and before throwing in the user's face the corresponding error message i try to call the setFocus of the related control (if any exists).
One problem that arrises is to ensure that the control can be focused (is it enabled? or worst ... is it visible ?) and the complexity of this depends on the form's design.
And my case is suffering little from this.
All my forms have 2 PageControls with mutliple tabSheets, so i need also to check page controls and tabsheet if they are visible, enabled in order to select them etc etc...
What i did for this till now...
I wrote a general focusField(f:TField) virtual method in a base form class and i use it all the time....
Now in every descedant form, i must override this and for every field in my form i try to find the related control and set the focus on it.
take an example of what i mean...
procedure TfrmTranTypes.focusField(f: TField);
begin
if pgc.Enabled and pgc.Visible
and tabRecord.Enabled and tabRecord.tabVisible then begin
pgc.ActivePage:=tabRecord;
if (edt_tran_type.Field=f)
and edt_tran_type.Enabled
and edt_tran_type.Visible then
edt_tran_type.SetFocus
else if (edt_tran_type_label.Field=f)
and edt_tran_type_label.Enabled
and edt_tran_type_label.Visible then
edt_tran_type_label.SetFocus
else if (edt_tran_type_desc.Field=f)
and edt_tran_type_desc.Enabled
and edt_tran_type_desc.Visible
and pgcDetails.Enabled
and pgcDetails.Visible
and tab_tran_type_desc.Enabled
and tab_tran_type_desc.tabVisible then begin
pgcDetails.ActivePage:=tab_tran_type_desc;
edt_tran_type_desc.SetFocus
end else if (chk_tran_type_hardcoded.Field=f)
and chk_tran_type_hardcoded.Enabled
and chk_tran_type_hardcoded.Visible then
chk_tran_type_hardcoded.SetFocus
else if (chk_tran_type_media.Field=f)
and chk_tran_type_media.Enabled
and chk_tran_type_media.Visible then
chk_tran_type_media.SetFocus
else if (chk_tran_type_member.Field=f)
and chk_tran_type_member.Enabled
and chk_tran_type_member.Visible then
chk_tran_type_member.SetFocus
end;
end;
One thing that would help this (not sure, only speculate) is to have in TFields a property where i can set the related control in design time or in runtime....
One other is a recursive routine where you give the control and tries to check if it is focus able, or should i just simple call setFocus in a try except ?
currently i must leave with this for my project (time pressure), but i would like to hear also other thoughts ...