【发布时间】:2014-07-13 09:34:38
【问题描述】:
我根据this改编了一个自己滚动的输入框。
我将代码修改为:
using System;
using System.Windows.Forms;
public static class PromptForText
{
public static string ShowDialog(string caption, string text)
{
Form prompt = new Form();
prompt.Width = 280;
prompt.Height = 150;
prompt.Text = caption;
Label textLabel = new Label() { Left = 16, Top = 20, Width = 240, Text = text };
TextBox textBox = new TextBox() { Left = 16, Top = 40, Width = 240 };
Button confirmation = new Button() { Text = "Okie Doak", Left = 16, Width = 80, Top = 72 };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(textBox);
prompt.StartPosition = FormStartPosition.CenterScreen;
prompt.ShowDialog();
textBox.Focus();
return textBox.Text;
}
}
我添加了“textBox.Focus()”,但它并没有达到我的预期。我在调用 ShowDialog() 之前和之后都尝试过。
我错过了什么?为什么没有在文本框上调用焦点将焦点设置为相同?
更新
现在根据史蒂夫(沃兹尼亚克的?)的回答创建控件:
TextBox textBox = new TextBox() { Left = 16, Top = 40, Width = 240, TabIndex = 0, TabStop = true };
Label textLabel = new Label() { Left = 16, Top = 20, Width = 240, Text = text };
Button confirmation = new Button() { Text = "Okie Doak", Left = 16, Width = 80, Top = 72, TabIndex = 1, TabStop = true };
【问题讨论】:
标签: c# dynamic focus windows-forms-designer