private void BeforeContextMenu(object sender,
AxMicrosoft.Office.Interop.OWC.ISpreadsheetEventSink_BeforeContextMenuEvent e)
{
Debug.WriteLine("BeforeContextMenu Event: Create Custom Menu");
// Build the menu structure:
// Menu Item Submenu Item
// ============== ============
// - Format As... - Blue
// - Red
// - Enter Date
object[] oAction1 = new object[]{"&Blue","FormatAsBlue"};
object[] oAction2 = new object[]{"&Red", "FormatAsRed"};
object[] oAction3 = new Object[]{"&Green","FormatAsGreen"};
object[] oSubMenu1 = new object[]{oAction1, oAction2, oAction3};
object[] oMenu1 = new Object[]{"&Format As...", oSubMenu1};
object[] oMenu2 = new object[]{"&Enter Date", "EnterDate"};
object[] oMenu = new object[]{oMenu1, oMenu2};
e.menu.Value=oMenu;
}
private void CommandExecute(object sender,
AxMicrosoft.Office.Interop.OWC.ISpreadsheetEventSink_CommandExecuteEvent e)
{
Debug.WriteLine("CommandExecute Event: Menu action = " +
e.command.ToString());
OWC10.Range sel = axSpreadsheet1.Selection;
object oColor = null;
// Take the action selected on the context menu.
switch(e.command.ToString())
{
case "FormatAsRed":
oColor = "red";
sel.Font.set_Color(ref oColor);
break;
case "FormatAsBlue":
oColor = "blue";
sel.Font.set_Color(ref oColor);
break;
case "FormatAsGreen":
oColor = "green";
sel.Font.set_Color(ref oColor);
break;
case "EnterDate":
sel.Formula="=TODAY()";
break;
}
}
private void EndEdit(object sender,
AxMicrosoft.Office.Interop.OWC.ISpreadsheetEventSink_EndEditEvent e)
{
Debug.Write("EndEdit Event: ");
// Verify if the cell that is being edited is cell A1.
object oOpt = System.Reflection.Missing.Value;
string sAddr = axSpreadsheet1.ActiveCell.get_Address(
ref oOpt, ref oOpt, OWC10.XlReferenceStyle.xlA1, ref oOpt,
ref oOpt);
if(sAddr!="$A$1")
{
Debug.WriteLine("Cell is Not A1, Allow edit");
return;
}
// If it is cell A1, confirm that the value entered is a number
// between zero and 100.
string sMsg = "Cell A1 must contain a number between 0 and 100.";
string sCaption = "Spreadsheet10 Event Demo";
try
{
double dVal =
System.Double.Parse(e.finalValue.Value.ToString());
if((dVal<0)||(dVal>100))
{
// Value not between 0 and 100.
Debug.WriteLine(
"Cell is A1 but value is not between 0 & 100. Cancel.");
System.Windows.Forms.MessageBox.Show(sMsg, sCaption);
e.cancel.Value=true; // Cancel the edit.
}
else
{
Debug.WriteLine(
"Cell is A1 and value is between 0 & 100. Allow edit.");
}
}
catch (System.FormatException fe)
{
// Cannot convert to a double.
Debug.WriteLine(
"Cell is A1 but the value is not a number. Cancel.");
System.Windows.Forms.MessageBox.Show(sMsg, sCaption);
e.cancel.Value=true; // Cancel the edit.
}
}