2015-09-09 45 views
1

我正在使用一個按鈕單擊一個輸入框來存儲一個值並對其執行一些驗證。我的問題是點擊取消按鈕它不會關閉,而是彈出信息的空檢查。任何幫助表示讚賞。謝謝輸入框中的取消按鈕沒有關閉

對於空檢查它彈出「請輸入一個值,然後再試一次」。
問題是,即使我點擊取消它顯示相同的消息,如「請輸入一個值,然後再試一次」,當我不提供任何值,並點擊確定它顯示相同的消息「請輸入一個值,然後再試一次「

procedure TForm1.Button1Click(Sender: TObject); 
var inputValue:string; 
begin 
    repeat 
    inputValue:=InputBox('Enter a value', 'value',''); 
    if (inputValue<>'') then 
    begin 
     if MessageDlg('Do You want to enter this value ' +inputValue+'?',mtInformation,[mbYes,mbNo],0)= mrYes then 
     MessageDlg('Your Value got stored', mtInformation,[mbOk],0) 
    end; 
    until(inputValue=''); 

    repeat 
    if (inputValue = '') then 
     MessageDlg('Please Enter a value and try again', mtInformation,[mbOk],0) ; 
    inputValue:=InputBox('Enter a value', 'value',''); 
    if (inputValue<>'') then 
    begin 
     if MessageDlg('Do You want to enter this value ' +inputValue+'?',mtInformation,[mbYes,mbNo],0)= mrYes then 
     MessageDlg('Your Value got stored', mtInformation,[mbOk],0) 
    end; 
    until (inputValue<>'') ; 
end; 
+0

我做了一個小編輯格式化代碼一點,但它仍然是接近不可讀。請你可以通過編輯來解決這個問題。還請說清楚「彈出信息用於空檢查」的含義。 –

+0

感謝您的編輯。請你也可以用可讀的方式格式化代碼。我們真的需要看到它嗎?僅僅顯示一個重複塊是不夠的?請多花點時間製作問題。 –

+0

爲了避免在剩餘的日子中輸入值,您可以考慮使用[間斷程序](http://docwiki.embarcadero.com/Libraries/XE8/en/System.Break)退出重複'當你的某些條件得到滿足時循環 – fantaghirocco

回答

3

的問題是,你的功能InputBox參數default爲空字符串。空字符串也是當用戶按下「取消」按鈕時函數返回的值。

如果用戶按下確定,則默認或用戶輸入的數據被存儲在返回字符串 中,否則返回空字符串。

在這種情況下,您無法確定它來自何處(從Ok或取消)。

我推薦使用InputQuery代替InputBox。

喜歡的東西:

var 
    InputValue : string; 
    CustIsPressOK : boolean; 
begin 

    repeat 
    CustIsPressOK := InputQuery('Enter a value', 'value',InputValue); 

    if CustIsPressOK then 
     begin 
     if InputValue <> '' then 
      begin 
      if MessageDlg('Do You want to enter this value ' +inputValue+'?',mtInformation,[mbYes,mbNo],0)= mrYes then 
       MessageDlg('Your Value got stored', mtInformation,[mbOk],0); 
      end 
     else 
      begin 
      //user is pressed OK button but value is empty string 
      MessageDlg('Please Enter a value and try again', mtInformation,[mbOk],0) ; 
      end; 
     end; 
    until (CustIsPressOK = false) or (CustIsPressOK and (InputValue <> '')); 
相關問題