0

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;
            }
        }
    }
}
Adrao
  • 412
  • 5
  • 18
  • Have a look [at this answer](http://stackoverflow.com/questions/32919918/how-to-draw-line-and-select-it-in-panel/32920894?s=1|1.0572#32920894) - It is written for lines; for rectangles things are event simples using the Contains method. But the basic idea of going over a List is the same – TaW Apr 02 '16 at 19:11
  • To use that ill have to add a Point property to every item, i cant do that. – Adrao Apr 02 '16 at 19:12
  • Not really. Don't use the Line class; just write something that works with your Items. Should be even simpler with rectangles..! – TaW Apr 02 '16 at 19:14

1 Answers1

1

Rectangle has a couple of nice methods. One is Contains(Point):

    protected override void OnClick(EventArgs e)
    {
        // Get the clicked item
        int ItemYPosition = 0;
        foreach (string Item in Items)
        {
            Rectangle ItemRectangle = new Rectangle(0, ItemYPosition,
                                                    this.Width, _ItemHeight);

            if (ItemRectangle.Contains(e.Location)) // found one hit!

            ItemYPosition += _ItemHeight;
        }
    }

It is up to you to decide what to do when you found a hit or even if you may find more than one hit..

TaW
  • 53,122
  • 8
  • 69
  • 111