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]);
}
}
}
Labelについて [C#]
Labelについて [C#]
このようにしてもLabelが1つしか出てこないのはなぜでしょうか?
Re: Labelについて [C#]
どうやって,
手元の環境では,LabelのHeightのデフォルトは23pixelだったので,単純に重なって下のLabelが見えていないだけだと思いますが。
---- お試し ----
diff
ことを確認しましたか。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);
}
}
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);
}
}
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}";