Writing Plugins For Windows Live Writer – Working With Forms

Most plugins for Live Writer will have a form appear when you click on the Insert link within Live Writer, this form will perform the functionality of the plugin and return the code or text that will be put in the blog entry. Within this form, you can do whatever you want the plugin to do, but for this example I will be keeping it simple and showing the basics, letting you code the rest of the plugin and change whatever settings for your form that you want.

Open up your project (I will be continuing with the “MyNewPlugin” example from the previous posts), and create a new form in your project (for this example I will be calling the form frmMyForm). For this example, my form will have only a text box, an insert button and a cancel button, and all it will do is allow the user to enter some text and have it appear in the blog entry when you press insert. In your form, add a textbox and two buttons (I have also added a label to describe what the textbox does), you should have something like this:
image04 Developer
Any other properties of this form can be changed as you wish. Except for the textbox which I have called txbxText, I have kept all the original names that get given when the form items are created.

Now we need to tell the buttons what they are going to be done. We’ll start with the cancel button; in the properties of the form, there is a property called CancelButton, just set that to be the cancel button (in this case Button2). For the Insert button, double click it and Visual Studio will create the method for you, we can then add our code into that method. Before we do this though, at the top of the Class (before the initializing method), we need to declare a string variable, so just put in string text;.

In this plugin, all we need to do for the Insert button is assign the DialogResult value, set the text and close the form. So the code we need to put in is:

            DialogResult = DialogResult.OK;
            text = txbxText.Text;
            this.Close();

Note how we are assigning the text string to equal the text from the textbox. The last thing we need to do for this plugin is just create a string method that returns the text string, this will be called later from the main plugin class.

        public string getText
        {
            get { return text; }
        }

Now, in the main class where we overrode the CreateContent method, we need to add in a new section, so before we had just newContent = “Some text”; we now need to tell the plugin that we want to use our new form:

using (frmMyForm form = new frmMyForm())
            {

Now, you might remember in the last post that I mentioned about the DialogResult section (and I also touch upon it above), this is where it actually comes in to play. Once we have told the plugin to use the form, we need to create a new DialogResult that opens up our newly called form, so we insert the next line:

                DialogResult result = form.ShowDialog();

In the form when we click on the Insert button, we set the DialogResult to equal DialogResult.OK, this signifies that an OK was pressed in a dialog, so we can check for this by using an if statement. Within that if statement, we are going to set the newContent value to equal the getText string from the form:

                if (result == DialogResult.OK)
                {
                    newContent = "You entered: " + form.getText + ".";
                }

We now need to close off all open sections (using a } ) . Hopefully you can see where everything falls into place, form.getText is the public string that we set to return the text string in the form, and the text string gets the value of the textbox, which is what finally gets inserted into the blog entry.

Any questions about this post please feel free to leave a comment, as this one is a little more involved that the previous two steps.

Download Source Code: here.

SL

<< Previous – Adding An Icon