Code for a custom ToolPart

Listing 2. The code for building a custom ToolPart. ToolParts inherit from the ToolPart namespace in the SharePoint Services object model.

. . .
   public class CustomToolPart : ToolPart
   {
      private string inputname;

      public CustomToolPart()
      {
         //Set defaults
         this.UseDefaultStyles = true;
         this.AllowMinimize = false;
         this.Title = "My Custom Toolpart";

         this.Init += new EventHandler(CustomToolPart_Init);

      }

      private void CustomToolPart_Init(object sender, System.EventArgs e)
      {
         inputname = this.UniqueID + "message";
      }

      protected override void RenderToolPart(HtmlTextWriter output)
      {
         ToolPane tp = this.ParentToolPane;
         CustomPropertyWebPart myWP = 
            (CustomPropertyWebPart)tp.SelectedWebPart;
         
         output.Write("Please enter your custom text: <input name=\"" 
            + inputname + "\" type=\"text\" value=\"" + myWP.MyStringforToolPart
            + "\"><BR>");

      }
      
      public override void ApplyChanges()
      {
         //Get our web part
         ToolPane tp = this.ParentToolPane;
         CustomPropertyWebPart myWP = 
            (CustomPropertyWebPart)tp.SelectedWebPart;

         //Send back our text to our web part
         myWP.MyStringforToolPart = Page.Request.Form[inputname];
      }
      
      

   }
. . .