For Microsoft Visual Basic .NET versions of this article, see
319342.
This task content
summary
Step-by-step guide
Overview You can use this step in Visual C # .NET to handle events of Office XP spreadsheet components in the Windows Form.
Back to top
Step Instructions Before starting to perform the following steps, you must modify the class package generated by Visual Studio .NET to Office XP Web Components (OWC). The class package that must be modified to modify the Visual Basic .NET can handle the OWC event correctly. For additional information, click the article number below to see the article in the Microsoft Knowledge Base:
328275 How to: Handle Events for the Office Web Components in Visual Studio .NET
Create a new Visual C # .NET Windows application project. Name the project Shetevents. Form1 will be created and then open in the Design view. On the View menu, click Toolbox. Drag and drop the spreadsheet component from the toolbox to Form1. On the View menu, click on the code. Add the following lines of code in front of the Namespace statement in the Form1.cs file: use system.diagnostics;
USING OWC10 = Microsoft.Office.Interop.owc;
Add the following code to endedit, BeforeContextMenu, and Commandexecute events: Private Void BeforeContextMenu (Object Sender,
Axmicrosoft.Office.Interop.owc.ispreadsheteeventsink_beforecontextMenuevent E)
{
Debug.writeline ("BeforeContextMenu Event: Create Custom Menu");
// build the menu structure:
// Menu Item Submenu Item
/ / ============== =============
// - Format as ... - Blue
// - red
// - Enter Date
Object [] OADION1 = New Object [] {"& blue", "formatasblue"}
Object [] OAAction2 = New Object [] {"& red", "formatared"}
Object [] OAADITION3 = New Object [] {"& green", "formatasgreen"};
Object [] osubmenu1 = new object [] {oction1, oction2, oction3}
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.ispreadsheteeventsink_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 "formatared":
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 vid endedit (Object Sender,
Axmicrosoft.Office.Interop.owc.ispreadsheetEventsink_endediteVent E)
{
Debug.write ("EndEdit Event:");
// Verify if the cell what is being edited is Cell A1.
Object OOPT = system.reflection.missing.value;
String saddr = axspreadsheet1.activecell.get_address
Ref oopt, ref oopt, oowc10.xlreference, oLA1, REF OOPT,
Ref OOPT);
IF (Saddr! = "$ 1")
{
Debug.writeline ("Cell Is Not A1, Allow Edit");
Return;
}
// if it is cell a1, confirm what 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.
}
}
Press F5 to build and run this example. Enter a value in cell A1. If the value is not a number between 0 and 100, a message will appear and the editing operation is canceled. Right-click on any cell to display the shortcut menu, then click any of the commands in the menu to view the results.
Back to top
Refer to other information, please visit the Microsoft Web site below:
Microsoft Office developed by Visual Studio http://msdn.microsoft.com/library/en-us/dnoffdev/html/vsofficeDev.asp
Back to top
The information in this article applies to:
Microsoft Office XP Web Components Microsoft Visual C # .NET (2002)
Recent Updated: 2003-8-29 (1.0) Keywords: Kbhowto KbhowTomaster KbofficeWebspread KB319341 KBAUDDEVELOPER