Reputation: 87
How can I add these label array named tile to my form? Also the properties of actual label appearing in the form should change as i change the properties in the code. Can anybody help me out with this?
using System; using System.Collections.Generic; using
System.ComponentModel; using System.Data; using System.Drawing;
using System.Linq; using System.Text; using
System.Threading.Tasks; using System.Windows.Forms;
namespace Piano_Tiles
{
public partial class Form1 : Form
{
public Label[] tile = new Label[4];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i=0;i<4;i++)
{
tile[i] = new Label();
tile[i].Height = 200;
tile[i].Width = 100;
tile[i].Left = (i % 3) * 100;
tile[i].Top = i * 200;
tile[i].BackColor = Color.Black;
tile[i].Visible = true;
}
}
}
}
Upvotes: 0
Views: 320
Reputation: 26846
Key feature you've missed: any control you're trying to add to the form, should be also added to form's Controls
collection, otherwise it will not be displayed by form.
So just add Controls.Add(tile[i]);
at the end of your loop.
Upvotes: 1
Reputation: 2141
Here you are, you should add control to control collection of the form:
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
tile[i] = new Label();
tile[i].Height = 200;
tile[i].Width = 100;
tile[i].Left = (i % 3) * 100;
tile[i].Top = i * 200;
tile[i].BackColor = Color.Black;
tile[i].Visible = true;
Controls.Add(tile[i]);
}
}
Hope this helps.
Upvotes: 2