我試圖從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;
}
'而{TRUE} Thread.sleep代碼(100);'只會無限循環鎖定您的應用程序。實際上,它不會編譯......那些應該是括號,而不是花括號。 – 2014-09-19 03:05:44
是真的,那麼我怎樣才能使它不鎖定工作?我應該使用另一個線程嗎?怎麼樣? – NESHOM 2014-09-19 03:08:42