Reputation:
Here's the error:
It says the source of the exception is on Line20 of my class. Here is my class:
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;
using System.Collections.ObjectModel;
namespace WebServiceScanner
{
public partial class imageList : UserControl
{
private int XPosition = 0;
public imageList()
{
InitializeComponent();
Images.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Images_CollectionChanged);
}
public ObservableCollection<selectablePicture> Images { get; set; }
public void AddImage(selectablePicture image)
{
Images.Add(image);
}
public void RemoveImage(selectablePicture image)
{
Images.Remove(image);
}
public void MoveImageLeft(int index)
{
selectablePicture tmpImage = Images[index];
Images[index] = Images[index - 1];
Images[index - 1] = tmpImage;
}
public void MoveImageRight(int index)
{
selectablePicture tmpImage = Images[index];
Images[index] = Images[index + 1];
Images[index + 1] = tmpImage;
}
void Images_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
RedrawImages();
}
private void RedrawImages()
{
foreach (var picture in Images)
{
picture.Location = new Point(XPosition + panel1.AutoScrollPosition.X, 0);
XPosition += 130;
panel1.Controls.Add(picture);
}
}
}
}
Maybe I'm doing something dumb like setting an event handler at the constructor. Any ideas? This user control isn't really that complex, so there are few places where something could be wrong.
If you guys need more information please let me know.
Upvotes: 0
Views: 586
Reputation: 19881
Does < selectablePicture >
have a parameterless public constructor? And are you instancing Images
?
Upvotes: 0
Reputation: 722
I'm guessing it is something to do with the fact that Images isn't set to any value so the control is trying to render it for DesignView but Images is null hence the NullReferenceException. It's probably the code in the Constructor when it tries to register against the Images which will probably run before any injection code setting images.
Upvotes: 0
Reputation: 8521
Add a statement to instantiate Images
.
public imageList()
{
InitializeComponent();
Images = new ObservableCollection<selectablePicture>();
Images.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Images_CollectionChanged);
}
Upvotes: 1