Single cell : click perfom custom action

Posted by: fsegui on 13 September 2022, 2:40 pm EST

    • Post Options:
    • Link

    Posted 13 September 2022, 2:40 pm EST

    Hi,

    I’m using C1FlexGrid. I’m displaying results concatened in a single cell like this

    Result 1, Result2....
    

    Some rows can have several results, some none

    .

    Is there a possibility for a use to click on the cell and depending where he clicks, the action can take into parameter the value like this :

    • click on Result 1 => call method test(“Result 1”)
    • click on Result 2 => call method test(“Result 2”)
    • etc

    Thanks

    Best regards

    Florent

  • 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

  • Posted 14 September 2022, 6:04 am EST

    Hi Florent,

    Finding the clicked part of the string within a cell will not be feasible, as it requires handling a lot of scenarios, like different Cell styles, Font styles, Word-wrapping, etc. The only workaround we could think of is exactly similar to what @Wolfgang has suggested, which could be a basic implementation for your requirement.

    Although, if you have a more complex implementation, we would suggest separating the “Results” in your cells into different columns, if possible.

    PS. Thank you for the workaround, Wolfgang! :wink:

    Best Regards,

    Kartik

  • Posted 14 September 2022, 7:05 am EST

    Hi Wolfgang & Kartik,

    Thanks for your comments.

    I was thinking the same for measuring the text in the cell and its inconvenients.

    Adding columns is not possible as the “Results” are not known in advance and each row could have different count of results.

    I could maybe use the “Rowdetail” possibility. Is it possible to hide the “+” on the first column? and display the rowdetail content only when clicking on the cell?

    Thanks

    Regards

    Florent

  • Posted 15 September 2022, 5:59 am EST

    Hi Florent,

    To Expand/Collapse the RowDetails on any cell click, you can handle the MouseClick event of the C1FlexGrid to use the Collapsed property (RowWithDetails class) of the row. Also, there is no direct way of hiding the expand/collapse icon for RowDetails in the first column. As a workaround, you can hide the default first column and add an extra Fixed column, which will not show these icons.

    Kindly refer to the attached sample showing both the above approaches.

    FgRowDetail.zip

    Best Regards,

    Kartik

  • Posted 15 September 2022, 3:25 pm EST

    Hi Kartik

    Thanks for your sample. I’ll change to use rowdretails.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels