Labelについて [C#]

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
ryouto310

Labelについて [C#]

#1

投稿記事 by ryouto310 » 9年前

このようにしてもLabelが1つしか出てこないのはなぜでしょうか?

コード:

public partial class Form1 : Form
{
    private Label[] label1 = new Label[10];

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 10; i++)
        {
            this.label1[i] = new Label();
            this.label1[i].Text = "Hello world  " + i;
            this.label1[i].Location = new Point(0, i * 10);

            Console.WriteLine(i);

            Controls.Add(this.label1[i]);
        }
    }
}

YuO
記事: 947
登録日時: 14年前
住所: 東京都世田谷区

Re: Labelについて [C#]

#2

投稿記事 by YuO » 9年前

どうやって,
ryouto310 さんが書きました:Labelが1つしか出てこない
ことを確認しましたか。
手元の環境では,LabelのHeightのデフォルトは23pixelだったので,単純に重なって下のLabelが見えていないだけだと思いますが。

---- お試し ----

コード:

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var form = new Form();
        form.Load += (sender, __) =>
        {
            var f = (Form)sender;
            f.Controls.AddRange(Enumerable.Range(1, 10).Select(i => new Label
            {
                Text = $"Hello world {i}",
                Location = new Point(0, i * 10),
                BackColor = Color.FromArgb((i & 1) * 0xFF, ((i / 2) & 1) * 0xFF, ((i / 4) & 1) * 0xFF),
            }).ToArray());
            f.Text = $"{f.Controls.Cast<Control>().First().Height}";
        };
        Application.Run(form);
    }
}
2016-05-07.png
2016-05-07.png (2.5 KiB) 閲覧数: 1637 回

コード:

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var form = new Form();
        form.Load += (sender, __) =>
        {
            var f = (Form)sender;
            f.Controls.AddRange(Enumerable.Range(1, 10).Select(i => new Label
            {
                Text = $"Hello world {i}",
                Location = new Point(0, i * 25),
                BackColor = Color.FromArgb((i & 1) * 0xFF, ((i / 2) & 1) * 0xFF, ((i / 4) & 1) * 0xFF),
            }).ToArray());
            f.Text = $"{f.Controls.Cast<Control>().First().Height}";
        };
        Application.Run(form);
    }
}
2016-05-07 (1).png
2016-05-07 (1).png (4.33 KiB) 閲覧数: 1637 回
diff

コード:

diff --git a/WindowsFormsApplication1/Program.cs b/WindowsFormsApplication1/Program.cs
index 980e7b0..928be0a 100644
--- a/WindowsFormsApplication1/Program.cs
+++ b/WindowsFormsApplication1/Program.cs
@@ -17,7 +17,7 @@ static class Program
             f.Controls.AddRange(Enumerable.Range(1, 10).Select(i => new Label
             {
                 Text = $"Hello world {i}",
-                Location = new Point(0, i * 10),
+                Location = new Point(0, i * 25),
                 BackColor = Color.FromArgb((i & 1) * 0xFF, ((i / 2) & 1) * 0xFF, ((i / 4) & 1) * 0xFF),
             }).ToArray());
             f.Text = $"{f.Controls.Cast<Control>().First().Height}";

ryouto310

Re: Labelについて [C#]

#3

投稿記事 by ryouto310 » 9年前

ありがとうございました!
無事表示されました。
LabelのデフォルトのHeightなんて考えてもいませんでした...^^;

閉鎖

“C言語何でも質問掲示板” へ戻る