I want to create a C# Windows Form application where there is a flow layout panel, filled with User Control elements. Every time I click an element, the some labels must show its attributes. However, the click event doesn't register. I added some breakpoints on the show_preview() method and it never calls the method. The Load event is calling show_preview but I want to do change the labels acordinging to a User Control being clicked.
This is the Form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
load_plots();
}
private void load_plots()
{
ArrayList items = new ArrayList();
for (int i = 0;i<100;i++)
{
PlotItem plot = new PlotItem();
plot.Place = "Ilion";
plot.Price = i * 1000;
plot.Space = i * 10;
plot.Title = "Plot No." + i;
plot.Width = flowLayoutPanel1.Width - 30;
plot.Click += new EventHandler(this.show_preview);
items.Add(plot);
flowLayoutPanel1.Controls.Add(plot);
}
}
private void show_preview(object sender, EventArgs e)
{
PlotItem clicked = sender as PlotItem;
plot_title.Text = "Hello";
plot_size.Text = clicked.Space.ToString();
plot_value.Text = clicked.Price.ToString();
}
This is the Plot Item:
public partial class PlotItem : UserControl
{
public PlotItem()
{
InitializeComponent();
}
#region Properties
[Category("Plot Object")]
private String _place;
public String Place
{
get { return _place; }
set { _place = value; }
}
[Category("Plot Object")]
private double _price;
public double Price
{
get { return _price; }
set { _price = value; label_price.Text = value.ToString() + "€"; }
}
[Category("Plot Object")]
private String _use;
public String Use
{
get { return _use; }
set { _use = value; }
}
[Category("Plot Object")]
private double _space;
public double Space
{
get { return _space; }
set { _space = value; label_size.Text = value.ToString() + "m2"; }
}
[Category("Plot Object")]
private String _title;
public String Title
{
get { return _title; }
set { _title = value; label_title.Text = value; }
}
[Category("Plot Object")]
private Image _icon;
public Image Icon
{
get { return _icon; }
set { _icon = value; plot_preview.Image = value; }
}
#endregion
private void panel1_MouseEnter(object sender, EventArgs e)
{
this.BackColor = Color.Silver;
}
private void panel1_MouseLeave(object sender, EventArgs e)
{
this.BackColor = System.Drawing.SystemColors.Control;
}
Thanks for your time.