为什么我的标签不显示所有文字?

时间:2014-01-20 01:38:11

标签: c# winforms label

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);

My Prompt

问题是它只会显示“输入数字”。由于某种原因,它不会显示全文。我尝试了较短的“E”并且有效。我甚至试过“输入一个数字”,但它仍然没有完全显示。

3 个答案:

答案 0 :(得分:5)

您可以启用AutoSize,默认设置为true,如果您是通过设计师创建的话:

Label amountLabel
    = new Label { AutoSize = true, Left = 50, Top = 0, Text = "Enter a number from 1-50" };

答案 1 :(得分:1)

设置标签的宽度:

Label amountLabel = new Label() { Left = 75, Top = 0, Width = 1000, Text = "Enter a number from 1-50" };

不要宽度 宽。我只是想确保文本适合而不测试不同的值。

我很惊讶它没有自动调整宽度。

答案 2 :(得分:0)

将Label的AutoSize属性设置为true。

Label amountLabel = new Label() { AutoSize=true, Left = 50, Top = 0, Text = "Enter a number from 1-50" };