我试图从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