【问题标题】:Why isn't my label displaying all the text?为什么我的标签没有显示所有文本?
【发布时间】:2014-01-20 22:45:05
【问题描述】:
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”并且它起作用了。我什至尝试过“输入来自的号码”,但它仍然没有完全显示。

【问题讨论】:

  • 如果Label.Left = 0 会发生什么?
  • @mattm 是左标签吗?
  • 是的。不确定这是否是解决方案,但似乎是第一个明显的故障排除。
  • @mattm 是的,我试图将它与文本框 (50) 对齐,但它仍然是同一件事 :(
  • @mattm 不需要演员表吗?它给了我一个错误。

标签: c# winforms label


【解决方案1】:

您可以启用AutoSize,如果您通过设计器创建它,则默认设置为true

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

【讨论】:

  • 好吧,您必须以编程方式写出属性。老实说,我从设计师那里学到了没有设计师编写的所有控件代码。这就是我制作文本框或控件数组的方式。 “你可以在 1 秒内接受答案”
【解决方案2】:

设置标签的宽度:

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

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

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

【讨论】:

  • 不设置的默认宽度是多少?
  • 我认为是 100。这就是我使用 MessageBox.Show(amountLabel.Width.ToString()); 时在我的机器上显示的内容
  • 是的,它是 100。但是我们怎么知道标签有多大呢?我的只有 24 个字符“输入一个数字”
  • 我认为反复试验是找出正确宽度的最简单方法。
【解决方案3】:

将标签的 AutoSize 属性设置为 true。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    相关资源
    最近更新 更多