gpt4 book ai didi

c# - Windows C# 窗体 : Prompt focus on a textbox

转载 作者:行者123 更新时间:2023-12-02 05:14:18 25 4
gpt4 key购买 nike

我想知道在 Windows 窗体上使用提示时如何自动选择文本框。我下面的代码显示了我的尝试,但它仍然专注于按钮而不是文本框。提前感谢您的帮助和帮助。

            Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 200;
prompt.Text = caption;
Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
Button confirmation = new Button() { Text = "Ok", Left = 50, Width = 100, Top = 90 };
confirmation.Click += (sender, e) => { prompt.Close(); };
textBox.Select();
textBox.Focus();
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(textBox);
prompt.ShowDialog();
return textBox.Text;

最佳答案

您需要等到显示表单后才能聚焦文本框。在表单第一次显示之前,它无法聚焦任何东西。您可以使用 Shown 事件在表单首次显示后执行一些代码。

string text = "Text";
string caption = "caption";
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 200;
prompt.Text = caption;
Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
Button confirmation = new Button() { Text = "Ok", Left = 50, Width = 100, Top = 90 };
confirmation.Click += (s, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(textBox);
prompt.Shown += (s, e) => textBox.Focus();
prompt.ShowDialog();
return textBox.Text;

关于c# - Windows C# 窗体 : Prompt focus on a textbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14902591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com