Ok, I did some more testing on this. My set property wasn't exactly what I
said below. First the background. I am extending a listbox to include
images. I have an ImageList property of the new control. I want the option
to use a default image if the ImageList has been set, if it hasn't I want the
control to function like the base class listbox. This has caused a number of
issues with intializing all the variables and properties. Here is the set
method I was using for the ImageIndex property:
private ImageList _imageList = null;
private int _imageIndex = 0;
public int ImageIndex {
get { return _imageIndex; }
set
{
if (_imageList != null) //confirm that images are being used
{
//check for valid index first - default to 0
if (value >= 0 && value < ImageList.Images.Count)
_imageIndex = value;
else
_imageIndex = 0; //default to first image if value out of range
//invalidate control to update images
this.Invalidate();
}
}
Note, I need to check for _imageList != null becuase I am making the use of
an ImageList optional. When this method was in place I could set the
_imageIndex value in the designer just fine when there was an ImageList
assigned. However, at compile time the _imageIndex would default back to
whatever I set it to by default when it was decared (0). I changed the set
method to:
set
{
_imageIndex = value;
if (_imageList != null) //confirm that images are being used
{
//check for valid index first - default to 0
if (value >= 0 && value < ImageList.Images.Count)
_imageIndex = value;
else
_imageIndex = 0; //default to first image if value out of range
//invalidate control to update images
this.Invalidate();
}
and things now work fine. This seems redundant to do it this way. Could
someone please explain what is going on at compile/run time? Why are the set
methods being called at all? My first clue on all this was before I put the
condtion statement if(_imageList != null) in the set method, if I didn't use
an ImageList I was getting exceptions at run time even though I wasn't
calling the set method and I didn't see any reason for it to get called.
Thanks.