2011-09-28 75 views
2

我想創建一個對話窗口,用戶可以在其中執行各種任務,並希望他通過用鼠標單擊Cancel按鈕(即不按Enter)從對話框返回。因此我不想使用CreateDialog。但是,通過CreateWindow創建一個不太明確的對話框窗口,所有字符串都顯示爲未格式化。對話框操作

expr = Column[{ 
    [email protected]{"set variable to: ", InputField["value", String]}, 
    "Try to hit Enter in any of the dialogs: it closes #2 but not #1.", 
    CancelButton[] 
    }]; 

CreateWindow[DialogNotebook[expr], WindowSize -> All, WindowMargins -> {{100, Automatic}, {Automatic, Automatic}}, WindowTitle -> "1. CreateWindow & DialogNotebook"]; 
CreateDialog[expr, WindowTitle -> "2. CreateDialog"]; 

dialog windows

有沒有什麼聰明的辦法有第二對話窗口的外觀,但第一個的按鈕的行爲?當然,expr這裏是一個簡單的例子,但它在現實中可能相當複雜,因此它不可能將每個字符串包裝到Cell[string, "Text"]中,而將每個其他表達式包裝到一些不太明確的boxform中。

+1

「取消」按鈕通常用於取消(即忽略)您的輸入... –

回答

6

被按下時,輸入這會阻止你的對話窗口關閉:

CreateDialog[expr, WindowTitle -> "2. CreateDialog", NotebookEventActions -> {}]; 

它覆蓋默認對話框NotebookEventActions。

2

也許使用TextCell

expr = Column[{[email protected]{[email protected]"set variable to: ", 
        InputField["value", String]}, 
       [email protected]"Try to hit Enter in any of the dialogs: \ 
         it closes #2 but not #1.", 
       CancelButton[]}]; 

CreateWindow[ 
DialogNotebook[expr], WindowSize -> All, 
WindowMargins -> {{100, Automatic}, {Automatic, Automatic}}, 
WindowTitle -> "1. CreateWindow & DialogNotebook"] 

編輯

使用

[email protected][" ... blah blah ...", style_opt ] 

進行格式化。

+0

我已經嘗試過這種方法,但它要求每個表達式都包裝在TextCell或Style中,這有點乏味當你有一個複雜的對話窗口。另外,正如我上面提到的拉格菲爾德的解決方案,人們必須找出重現對話的默認樣式,這不是一項簡單的任務。 –

2

另一種選擇:

expr = Style[ 
    Column[{[email protected]{"set variable to: ", InputField["value", String]}, 
    "Try to hit Enter in any of the dialogs: it closes #2 but not \ 
#1.", CancelButton[]}], ShowStringCharacters -> False]; 
+0

這很有用,我總是忘記這個選項,謝謝!但是在這種情況下,我必須手動設置字體樣式選項以匹配使用CreateDialog創建的其他對話框元素的默認樣式。 –

1

有許多方法可以做到這一點,和其他人已經發布了兩個好的,但在我看來,最簡單的方法是將Column表達BaseStyle設爲匹配對話框的基礎樣式,然後使用CreateWindow。有問題的風格是"Panel",所以這讓你你想要的結果:

expr = Column[{[email protected]{"set variable to: ", InputField["value", String]}, 
    "Try to hit Enter in any of the dialogs: it closes #2 but not #1.", 
    CancelButton[]}, BaseStyle -> "Panel"]; 

CreateWindow[DialogNotebook[expr], WindowSize -> All, 
    WindowMargins -> {{100, Automatic}, {Automatic, Automatic}}, 
    WindowTitle -> "1. CreateWindow & DialogNotebook"]; 
+0

幾乎完美的解決方案,但沒有雪茄:在我的機器上,該列在對話窗口的灰色背景前面出現白色背景。再次,我必須手動設置正確的GrayLevel,因爲甚至背景 - >無似乎適用於列。 –