
Adding Code Behind the Worksheet
In this step, you will add a message box to an initialization event of the worksheet.
To add a message box to an initialization event
-
Verify that the My Security Test.xls workbook is open in the Visual Studio designer, with Sheet1 displayed.
-
In Solution Explorer, right-click Sheet1.vb or Sheet1.cs, and then click View Code on the shortcut menu.
-
Add the following code to the Startup method inside class Sheet1 to show a message box during initialization.
Public Sub Sheet1_Startup(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Startup
MessageBox.Show("Security settings are correct.")
End Sub
private void Sheet1_Startup(object sender, System.EventArgs e)
{
MessageBox.Show("Security settings are correct.");
}
-
On the File menu, click Save All.
-
In the Save Project dialog box, type C:\Walkthroughs in the Location box.
-
Click Save.
-
Press F5 to run the project.
Excel starts and the message box appears.
-
Close the message box.
-
Close Excel.
-
In the Visual Studio designer, find and select the tab for My Security Test.xls.
-
Close My Security Test.xls (the Excel worksheet design surface) in the Visual Studio designer.
You must close all open instances of Excel before you can change the security policy.
When you built the Excel Workbook project, the wizard added a new security code group and a child code group to the Runtime Security Policy at the User level.
Next, you will remove the code group that the wizard created.