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 ComponentProgramming { public partial class ControlImage : UserControl { private event EventHandler? _avatarChanged; private event Action? _errorOccured; public string Error { get; private set; } public Image Avatar { get { return pictureBoxAvatar.Image; } set { pictureBoxAvatar.Image = value; } } public event EventHandler AvatarChanged { add { _avatarChanged += value; } remove { _avatarChanged -= value; } } public event Action AnErrorOccured { add { _errorOccured += value; } remove { _errorOccured -= value; } } public ControlImage() { InitializeComponent(); Error = string.Empty; } private void buttonLoad_Click(object sender, EventArgs e) { var ofd = new OpenFileDialog(); if(ofd.ShowDialog() == DialogResult.OK) { try { pictureBoxAvatar.Image = Image.FromFile(ofd.FileName); pictureBoxAvatar.Width = pictureBoxAvatar.Image.Width; _avatarChanged?.Invoke(this, e); } catch( Exception ex) { Error = ex.Message; _errorOccured?.Invoke(); } } } } }