1

I have a List in my custom user control. I'd like the control to redraw the each Image in the List whenever the contents of that list is changed. Either a movement, addition or removal of an items should fire an event.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebServiceScanner
{
    public partial class imageList : UserControl
    {
        public imageList()
        {
            InitializeComponent();
        }

        public List<Image> Images { get; set; }

        public void AddImage(Image image)
        {
            Images.Add(image);
        }

        public void RemoveImage(Image image)
        {
            Images.Remove(image);
        }

        public void MoveImageLeft(int index)
        {
            Image tmpImage = Images[index];
            Images[index] = Images[index - 1];
            Images[index - 1] = tmpImage;
        }

        public void MoveImageLeft(int index)
        {
            Image tmpImage = Images[index];
            Images[index] = Images[index + 1];
            Images[index + 1] = tmpImage;
        }
    }
}

Can this be done?

Thanks for your guidance! Eager to learn!

3 Answers3

4

You can use an ObservableCollection<T> instead of a List<T> and handle its CollectionChanged event.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
0
public partial class imageList : UserControl
    {

        public event OnChange;

        public imageList()
        {
            InitializeComponent();
        }

        public List<Image> Images { get; set; }

        public void AddImage(Image image)
        {
            Images.Add(image);
            this.OnChange();
        }

        public void RemoveImage(Image image)
        {
            Images.Remove(image);
            this.OnChange();
        }

        public void MoveImageLeft(int index)
        {
            Image tmpImage = Images[index];
            Images[index] = Images[index - 1];
            Images[index - 1] = tmpImage;
            this.OnChange();
        }

        public void MoveImageLeft(int index)
        {
            Image tmpImage = Images[index];
            Images[index] = Images[index + 1];
            Images[index + 1] = tmpImage;
            this.OnChange();
        }
    }
Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66
  • 2
    It may be advisable to also make the `Images` property read-only or private, unless it really _needs_ to be publicly settable. Otherwise it's possible to alter the list without calling the desired methods. – David Nov 26 '10 at 23:52
  • Can you explain what the this.OnChange() method does? –  Nov 26 '10 at 23:55
  • it raises the onchange event which you can handle outside of the class – Ali Tarhini Nov 26 '10 at 23:56
0

You could also try using the BindingList. The differences are discussed in here: https://stackoverflow.com/questions/4284663/...

Community
  • 1
  • 1
Prasanna K Rao
  • 1,086
  • 2
  • 8
  • 18