Share via


Adding Command Buttons (C# Tutorial) [Office 2003 SDK Documentation]

Previous  Creating a Smart Document using C#

The following steps show you how to add command buttons to the SimpleSample smart document.

  1. The first thing you need to do is add a constant for the commandbutton element in the SimpleSample schema. Insert the following code into the general declarations section of your code module, below the existing constants.

    const String cBUTTON = cNAMESPACE + "#commandbutton";
    
  2. Next you need to add 1 to the cTYPES constant. Remove the existing cTYPES constant, and enter the following code or change your code to match.

    const Int32 cTYPES = 2;
    
  3. Now you are ready to modify the existing code to insert the command button. The subroutines you need to modify are SmartDocXMLTypeName, SmartDocXMLTypeCaption, ControlCount, ControlID, ControlTypeFromID, and ControlCaptionFromID.

    In the SmartDocXMLTypeName property subroutine, insert the following code.

                case 2:
                   strTypeName = cBUTTON;
                   break;
    

    In the SmartDocXMLTypeCaption property subroutine, insert the following code.

                case 2:
                   strTypeCaption = "Click";
                   break;
    

    In the ControlCount property subroutine, insert the following code.

                case cBUTTON:
                   intNumberOfControls = 1;
                   break;
    

    In the ControlID property subroutine, insert the following code.

                case cBUTTON:
                   intControlID = ControlIndex + 100;
                   break;
    

    In the ControlTypeFromID property subroutine, insert the following code.

                case 101:
                   type = C_TYPE.C_TYPE_BUTTON;
                   break;
    

    Finally, in the ControlCaptionFromID property subroutine, insert the following code.

                case 101:
                   strControlCaption = "Test button";
                   break;
    
  4. Next, you need to enter the code that operates the button you added above. In the InvokeControl method, insert the following code. The InvokeControl method is used to specify code that you want to run for command buttons, hyperlinks, and, in Microsoft Word only, clicking document fragments.

                switch ( ControlID){
                   case 101:
                      System.Windows.Forms.MessageBox.Show
                         ("This is an example of a button.");
                      break;
    
                   default:
                      break;
                }
    
  5. Recompile your SimpleSample smart document DLL, and copy it to the deployment location that you specified earlier. When you reopen your SimpleSample smart document, delete the SimpleSample XML expansion pack, and then re-add it to the document.

Next  Adding Help Content