Click here to Skip to main content
15,881,715 members
Articles / Desktop Programming / Windows Forms

PluginManager - A C# utility to load plug-in based components

Rate me:
Please Sign up or sign in to vote.
4.72/5 (11 votes)
3 Dec 2008CC (ASA 2.5)9 min read 64.9K   2.7K   150  
PluginManager is a simple desktop utility that loads and runs simple control based plug-ins.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Citrus.Forms.StickyNote
{
    /// <summary>
    /// A *very simple* example to show how to create a sticky notes plugin    
    /// </summary>
    [Plugin("Sticky Note", "A sticky notes plugin sample")]
    public partial class StickyNote : UserControl
    {
        readonly string NotesFile = AppDomain.CurrentDomain.BaseDirectory + "/plugins/StickyNote/Note.txt";

        public StickyNote()
        {
            InitializeComponent();
            LoadNotes();
        }

        /// <summary>
        /// Saves the current text to the notes file
        /// </summary>
        private void SaveNotes()
        {
            noteSaver.Enabled = false;
            using (StreamWriter w = new StreamWriter(NotesFile))
            {
                w.AutoFlush = true;
                w.Write(noteEntry.Text);
                w.Close();
            }
            noteSaver.Enabled = true;
        }

        /// <summary>
        /// Loads the notes file
        /// </summary>
        private void LoadNotes()
        {
            CheckNotesFile();
            using (StreamReader r = new StreamReader(NotesFile))
            {
                noteEntry.Text = r.ReadToEnd();
                r.Close();
            }
        }

        /// <summary>
        /// Checks the notes file (creates one if not found)
        /// Loads the contents to screen
        /// </summary>
        private void CheckNotesFile()
        {
            if (!File.Exists(NotesFile))
            {
                File.CreateText(NotesFile).Close();
            }
        }


        /// <summary>
        /// Tick hanlder to save the note every now and then
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void noteSaver_Tick(object sender, EventArgs e)
        {
            SaveNotes();
        }


        /// <summary>
        /// Initialize extra event handlers
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StickyNote_Load(object sender, EventArgs e)
        {
            // Ensure the notes get saved in various events
            // Currently, we bother about only when 
            //     * the main form is closed
            //     * the tab that contains this plugin is removed
            this.ParentForm.FormClosing += new FormClosingEventHandler(ParentForm_FormClosing);
            ((TabControl)this.Parent.Parent).ControlRemoved += new ControlEventHandler(StickyNote_ControlRemoved);
        }


        void StickyNote_ControlRemoved(object sender, ControlEventArgs e)
        {
            if (e.Control == this.Parent)
                SaveNotes();
        }

        void ParentForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveNotes();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Web Developer
United Kingdom United Kingdom
I work as a Technology Lead for an IT services company based in India.

Passions include programming methodologies, compiler theory, cartooning and calligraphy.

Comments and Discussions