Posted 14 September 2022, 4:18 am EST
Hi Florent,
step 1 would be to detect where the mouse click has happened. This can be achieved with the method “C1FlexGrid.HitTest”. The result is the clicked cell and an position - but unfortunately is it still the screen location.
Next, you would have to calculate the relative position in the cell: “C1FlexGrid.GetCellRect” gives you the coordinates of the cell in the control.
Now the tricky part: split the cell content and measure each string part. Check which string part matches with the click position.
Attached sample shows this.
FlexClickedCellContent.zip
The solution is not perfect - there is an offset in the cell that I did not check. But I hope it gives you a starting point.
For the records: here is the code:
private void c1FlexGrid1_MouseClick(object sender, MouseEventArgs e)
{
HitTestInfo hitTest = this.c1FlexGrid1.HitTest(e.Location);
if (hitTest.Row >= this.c1FlexGrid1.Rows.Fixed && hitTest.Column >= this.c1FlexGrid1.Cols.Fixed)
{
Rectangle cellRect = this.c1FlexGrid1.GetCellRect(hitTest.Row, hitTest.Column);
//Coordinates in Cell:
int xInCell = hitTest.X - cellRect.X;
int yInCell = hitTest.Y - cellRect.Y;
//Measure cell content:
string cellContent = (string)this.c1FlexGrid1[hitTest.Row, hitTest.Column];
Font fontCell = this.c1FlexGrid1.GetCellStyleDisplay(hitTest.Row, hitTest.Column).Font;
using (Graphics graphics = this.c1FlexGrid1.CreateGraphics())
{
//Measure string parts until there is a match:
string[] stringParts = cellContent.Split(';');
float currentLocation = 0;
foreach (string stringPartCurrent in stringParts)
{
//Add separator to string to measure - otherwise part 2 would have an incorrect startup.
//The measuring area is limited by the cell width:
SizeF sizeText = graphics.MeasureString(stringPartCurrent + ";", fontCell, this.c1FlexGrid1.Cols[hitTest.Column].Width);
//Are we inside this string?
if (xInCell >= currentLocation && xInCell < (currentLocation + sizeText.Width))
{
MessageBox.Show(this, "You have clicked this part: " + stringPartCurrent);
}
//Increment width:
currentLocation += sizeText.Width;
}
}
}
}
Best regards
Wolfgang