0

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.

Azzarian
  • 323
  • 1
  • 9
  • 1
    See [How do I make an Event in the Usercontrol and have it handled in the Main Form?](https://stackoverflow.com/q/7880850/719186) – LarsTech Mar 19 '21 at 16:22
  • 1
    see also **[ArrayList vs List<> in C#](https://stackoverflow.com/questions/2309694/arraylist-vs-list-in-c-sharp)** – Ňɏssa Pøngjǣrdenlarp Mar 19 '21 at 16:31
  • Does the Mouse click actually reach the UserControl's *surface*? Or is it *intercepted* by other child Controls? In that case, you can use the `Click` handler of the child Controls that could also be clicked to cause the same *effect* and call `this.OnClick(e);` from there (`e` should be probably rebuilt if the Mouse Pointer location matters). – Jimi Mar 19 '21 at 17:16

0 Answers0