2014-09-19 67 views
0

我試圖從C#中的控制檯應用程序顯示WinForm(輸入框),並等待用戶關閉窗體。輸入框在打開時處於活動狀態並處於活動狀態對我來說很重要。 ShowDialog()在我的情況下不起作用,因爲在某些情況下它不會顯示爲活動表單。所以我想改變我的代碼並使用Show()。通過這種方式,我可以手動查找表單是否處於活動狀態,如果不是自己激活它。用ShowDialog()。我的代碼停止了,並且在關閉之前我什麼也做不了。從C#控制檯應用程序顯示WinFrm並等待關閉

以下是我的代碼。它顯示輸入框,但它被凍結。請問我做錯了什麼?清除「inputBox.Show();」之後的while循環沒有做任何事情,但如果我可以設法運行循環,並且輸入框不凍結,我會自己整理其餘部分。我感謝您的幫助。

public static string mInputBox(string strPrompt, string strTitle, string strDefaultResponse) 
    { 
     string strResponse = null; 
     Form inputBox = new Form(); 
     inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; 
     inputBox.ClientSize = new Size(500, 85); 
     inputBox.Text = strTitle; 
     inputBox.StartPosition = System.Windows.Forms.FormStartPosition.Manual; 
     inputBox.Left = (Screen.PrimaryScreen.Bounds.Size.Width/2) - (inputBox.ClientSize.Width/2); 
     inputBox.Top = (Screen.PrimaryScreen.Bounds.Size.Height/2) - (inputBox.ClientSize.Height/2); 

     Label lblPrompt = new Label(); 
     lblPrompt.Text = strPrompt; 
     inputBox.Controls.Add(lblPrompt); 

     TextBox textBox = new TextBox(); 
     textBox.Text = strDefaultResponse; 
     inputBox.Controls.Add(textBox); 

     Button okButton = new Button(); 
     okButton.Text = "&OK"; 
     inputBox.Controls.Add(okButton); 

     Button cancelButton = new Button(); 
     cancelButton.Text = "&Cancel"; 
     inputBox.Controls.Add(cancelButton); 

     okButton.Click += (sender, e) => 
     { 
      strResponse = textBox.Text; 
      inputBox.Close(); 
     }; 

     cancelButton.Click += (sender, e) => 
     { 
      inputBox.Close(); 
     }; 
     inputBox.AcceptButton = okButton; 
     inputBox.CancelButton = cancelButton; 

     SetWindowPos(inputBox.Handle, HWND_TOPMOST, inputBox.Left, inputBox.Top, inputBox.Width, inputBox.Height, NOACTIVATE); 

     inputBox.Show(); 

     while {true} 
      Thread.Sleep(100); 

     Application.DoEvents(); 
     return strResponse; 
    } 
+0

'而{TRUE} Thread.sleep代碼(100);'只會無限循環鎖定您的應用程序。實際上,它不會編譯......那些應該是括號,而不是花括號。 – 2014-09-19 03:05:44

+0

是真的,那麼我怎樣才能使它不鎖定工作?我應該使用另一個線程嗎?怎麼樣? – NESHOM 2014-09-19 03:08:42

回答

1

我不知道爲什麼你正在服用的這條路,我敢肯定有更好的方法來做到這一點(解釋一個在結束)。然而,爲了使你的代碼運行,你應該添加Application.DoEvents()您的循環內

代碼應該是這樣的:

 var formActive = true; 
     inputBox.FormClosed += (s, e) => formActive = false; 
     inputBox.Show(); 
     inputBox.TopMost = true; 
     inputBox.Activate(); 

     while (formActive) 
     { 
      Thread.Sleep(10); 
      Application.DoEvents(); 
     } 

,整個方法是:

public static string mInputBox(string strPrompt, string strTitle, string strDefaultResponse) 
    { 
     string strResponse = null; 
     Form inputBox = new Form(); 
     inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; 
     inputBox.ClientSize = new Size(500, 85); 
     inputBox.Text = strTitle; 
     inputBox.StartPosition = System.Windows.Forms.FormStartPosition.Manual; 
     inputBox.Left = (Screen.PrimaryScreen.Bounds.Size.Width/2) - (inputBox.ClientSize.Width/2); 
     inputBox.Top = (Screen.PrimaryScreen.Bounds.Size.Height/2) - (inputBox.ClientSize.Height/2); 

     Label lblPrompt = new Label(); 
     lblPrompt.Text = strPrompt; 
     inputBox.Controls.Add(lblPrompt); 

     TextBox textBox = new TextBox(); 
     textBox.Text = strDefaultResponse; 
     inputBox.Controls.Add(textBox); 

     Button okButton = new Button(); 
     okButton.Text = "&OK"; 
     inputBox.Controls.Add(okButton); 

     Button cancelButton = new Button(); 
     cancelButton.Text = "&Cancel"; 
     inputBox.Controls.Add(cancelButton); 

     okButton.Click += (sender, e) => 
     { 
      strResponse = textBox.Text; 
      inputBox.Close(); 
     }; 

     cancelButton.Click += (sender, e) => 
     { 
      inputBox.Close(); 
     }; 
     inputBox.AcceptButton = okButton; 
     inputBox.CancelButton = cancelButton; 


     var formActive = true; 
     inputBox.FormClosed += (s, e) => formActive = false; 
     inputBox.Show(); 
     inputBox.TopMost = true; 
     inputBox.Activate(); 

     while (formActive) 
     { 
      Thread.Sleep(10); 
      Application.DoEvents(); 
     } 

     return strResponse; 
    } 

但我認爲這將是一個更好的想法,從Form派生並創建一個InputBox並設置TopMost並致電ActivateOnLoad創建您所需的內容,然後簡單地撥打ShowDialog,是這樣的:

class Inputbox : Form 
     { 

      protected override void OnLoad(EventArgs e) 
      { 
       base.OnLoad(e); 

       TopMost = true; 
       Activate(); 
      } 
     } 

,並更好地把UI代碼的InputBox類作爲整個代碼和用法是這樣的:

class Inputbox : Form 
{ 
    public string Response { get; set; } 

    public Inputbox(string strTitle, string strPrompt, string strDefaultResponse) 
    { 
     //add UI and Controls here 

     FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; 
     ClientSize = new Size(500, 85); 
     Text = strTitle; 
     StartPosition = System.Windows.Forms.FormStartPosition.Manual; 
     Left = (Screen.PrimaryScreen.Bounds.Size.Width/2) - (ClientSize.Width/2); 
     Top = (Screen.PrimaryScreen.Bounds.Size.Height/2) - (ClientSize.Height/2); 

     Label lblPrompt = new Label(); 
     lblPrompt.Text = strPrompt; 
     Controls.Add(lblPrompt); 

     TextBox textBox = new TextBox(); 
     textBox.Text = strDefaultResponse; 
     Controls.Add(textBox); 

     Button okButton = new Button(); 
     okButton.Text = "&OK"; 
     Controls.Add(okButton); 

     Button cancelButton = new Button(); 
     cancelButton.Text = "&Cancel"; 
     Controls.Add(cancelButton); 

     okButton.Click += (sender, e) => 
     { 
      Response = textBox.Text; 
      Close(); 
     }; 

     cancelButton.Click += (sender, e) => 
     { 
      Close(); 
     }; 
     AcceptButton = okButton; 
     CancelButton = cancelButton; 
    } 

    protected override void OnLoad(EventArgs e) 
    { 
     base.OnLoad(e); 

     TopMost = true; 
     Activate(); 
    } 
} 

public static string mInputBox(string strPrompt, string strTitle, string strDefaultResponse) 
{ 
    string strResponse = null; 
    Inputbox inputBox = new Inputbox(strPrompt,strTitle,strDefaultResponse); 

    inputBox.ShowDialog(); 

    return inputBox.Response; 
} 
+0

太好了,非常感謝! – NESHOM 2014-09-19 15:32:25

0

你需要運行一個消息循環:

Application.Run(inputBox); 
+0

我在我的代碼中使用HotKeyManager類,它使用Application.Run(new MessageWindow());.所以當我使用Application.Run(inputBox);它給了我這個錯誤:在單個線程上啓動第二個消息循環不是一個有效的操作。 – NESHOM 2014-09-19 03:07:35

+0

@ M0HS3N:然後你想'ShowDialog()'。 – SLaks 2014-09-19 14:14:05

相關問題