1 (edited by tcoton 2015-10-22 07:52:42)

Topic: Mandatory field on condition

Hi all,

I am struggling finding a way to force a field to be mandatory on a condition.

I would like to set a date picker to become mandatory when a specific choice in a combobox is selected.

e.g:

My combobox Status has many choices but on choice with id = '4' , I want to force the user to set a Return date otherwise other scripts do not work properly.

Displaying a message would not suffice... we all know users wink

http://myvisualdatabase.com/forum/misc.php?action=pun_attachment&item=1414


Any idea welcome.

Post's attachments

Attachment icon forcing_date_on_condition.png 19.2 kb, 331 downloads since 2015-10-22 

Re: Mandatory field on condition

You should create event OnClick for button "Save"

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
    if (Form1.ComboBox1.dbItemID = 4) and (not Form1.DateTimePicker1.Checked) then
    begin
        ShowMessage('Date is mandatory field.');
        Form1.DateTimePicker1.SetFocus;
        Cancel := True;
    end;
end;
Dmitry.

Re: Mandatory field on condition

Thank you Dmitry, I really need to dive into Delphi scripting as it is still not that obvious to me smile

Re: Mandatory field on condition

Just a comment using version 2.1, focus on the object is not obvious at all. This is just cosmetics but a user could not spot which field is required.

Re: Mandatory field on condition

Hi Tcoton,

With mandatory fields, as well as using .setfocus to take the User back to a field they've missed, I like to set it to a different color (as a non too subtle reminder!).  So, in Dmitry's code, you'd also have

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
    if (Form1.ComboBox1.dbItemID = 4) and (not Form1.DateTimePicker1.Checked) then
    begin
        ShowMessage('Date is mandatory field.');
        Form1.DateTimePicker1.Color := clred;
        Form1.DateTimePicker1.SetFocus;
        Cancel := True;
    end;
end;

Maybe that helps.

Derek.

Re: Mandatory field on condition

Hi Derek,

thanks for the tip but unfortunately, it does not seem to work with the DateTimePicker object. It does not change color sad

Re: Mandatory field on condition

Hi Tcoton,
Maybe it's a version issue - I'm 2.0 Alpha on Windows XP(!) and it's fine with edit fields, comboboxes and datetimepickers.
Sorry about that.
Derek.

Re: Mandatory field on condition

In Windows 7 and above, can't to change background color for some components.

Dmitry.

Re: Mandatory field on condition

You can popup calendar

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
    if (Form1.ComboBox1.dbItemID = 4) and (not Form1.DateTimePicker1.Checked) then
    begin
        ShowMessage('Date is mandatory field.');
        Form1.DateTimePicker1.SetFocus;
        Form1.DateTimePicker1.OpenCalendar;
        Cancel := True;
    end;
end;
Dmitry.

Re: Mandatory field on condition

Oh great, OpenCalendar does the trick, thanks a lot.

@Derek, I am running Windows 7 & 10... smile