/// <summary> /// Calculates the column (X) index of a cell clicked. /// </summary> /// <param name="table"></param> /// <param name="mouseXPosition"> /// X position of the mouse when click event occurred (must be position relative to TableLayoutPanel component, not entire screen). /// </param> /// <returns> /// Integer representing the index of the cell column clicked. Starts at 0. /// </returns> public static int CellClickedColumnIndex(this TableLayoutPanel table, int mouseXPosition) { int colIndex = 0; //Width of the first column //Add 1 to account for rightside border thickness float widthSum = table.ColumnWidthAsPixels(colIndex) + 1; while ((colIndex < table.ColumnCount) && (mouseXPosition > widthSum)) { widthSum += table.ColumnWidthAsPixels(colIndex) + 1; colIndex++; } return(colIndex); }