Creating a Windows Live Writer Plug-in using C# Part 2

First we will start off with the Plug-in class that inherits from ContentSource.  Please note from Part 1 that “InsertForm” is a class we will create.

 

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Linq;

   4: using System.Text;

   5: using WindowsLive.Writer.Api;

   6: using System.Windows.Forms;

   7:  

   8: namespace AmazonQuickLinkPlugin

   9: {

  10:     [WriterPlugin(

  11:         "93DD80D7-9327-4375-A9D8-777C0549C1C8", 

  12:         "Amazon Quick Link", 

  13:         PublisherUrl="http://www.whatifoundout.com",

  14:         Description="Creates a Amazon Quick Link url",

  15:         ImagePath="Images.Shazam.png")]

  16:     

  17:     [InsertableContentSource("Amazon Quick Link")]

  18:     public class Plugin : ContentSource

  19:     {

  20:         public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)

  21:         {

  22:             using (InsertForm form = new InsertForm())

  23:             {

  24:                 DialogResult result = form.ShowDialog();

  25:                 if (result == DialogResult.OK)

  26:                     content = form.Link;

  27:                 

  28:                 return result;

  29:             }

  30:  

  31:         }

  32:     }

  33: }

Now that we have the basics lets create the “InsertForm” form.  First a capture of what I made it look like and then the code.

image

   1: using System;

   2: using System.Collections.Generic;

   3: using System.ComponentModel;

   4: using System.Data;

   5: using System.Drawing;

   6: using System.Linq;

   7: using System.Text;

   8: using System.Windows.Forms;

   9:  

  10: namespace AmazonQuickLinkPlugin

  11: {

  12:     public partial class InsertForm : Form

  13:     {

  14:         public string ASIN { get; set; }

  15:         public string Category { get; set; }

  16:         public string SearchTerm { get; set; }

  17:         public string InnerText { get; set; }

  18:         public string Link { get; set; }

  19:  

  20:         public InsertForm()

  21:         {

  22:             InitializeComponent();

  23:         }

  24:  

  25:         private void btnOK_Click(object sender, EventArgs e)

  26:         {

  27:             AssignProperties();

  28:             this.Link = CreateAmazonQuickLink();

  29:             this.DialogResult = DialogResult.OK;

  30:             Close();

  31:         }

  32:  

  33:         private void btnCancel_Click(object sender, EventArgs e)

  34:         {

  35:             this.DialogResult = DialogResult.Cancel;

  36:             Close();

  37:         }

  38:  

  39:         private void AssignProperties()

  40:         {

  41:             //TODO: Add validation of ASIN format

  42:             this.ASIN = txtASIN.Text.Trim();

  43:             //TODO: Get categories from Amazon

  44:             this.Category = txtCategory.Text.Trim();

  45:             this.SearchTerm = txtSearchTerm.Text.Trim();

  46:             this.InnerText = txtInnerText.Text.Trim();

  47:         }

  48:  

  49:         private void btnPreview_Click(object sender, EventArgs e)

  50:         {

  51:             AssignProperties();

  52:             txtPreview.Text = CreateAmazonQuickLink();

  53:         }

  54:  

  55:         private string CreateAmazonQuickLink()

  56:         {

  57:             StringBuilder s = new StringBuilder();

  58:             s.Append("<a type=\"amzn\" ");

  59:  

  60:             if (ASIN != string.Empty)

  61:                 s.Append("asin=\"").Append(ASIN).Append("\" ");

  62:             else

  63:             {

  64:                 if (SearchTerm != string.Empty)

  65:                 {

  66:                     s.Append("search=\"").Append(SearchTerm).Append("\"");

  67:                     if (Category != string.Empty)

  68:                         s.Append("category=\"").Append(Category).Append("\"");

  69:                 }

  70:                 else

  71:                     return string.Empty;

  72:             }

  73:  

  74:             s.Append(">").Append(InnerText).Append("</a>");

  75:  

  76:             return s.ToString();

  77:         }

  78:  

  79:     }

  80: }

I’ve added a few TODO’s but for the most part it is done and works great.  One other thing I might note.  I never did get the XCOPY command they give you in the documentation to work after the first compile.  I had to keep deleting the DLL I created in the WindowsLive Writer plug-in directory.

Here is the Amazon Quick Linker plug-in in action.  Let me recommend one of my favorite books on estimation.  Steve McConnell just knows how to put it in a way I understand.  When I read his book Software Estimation: Demystifying the Black Art (Best Practices (Microsoft)) it really opened my eyes and made my programming more enjoyable.  I hope it will do the same for you.

Adding a PayPal Donate Button with HTML in ASP.Net

How do I add a PayPal donate button to my ASP.Net website with what PayPal generates for me?  After looking through Google I came up with my own easy solution.  It only involves HTML too!  Here it is and then I’ll explain it.

<a id="paypalDonate" href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4381882"><img border="0" id="payPalImage" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" alt="Donate to ASINLookUp.com" /></a>

First of all, create a anchor tag.  In the herf property of the anchor tag past the code from the “E-MAIL” tab where you generated the button from PayPal.  Next inside the anchor tag place a image tag.  Set its border to 0.  This gets ride of the outline around it.  Then in the source property add the GIF used to display the button from the “WEBSITE” tab where you generated the button from PayPal.

Last, set the style of “Text-Decoration: none” on your anchor link.  I did mine in a CSS but you can do yours in the tag if you want.  This gets rid of the underline you see with a link.

I hope this helped as there were may confusing ways to do it out there and some rather complicated, I thought.  See my PayPal Donate button in action by going to: AsinLookUp.com