{"id":240,"date":"2010-01-12T09:53:20","date_gmt":"2010-01-12T09:53:20","guid":{"rendered":"http:\/\/dirk.forbiddendream.be\/?p=240"},"modified":"2016-01-07T10:50:18","modified_gmt":"2016-01-07T10:50:18","slug":"creating-plugins-in-c-the-plugin-part-34","status":"publish","type":"post","link":"https:\/\/dirk.schuermans.me\/?p=240","title":{"rendered":"Creating plugins in C#: The plugin (Part 3\/4)"},"content":{"rendered":"<p>Welcome to the third part of our little tutorial ^.^<\/p>\n<p>In this part I&#8217;ll explain how the <strong>plugin<\/strong> works and what you need in order for it to work&#8230;<\/p>\n<p><!--more--><\/p>\n<h1>Plugin<\/h1>\n<p>Ok, we have our handler, now we can use it on our plugins and our application<\/p>\n<p>During this tutorial we&#8217;ll create a simple plugin which is a windows form with a button on it and when you press the button it will show you a messagebox.<\/p>\n<p>Start off by adding a new <strong>C#<\/strong> <strong>Class Library project <\/strong>to the solution and call it <strong>TestPlugin<\/strong> (<em>Add &gt; New Project<\/em>)<\/p>\n<p><span style=\"color: #ff0000;\"><span style=\"text-decoration: underline;\">Delete<\/span><\/span> the default class that gets created (<em>Class1.cs<\/em>)<\/p>\n<p><em>* Note: Because we are working with a class library here it will be hard to debug the applications you are creating as plugins. A workaround could be to create a normal windows form project first, test it out and when it is working like you want it to work, move the files over to the class library project.<\/em><\/p>\n<p>Add a new <strong>Windows Form <\/strong>and call it <strong>TestPlugin<\/strong><strong> <\/strong>(<em>Add &gt; Windows Form<\/em>)<\/p>\n<figure id=\"attachment_269\" aria-describedby=\"caption-attachment-269\" style=\"width: 476px\" class=\"wp-caption alignnone\"><a rel=\"attachment wp-att-269\" href=\"http:\/\/dirk.schuermans.me\/?attachment_id=269\"><img loading=\"lazy\" class=\"size-full wp-image-269 \" title=\"add_pluginform\" src=\"http:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_pluginform.png\" alt=\"\" width=\"476\" height=\"284\" srcset=\"https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_pluginform.png 794w, https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_pluginform-300x179.png 300w\" sizes=\"(max-width: 476px) 100vw, 476px\" \/><\/a><figcaption id=\"caption-attachment-269\" class=\"wp-caption-text\">Adding a Windows Form<\/figcaption><\/figure>\n<p>Next up is adding a reference to the <strong>PluginHandler<\/strong> project (<em>Add Reference &gt; Projects tab &gt; PluginHandler<\/em>)<\/p>\n<figure id=\"attachment_270\" aria-describedby=\"caption-attachment-270\" style=\"width: 283px\" class=\"wp-caption alignnone\"><a rel=\"attachment wp-att-270\" href=\"http:\/\/dirk.schuermans.me\/?attachment_id=270\"><img loading=\"lazy\" class=\"size-full wp-image-270 \" title=\"add_handlerreference\" src=\"http:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_handlerreference.png\" alt=\"\" width=\"283\" height=\"239\" srcset=\"https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_handlerreference.png 471w, https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/add_handlerreference-300x254.png 300w\" sizes=\"(max-width: 283px) 100vw, 283px\" \/><\/a><figcaption id=\"caption-attachment-270\" class=\"wp-caption-text\">Adding a reference to an existing project<\/figcaption><\/figure>\n<p>Now that we have our reference to the handler, it&#8217;s time to implement it \ud83d\ude09<\/p>\n<p>Open up the <strong>designer code view<\/strong> for the windows form (<em>Double click the TestPlugin.designer.cs file)<\/em> and add \/ change the following:<\/p>\n<pre name=\"code\" class=\"c-sharp\">using PluginHandler;\r\n\r\nnamespace TestPlugin\r\n{\r\n    [PluginAttribute]\r\n    partial class TestPlugin : IPlugin\r\n    {\r\n        ...\r\n    }\r\n}<\/pre>\n<p>Since we have implemented the IPlugin interface, we are forced to use all methods and properties from within the IPlugin interface.<br \/>\nTherefor, open the code view for the windows form (<em>TestPlugin.cs<\/em>) and add the following method:<\/p>\n<pre name=\"code\" class=\"c-sharp\">public void ShowPlugin()\r\n{\r\n    this.Show();\r\n}<\/pre>\n<p>Only thing left to do is add the button to the form and have it show a message box.<br \/>\nDrag a new button onto the form in the design view and call it <strong>btnMessage<\/strong><\/p>\n<p>Double click the button and add the following code:<\/p>\n<pre name=\"code\" class=\"c-sharp\">private void btnMessage_Click(object sender, EventArgs e)\r\n{\r\n    MessageBox.Show(\"Bar\", \"Foo\", MessageBoxButtons.OK, MessageBoxIcon.Information);\r\n}<\/pre>\n<p>There we go, our plugin is done \ud83d\ude42<\/p>\n<figure id=\"attachment_275\" aria-describedby=\"caption-attachment-275\" style=\"width: 297px\" class=\"wp-caption alignnone\"><a rel=\"attachment wp-att-275\" href=\"http:\/\/dirk.schuermans.me\/?attachment_id=275\"><img loading=\"lazy\" class=\"size-full wp-image-275\" title=\"plugin_design\" src=\"http:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/plugin_design.png\" alt=\"\" width=\"297\" height=\"297\" srcset=\"https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/plugin_design.png 297w, https:\/\/dirk.schuermans.me\/wp-content\/uploads\/2010\/01\/plugin_design-150x150.png 150w\" sizes=\"(max-width: 297px) 100vw, 297px\" \/><\/a><figcaption id=\"caption-attachment-275\" class=\"wp-caption-text\">This is how the plugin should look like<\/figcaption><\/figure>\n<p><strong><a href=\"http:\/\/dirk.schuermans.me\/?p=243\" target=\"_self\">Continue to the final part of this tutorial&#8230;<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the third part of our little tutorial ^.^ In this part I&#8217;ll explain how the plugin works and what you need in order for it to work&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[38,6],"tags":[],"_links":{"self":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/240"}],"collection":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=240"}],"version-history":[{"count":7,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions"}],"predecessor-version":[{"id":544,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions\/544"}],"wp:attachment":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}