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 = 50, Top = 0, Text = "Enter a number from 1-50" }; // 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
prompt.KeyPreview = true;
prompt.KeyDown += (sender, e) =>
{
if (e.KeyCode == Keys.Escape) prompt.DialogResult = DialogResult.Cancel; // user presses ESC key to close
};
// adding the controls
prompt.Controls.Add(value);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(amountLabel);
prompt.ShowDialog();
// returning and checking if int block
int num;
Int32.TryParse(value.Text, out num);
return num;
}
Run Code Online (Sandbox Code Playgroud)
这是完整的代码.该代码的简短版本是:
Label amountLabel = new Label() { Left = 50, Top = 0, Text = "Enter a number from 1-50" };
prompt.Controls.Add(amountLabel);
Run Code Online (Sandbox Code Playgroud)

问题是它只会显示"输入一个数字".由于某种原因,它不会显示全文.我尝试了较短的"E"并且有效.我甚至试过"输入一个数字"但它仍然没有完全显示.
您可以启用AutoSize,true如果您通过设计器创建它,则默认设置为:
Label amountLabel
= new Label { AutoSize = true, Left = 50, Top = 0, Text = "Enter a number from 1-50" };
Run Code Online (Sandbox Code Playgroud)