views:

214

answers:

2

I have a C# application that uses SpreadsheetGear's WorkbookView control. A user wants to be able to have changes he makes to comments on a cell saved.

Does anyone know of a way to capture changes when a user edits a comment. The cell edit events are not fired when a comment is edited and I can not find any way to capture changes a user makes to comments.

+1  A: 

Unfortunately, the answer is "sort of but not really".

You can catch the ShapeAction event as demonstrated below, but the ShapeAction event is really designed for shapes and not for comments so you cannot get the current text (the text hsa not yet been stored in the shape) and you cannot even get the cell which the comment belongs to. Still, I will paste in some code since it might be of use to you:

    private void workbookView1_ShapeAction(object sender, SpreadsheetGear.Windows.Forms.ShapeActionEventArgs e)
    {
        switch (e.ShapeActionType)
        {
            case SpreadsheetGear.Windows.Forms.ShapeActionType.TextChanged:
                if (e.Shape.Type == SpreadsheetGear.Shapes.ShapeType.Comment)
                {
                    // Unfortunately, this is as far as we can get since IShape
                    // does not have a way to get back to the cell which owns the comment.
                    //
                    // Furthermore, the text is not yet stored in the IShape, so we
                    // cannot get the current text either.
                }
                break;
        }
    }

If you send email to support at SpreadsheetGear requesting this capability they will add this feature request to their list with your email address associated with it.

Joe Erickson
Joe, thanks for your code. It led me to a solution that seems to work which I posted in an answer below.
Daniel
+2  A: 

Before I read Joe's reply not knowing of a way to save comments individually, I decided to save comments by looping through the cells in a workbookView and grabbing the comments as the form is closing.

After seeing his reply, I found a way to get a comment right after it is entered using a combination of the workbookView1_ShapeSelectionChanged and workbookView1_ShapeAction events. The workbookView1_ShapeSelectionChanged event fires when the comment box is opened for editing and when the comment box is closed for editing. When it fires for the comment box opening, I get the ActiveCell because it may no longer be the ActiveCell when the comment box closes. I use that cell for getting the comment when the comment box closes if a comment has changed. The workbookView1_ShapeAction event fires everytime a new character is entered in the comment box which is useful for marking the comment as changed. After the comment is saved, the commentChanged variable is set back to false. Here is the code using the code that Joe posted in his answer:

private void workbookView1_ShapeAction(object sender, SpreadsheetGear.Windows.Forms.ShapeActionEventArgs e)
{
  switch (e.ShapeActionType)
  {
    case SpreadsheetGear.Windows.Forms.ShapeActionType.TextChanged:
      if (e.Shape.Type == SpreadsheetGear.Shapes.ShapeType.Comment)
      {
        //set comment changed flag to true
        _commentChanged = true;
      }
      break;
  } 
}

private void workbookView1_ShapeSelectionChanged(object sender, SpreadsheetGear.Windows.Forms.ShapeSelectionChangedEventArgs e)
{
  if (_commentChanged)
  {
    //get comment
    string comment = _commentCell.Comment.Shape.TextFrame.Characters.Text;

    //save comment
    //....

    //set comment changed flag to false
    _commentChanged = false;
  }
  else
  {
    //get the cell whose comment is being edited
    _commentCell = workbookView1.ActiveCell;
  }
}
Daniel