【问题标题】:Insert long text to a label in runtime在运行时将长文本插入标签
【发布时间】:2013-12-19 09:54:36
【问题描述】:

我正在c#中创建一个标签(随机文本,仅作为示例):

Label lblText = new Label();
lblText.Text = "A computer is a general purpose device that can be programmed to carry out a set of arithmetic or logical operations. Since a sequence of operations can be readily changed, the computer can solve more than one kind of problem.";
lblText.Location = new Point(48, 95);

但是当它显示时我只能看到:“A computer is a”

如何显示整个文本?

编辑: AutoSize 工作,但它超出了窗口的边界,有没有像“autoNewLine”这样的东西?将文本保留在窗口内

【问题讨论】:

标签: c# visual-studio-2010 text label


【解决方案1】:

看看Label.AutoSize

获取或设置一个值,该值指示控件是否自动调整大小以显示其全部内容。 [...] 使用设计器添加到表单时,默认值为 true。 从代码中实例化时,默认值为 false

如果您还想进行自动换行,请查看this question

引用John Gietzen的回答:

如果您将标签设置为 AutoSize,它会随着您放入的任何文本自动增长。 (这包括垂直增长。) 如果要使其以特定宽度自动换行,可以设置 MaximumSize 属性。

myLabel.MaximumSize = new Size(100, 0);
myLabel.AutoSize = true;

【讨论】:

    【解决方案2】:

    您需要将此属性 lblText.AutoSize = True; 设置为 true,因为它是在运行时创建的,并且默认值为 false

       Label lblText = new Label();    
        lblText.AutoSize = True;
        lblText.Text = "A computer is a general purpose device that can be programmed to carry out a set of arithmetic or logical operations. Since a sequence of operations can be readily changed, the computer can solve more than one kind of problem.";
        lblText.Location = new Point(48, 95);
    

    【讨论】:

      【解决方案3】:

      那是因为你的TextBoxWidth 属性不够高。增加TextBoxWidth,您的其余文本将自动出现。

      您还可以通过将 lblText.AutoSize 属性设置为 true 来使 TextBox 自动调整为正确的宽度。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-30
        • 1970-01-01
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多