System.Windows.Forms.ListBox.GetItemRectangle C# (CSharp) Method

GetItemRectangle() public method

public GetItemRectangle ( int index ) : Rectangle
index int
return System.Drawing.Rectangle
		public Rectangle GetItemRectangle (int index)
		{
			if (index < 0 || index >= Items.Count)
				throw new  ArgumentOutOfRangeException ("GetItemRectangle index out of range.");

			Rectangle rect = new Rectangle ();

			if (MultiColumn) {
				int col = index / RowCount;
				int y = index;
				if (y < 0) // We convert it to valid positive value 
					y += RowCount * (top_index / RowCount);
				rect.Y = (y % RowCount) * ItemHeight;
				rect.X = (col - (top_index / RowCount)) * ColumnWidthInternal;
				rect.Height = ItemHeight;
				rect.Width = ColumnWidthInternal;
			} else {
				rect.X = 0;
				rect.Height = GetItemHeight (index);
				rect.Width = items_area.Width;
				
				if (DrawMode == DrawMode.OwnerDrawVariable) {
					rect.Y = 0;
					if (index >= top_index) {
						for (int i = top_index; i < index; i++) {
							rect.Y += GetItemHeight (i);
						}
					} else {
						for (int i = index; i < top_index; i++) {
							rect.Y -= GetItemHeight (i);
						}
					}
				} else {
					rect.Y = ItemHeight * (index - top_index);	
				}
			}

			if (this is CheckedListBox)
				rect.Width += 15;
				
			return rect;
		}

Usage Example

Example #1
0
        private void OnMouseHover(object sender, System.EventArgs e)
        {
            if (tipWindow != null)
            {
                tipWindow.Close();
            }

            if (settings.GetSetting("Gui.ResultTabs.ErrorsTab.ToolTipsEnabled", false) && hoverIndex >= 0 && hoverIndex < detailList.Items.Count)
            {
                Graphics g = Graphics.FromHwnd(detailList.Handle);

                Rectangle itemRect = detailList.GetItemRectangle(hoverIndex);
                string    text     = detailList.Items[hoverIndex].ToString();

                SizeF sizeNeeded      = g.MeasureString(text, detailList.Font);
                bool  expansionNeeded =
                    itemRect.Width < (int)sizeNeeded.Width ||
                    itemRect.Height < (int)sizeNeeded.Height;

                if (expansionNeeded)
                {
                    tipWindow            = new TipWindow(detailList, hoverIndex);
                    tipWindow.ItemBounds = itemRect;
                    tipWindow.TipText    = text;
                    tipWindow.Expansion  = TipWindow.ExpansionStyle.Both;
                    tipWindow.Overlay    = true;
                    tipWindow.WantClicks = true;
                    tipWindow.Closed    += new EventHandler(tipWindow_Closed);
                    tipWindow.Show();
                }
            }
        }
All Usage Examples Of System.Windows.Forms.ListBox::GetItemRectangle