Im creating a custom item list, where i draw all the items. Drawing the items was easy, finding out which item is clicked is the hard part, i've been trying to figure it out for the past 2 hours. Does anybody know how can i find out which item was clicked? Here is my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication2
{
class MyList : Control
{
#region Constructor
public MyList()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
#endregion
#region Properties
private string[] Items = new string[] { "Item 1", "Item 2", "Item 3", "Item 4" };
private int _ItemHeight = 25;
public int ItemHeight
{
get { return _ItemHeight; }
set { _ItemHeight = value; this.Invalidate(); }
}
#endregion
#region Mouse Events
protected override void OnClick(EventArgs e)
{
// Get the clicked item
}
#endregion
protected override void OnPaint(PaintEventArgs e)
{
int ItemYPosition = 0;
foreach (string Item in Items)
{
Rectangle ItemRectangle = new Rectangle(0, ItemYPosition, this.Width, _ItemHeight);
e.Graphics.DrawRectangle(Pens.Red, ItemRectangle);
ItemYPosition += _ItemHeight;
}
}
}
}