在输入框中输入值时单击取消不退出程序

Clicking cancel when entering values in inputbox not exiting procedure

我在我的项目中使用输入框来获取数据以输入到数据库中。问题在于,如果用户在输入框上单击取消,它会采用输入框中的默认值。点击cancel后如何退出程序?这是我的输入框的样子:

sTeamName := inputbox('Team Name','Enter a team name','Phillies');

这里如果用户点击取消,sTeamName 将 = 'Phillies'。我无法验证默认值是否存储在变量中,因为默认值可能是用户想要输入的值。是否有 if inputbox.cancel.click 退出之类的?

您需要使用InputQuery function instead. This returns a boolean which is False iff对话框取消,并在var参数中保存输入字符串:

var
  S: string;
begin
  S := 'My New Team';
  if InputQuery('Team Name', 'Enter a team name:', S) then
    ShowMessageFmt('You entered "%s".', [S]);

if the user clicks cancel on the input box it takes the default value in the inputbox

这是设计使然,是 documented behavior:

If the user chooses the Cancel button, InputBox returns the default value. If the user chooses the OK button, InputBox returns the value in the edit box.


I can't validate that the default value is stored in the variable, because the default value is possibly what the user wants to enter.

使用空字符串作为默认值,例如:

sTeamName := InputBox('Team Name', 'Enter a team name', '');
if sTeamName <> '' then begin
  // use sTeamName as needed...
end else
begin
  // cancelled, do something else
end;

否则,使用InputQuery() instead, as shown in 。文档指出:

Use the InputBox function when there is a default value that should be used when the user chooses the Cancel button (or presses Esc) to exit the dialog. If the application needs to know whether the user chooses OK or Cancel, use the InputQuery function instead.