-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the image indexer, used to distinguish between image indexes an…
…d image keys.
- Loading branch information
1 parent
2227cc8
commit 00b3d24
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Windows.Forms; | ||
|
||
namespace BetterControls.Drawing | ||
{ | ||
/// <summary> | ||
/// Used to provide a mechanism for a control to support using both image indexes and image keys. | ||
/// </summary> | ||
public class ImageIndexer | ||
{ | ||
private ImageList _imageList; | ||
private int _index = -1; | ||
private string _key = string.Empty; | ||
private bool _useIndex = false; | ||
|
||
/// <summary> | ||
/// Gets the image list associated with this image indexer. | ||
/// </summary> | ||
protected virtual ImageList ImageList => _imageList; | ||
|
||
/// <summary> | ||
/// Gets or sets the index of the image from the associated image list. The image indexer is configured to use this index as an image source. | ||
/// </summary> | ||
public virtual int Index | ||
{ | ||
get => _index; | ||
set | ||
{ | ||
if (Index != value) | ||
{ | ||
_index = value; | ||
} | ||
|
||
_useIndex = true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the key of the image from the associated image list. The image indexer is configured to use this key as an image source. | ||
/// </summary> | ||
public virtual string Key | ||
{ | ||
get => _key; | ||
set | ||
{ | ||
if (Key != value) | ||
{ | ||
if (value is null) | ||
_key = string.Empty; | ||
else | ||
_key = value; | ||
} | ||
|
||
_useIndex = false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the computed image index, based on whether the image indexer is configured to use the index or key as an image source. | ||
/// </summary> | ||
public virtual int ComputedIndex | ||
{ | ||
get | ||
{ | ||
if (_useIndex) | ||
{ | ||
return Index; | ||
} | ||
else if (ImageList != null) | ||
{ | ||
return ImageList.Images.IndexOfKey(Key); | ||
} | ||
|
||
return -1; | ||
} | ||
} | ||
} | ||
} |