[英]Why isn't my label displaying all the text?
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;
}
这是完整的代码。 该代码的简短版本为:
Label amountLabel = new Label() { Left = 50, Top = 0, Text = "Enter a number from 1-50" };
prompt.Controls.Add(amountLabel);
问题是它最多只能显示“输入号码”。 由于某种原因,它不会显示全文。 我尝试使用更短的“ E”,它奏效了。 我什至尝试过“输入一个来自的号码”,但是它仍然没有完全显示出来。
您可以启用AutoSize
,如果通过设计器创建,则默认设置为true
:
Label amountLabel
= new Label { AutoSize = true, Left = 50, Top = 0, Text = "Enter a number from 1-50" };
设置标签的宽度:
Label amountLabel = new Label() { Left = 75, Top = 0, Width = 1000, Text = "Enter a number from 1-50" };
但是,不要使宽度那么宽。 我只是想确保文本适合而无需测试其他值。
我很惊讶它没有自动调整宽度。
将标签的AutoSize属性设置为true。
Label amountLabel = new Label() { AutoSize=true, Left = 50, Top = 0, Text = "Enter a number from 1-50" };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.