public int dialog()
{
Form prompt = new Form(); // creates form
//dimensions
prompt.Width = 300;
prompt.Height = 125;
prompt.Text = "Adding Rows"; // title
Label amountLabel = new Label() { Left = 75, Top = 0, Text = "Enter a number" }; // label for prompt
amountLabel.Font = new Font("Microsoft Sans Serif", 9.75F);
TextBox value = new TextBox() { Left = 50, Top = 25, Width = prompt.Width / 2 }; // text box for prompt
Button confirmation = new Button() { Text = "Ok", Left = prompt.Width / 2 - 50, Width = 50, Top = 50 }; // ok button
confirmation.Click += (sender, e) => { prompt.Close(); }; // if clicked it will close
prompt.AcceptButton = confirmation; // enter
// adding the controls
prompt.Controls.Add(confirmation);
prompt.Controls.Add(amountLabel);
prompt.Controls.Add(value);
prompt.ShowDialog();
int num;
Int32.TryParse(value.Text, out num);
return num;
}
调用时我的提示是这样的
我只是点击了一个按钮来调用那个方法。现在您注意到,文本框未被选中。我该如何做到这一点,以便在调用此方法时默认选中文本框,而无需单击它或跳转到它?
(我知道这是次要的,但每个细节都会更好看)
请您参考如下方法:
用于在控件之间切换的顺序由属性 TabIndex 决定.这个属性由你添加控件的顺序自动决定(如果你不手动更改它) TabIndex = 0 的控件将聚焦在窗体的开头(当然如果控件可以聚焦)
试试
prompt.Controls.Add(value);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(amountLabel);
prompt.ShowDialog();
