0

I need to display the picturebox that is formed in the main form in the usercontrol, but also save subscriptions to this picturebox. In this class, I fill in the fields and then use foreach to pass the picturebox to the usercontrol

public class UserCtr
{
    public event Action<List<Users>> ClickPicture;
    public event Action<List<Users>, Group> DoubleClickPicture;
    public Group group { get; set; }
    public PictureBox picture { get; set; }
    public List<Users> users { get; set; }

    public UserCtr(Group group, PictureBox picture, List<Users> users)
    {
        this.group = group;
        this.picture = picture;
        this.users = users;
        this.picture.Click += PictureClick;
        this.picture.MouseDoubleClick += PictureDoubleClick;
    }
    private void PictureClick(object s, EventArgs e)
    {
        ClickPicture(users);
    }
    private void PictureDoubleClick(object s, EventArgs e)
    {
        DoubleClickPicture(users, group);
    }

}

foreach loop

foreach (var item in groups.users)
        {
            item.picture.Image = yellow;
            panel.Controls.Add(new UserControl1(item.picture)
            {
                Text = item.user.Name,
                //pb = item.picture
        });
        }

My usercontrol

   public partial class UserControl1 : UserControl
{
    public UserControl1(PictureBox pic)
    {

        InitializeComponent();
        Load += (s, e) => pictureBox1 = pic;
        Invalidate();
    }
    protected override void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
        label1.Text = Text;
    }

}

}

The label is filled in, and the picturebox is empty

Domex
  • 25
  • 5
  • Where is the code to set an image? - To subscribe the PBs to events use the same lambda syntax as you do with the Load event – TaW Apr 25 '21 at 13:07
  • Filling in the picturebox from another class. It implements a method that returns a picturebox. Then, using foreach, I upload the image to picturebox. – Domex Apr 25 '21 at 13:18
  • Something like this, foreach (var item in GetUserIntoPoint()) { item.Image = green_crcle; } – Domex Apr 25 '21 at 13:19
  • OK, what is the question then? doesn't that code work? – TaW Apr 25 '21 at 13:35
  • My picturebox remains empty. – Domex Apr 25 '21 at 13:38
  • I can just draw a picture. If I override the backgroundimagechange. But then the click subscription will not be saved. – Domex Apr 25 '21 at 13:42
  • Do use the debugger to see what happens in detail!! How many items in the loop? As expected? Can you set a BackColor instead ? etc.. - You may have two sets of pboxes by mistake. – TaW Apr 25 '21 at 13:42
  • Everything is fine in the debugger, all the elements are created. But the picturebox from the usercontrol is not replaced by the picturebox from the form – Domex Apr 25 '21 at 13:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231580/discussion-between-taw-and-domex). – TaW Apr 25 '21 at 15:04

0 Answers0