[英]TSaveDialog file extension and [ofOverwritePromt] issue
關於TSaveDialog和Delphi的 [ofOverwritePromt]的概念已經存在一個簡單的問題,它會在保存對話框中覆蓋現有文件 。
所以我的問題/場景如下:
TSaveDialog
Options
設置了[ofOverwritePromt]
所以現在我執行程序並調用對話框。 如果我選擇WITH MOUSE或KEYBOARD(沒有輸入)的文件存在,則保存對話框要求我用消息覆蓋:
但是,如果我執行相同的操作但輸入文件名如'Test'而未指定擴展名,則保存對話框不會確認覆蓋。 我知道實際上它返回另一個文件名“C:\\ Users \\ xxx \\ Desktop \\ Test”而不是“C:\\ Users \\ xxx \\ Desktop \\ Test.pdf”。 如果對話框要求您保存文件,但是您需要鍵入擴展名,這有點不太好。所以通常我會像這樣處理它:
repeat
{ Ask for the file if not silent }
if not dlgSave.Execute then
Exit;
{ Read the filename from the save dialog }
LTempFile := dlgSave.FileName;
if not SameText(ExtractFileExt(LTempFile), '.pdf') then
begin
{ Add the extension }
LTempFile := LTempFile + '.pdf';
{ As we bypassed the overwrite check in dialog do it now }
if FileExists(LTempFile) then
if MsgWarn(Format('%s already exists. Replace?', [ExtractFileName(LTempFile)]), mbOKCancel) <> mrOk then
Continue;
end;
Break;
until False;
有沒有辦法在沒有自定義標准對話框的情況下更優雅?
我的猜測是你沒有設置DefaultExt
,這就是你返回空白擴展名的原因。 使用此屬性,您將不會遇到問題。 如果使用多個過濾器,請使用OnFilterChange
事件。 這是一種方法:
procedure TFormMain.SigSaveDialogMainTypeChange(Sender: TObject);
begin
case (Sender as TSaveDialog).FilterIndex of
0: (Sender as TSaveDialog).DefaultExt := 'pdf';
1: (Sender as TSaveDialog).DefaultExt := 'txt';
end;
end;
這也意味着您無需檢查擴展並進行更改!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.