簡體   English   中英

為什么我的標簽不顯示所有文本?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM