【问题标题】:Creating an Array of Labels C#创建标签数组 C#
【发布时间】:2013-10-09 03:02:30
【问题描述】:

我知道这里有很多关于 C# 中的标签数组的问题,但我的问题与标签的外观有关,而不是如何创建标签数组。

这是我的一些代码(忽略所有静态值):

for (int i = 0; i < 10; i++)
            {
                if (i % 2 == 0)
                {
                    newsTitle = GetTagData(rootRss, "title", i + 2);
                    rssFeeds[i].Location = new Point(xPt, yPt);
                    rssFeeds[i].Font = new Font(rssFeeds[i].Font, FontStyle.Bold);
                    rssFeeds[i].Text = "\tTEST" + i + newsTitle;
                }
                else
                {
                    newsDesc = GetTagData(rootRss, "description", i + 1);
                    rssFeeds[i].Location = new Point(xPt, yPt + 25);
                    rssFeeds[i].Font = new Font(rssFeeds[i].Font, FontStyle.Regular);
                    rssFeeds[i].Text = newsDesc;
                    yPt += 75;
                }
                //rssFeeds[i].MaximumSize = new Size(400, 400);
                this.Controls.Add(rssFeeds[i]);
            }

我已经在全局范围内创建了 rssFeeds 标签,并在 Form.Load() 中使用 for 循环创建了所有标签 rssFeeds[i] = new Label();

但是,当我运行并查看表单时,它看起来像这样:

'This is the f'
'This is the s'
'This is the t'
'This is the f'
.
.
.

而不是这样:

'This is the first line of data from the news headline today'
'This is the second line of data from the news headline today'
'This is the thrid line of data from the news headline today'
'This is the fourth line of data from the news headline today'
.
.
.

我在标签属性中弄乱了MaximumSize 格式,但我可能做错了(在上面的代码中注释掉了)。我知道这应该可行,因为如果我将文本输出到消息框,它会显示整行文本。

我是否遗漏了一些愚蠢的东西,还是在创建我需要知道的标签数组之后还有更多内容?

【问题讨论】:

  • 标签的宽度小于字符串的大小。

标签: c# arrays labels


【解决方案1】:

设置rssFeeds[i].AutoSize = true;

【讨论】:

  • 谢谢,我认为默认情况下我应该考虑到这一点。长时间盯着代码让我变笨了。
【解决方案2】:

您的所有标签似乎都具有您未定义的相同默认宽度,并且宽度不足以容纳文本。因此,您需要确保它们具有正确的高度和宽度,以适合您希望它们显示的信息。

【讨论】:

  • 两个答案都是我一直在寻找的我只是选择了自动调整大小,因为我不知道我需要标签的宽度。谢谢。
猜你喜欢
  • 2011-06-27
  • 2019-10-17
  • 1970-01-01
  • 2019-09-20
  • 1970-01-01
  • 2019-08-02
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
相关资源
最近更新 更多