Actually there is a code where PictureBox is created in a loop.

foreach (var pars in parsing) { var pn = new Panel(); pn.Size = new Size(166, 260); flow.Controls.Add(pn); var lb = new Label(); lb.Text = parsing[i].NameFilm; lb.Dock = DockStyle.Bottom; lb.Font = new Font("Comic Sans MS", 9.0f); lb.ForeColor = ColorTranslator.FromHtml("#FFFFFF"); pn.Controls.Add(lb); var hd = new Label(); hd.Text = parsing[i].Quality; hd.Location = new Point (0,0); hd.Font = new Font("Comic Sans MS", 9.0f); hd.ForeColor = ColorTranslator.FromHtml("#FFFFFF"); //#c70016 hd.BackColor = ColorTranslator.FromHtml("#c70016"); hd.Padding = new Padding(5,2,5,2); hd.AutoSize = true; pn.Controls.Add(hd); var pb = new PictureBox(); pb.SizeMode = PictureBoxSizeMode.StretchImage; pb.Dock = DockStyle.Top; pb.Name = "pb" + i.ToString(); pb.Size = new Size(166, 246); pb.LoadAsync(parsing[i].ImageURl); pb.MouseClick += new MouseEventHandler((o, a) => Start(parsing[i].UrlFilm)); pn.Controls.Add(pb); i++; } 

Everything works fine except
pb.MouseClick + = new MouseEventHandler ((o, a) => Start (parsing [i] .UrlFilm)); When compiling, it gives an error that "Index is out of range".

If instead of i write [i-1]

 pb.MouseClick += new MouseEventHandler((o, a) => Start(parsing[i-1].UrlFilm)); 

Then when you click on any pictureBox will open the last, in this case, the film.

How to make so that everyone pictureBox was appropriated the MouseEventHandler?

  • one
    try replacing parsing[i-1] with pars - Monomax
  • in general, I would create a new class inherited from the Panel accepts your pars in the constructor. - Monomax
  • @Monomax Thanks, eyes zamylil..pb.MouseClick + = new MouseEventHandler ((o, a) => Start (pars.UrlFilm)); - SaDLiF
  • @Monomax This is how it works. - SaDLiF
  • Can I make my own version as an answer? - Monomax

1 answer 1

First of all, you need to create a class that is inherited from the Panel, not use the OOP language and not use the OOP:

  class FilmPanel:Panel { public FilmPanel(ClassPars Pars, int PN) { Size = new Size(166, 260); Controls.Add(new Label{ Text = Pars.NameFilm, Dock = DockStyle.Bottom, Font = new Font("Comic Sans MS", 9.0f), ForeColor = ColorTranslator.FromHtml("#FFFFFF"), }); Controls.Add(new Label{ Text = Pars.Quality, Location = new Point (0,0), ForeColor = ColorTranslator.FromHtml("#FFFFFF"), BackColor = ColorTranslator.FromHtml("#c70016"), Padding = new Padding(5,2,5,2), AutoSize = true }); Controls.Add(FPB(Pars, PN)); } PictureBox FPB((ClassPars Pars, int pn); { var res = new PictureBox{ SizeMode = PictureBoxSizeMode.StretchImage, Dock = DockStyle.Top, Name = "pb" + pn, Size = new Size(166, 246) }; res.LoadAsync(Pars.ImageURl); res.MouseClick += new MouseEventHandler((o, a) => Start(Pars.UrlFilm)); return res; } } 

and further in the code the following is written:

  int i=0; foreach (var pars in parsing) { flow.Controls.Add(new FilmPanel(pars,i)); i++; } 

Now the code is convenient to read, and easy to edit!

  • @SaDLiF hope you like my idea. - Monomax
  • in general, everything works, again except res.MouseClick += new MouseEventHandler((o, a) => Start(Pars.UrlFilm)); The point is that the Start method is in the form class. Even if you give an instance of the Form1 form1 = new Form1(); class, Form1 form1 = new Form1(); and use res.MouseClick += new MouseEventHandler((o, a) => form1.Start(Pars.UrlFilm)); Then when you click the mouse .. nothing happens. Maybe I'm a fool and don't catch up with something ") - SaDLiF
  • @SaDLiF is still too early to state death, the patient is surely alive! It's simple, you need the Start method, either move it to the panel, or create EventHandler as a field in the panel. - Monomax
  • The fact is that the Start method works directly with the form for me. Even if I add a method to the panel, I still need to refer to the form from this method. Actually, this is the problem ") How to do something for good?") - SaDLiF