ページ 1 / 1
Labelについて [C#]
Posted: 2016年5月07日(土) 00:52
by ryouto310
このようにしても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]);
}
}
}
Re: Labelについて [C#]
Posted: 2016年5月07日(土) 01:36
by YuO
どうやって,
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 (2.5 KiB) 閲覧数: 1641 回
コード:
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 (4.33 KiB) 閲覧数: 1641 回
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}";
Re: Labelについて [C#]
Posted: 2016年5月08日(日) 02:22
by ryouto310
ありがとうございました!
無事表示されました。
LabelのデフォルトのHeightなんて考えてもいませんでした...^^;