Click here to Skip to main content
15,886,578 members
Articles / Desktop Programming / Windows Forms

Context Help Made Easy

Rate me:
Please Sign up or sign in to vote.
4.93/5 (53 votes)
2 Feb 2007CPOL10 min read 267.2K   2.7K   274  
This article introduces a new way of instrumenting your code that enables help authors associate help topics with the application’s visual contexts at any time - even post-compilation – and to do so using the application’s user interface without the involvement of the developer.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ContextHelpMadeEasy
{
    public partial class FHelpMappingDialog : Form
    {
        private bool m_bChanged = false;

        /// <summary>
        /// Static entry point to pop up this form.
        /// </summary>
        /// <param name="contextIDs"></param>
        /// <returns></returns>
        public static bool ShowHelpWriterHelper(List<ContextIDHTMLPathMap> contextIDs)
        {
            FHelpMappingDialog frmHelper = new FHelpMappingDialog();
            frmHelper.IDList = contextIDs;  // Populate the treelist.
            if( frmHelper.lvMapping.Items.Count > 0 )
                frmHelper.lvMapping.SelectedIndices.Add(0);
            frmHelper.ShowDialog();         // Popup the form.
            if (frmHelper.Changed)
            {
                // For each item in the ListView, change the path map to correspond to the UI.
                foreach (ListViewItem lvi in frmHelper.lvMapping.Items)
                {
                    ContextIDHTMLPathMap pathMap = (ContextIDHTMLPathMap)lvi.Tag;
                    pathMap.HTMLPath = (string)lvi.SubItems[0].Text.Trim();
                }
            }
            return frmHelper.Changed;
        }

        public FHelpMappingDialog()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets and sets the list of ids.  The setter updates the UI.
        /// </summary>
        public List<ContextIDHTMLPathMap> IDList
        {
            set 
            {
                lvMapping.Items.Clear();
                foreach (ContextIDHTMLPathMap pathMap in value)
                {
                    AddMappingNode(pathMap);
                }
            }
        }

        /// <summary>
        /// Property that indicates if the values in the treelist have changed (set to false on
        /// cancel so that they are not saved).
        /// </summary>
        public bool Changed
        {
            get {return m_bChanged;}
            set {m_bChanged = value;}
        }

        /// <summary>
        /// Utility to add a node to the treelist.
        /// </summary>
        /// <param name="pathMap"></param>
        private void AddMappingNode(ContextIDHTMLPathMap pathMap)
        {
            ListViewItem lvi = new ListViewItem(pathMap.HTMLPath);
            lvi.SubItems.Add(pathMap.ContextID);
            lvi.Tag = pathMap;
            lvMapping.Items.Add(lvi);
        }

        /// <summary>
        /// Hide the form on OK.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        /// <summary>
        /// Hide the form and cancel changes on Cancel.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Changed = false;
            this.Hide();
        }

        /// <summary>
        /// Begin editing the label of the selected item when they click this button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEditTopicFile_Click(object sender, EventArgs e)
        {
            if( lvMapping.SelectedItems.Count == 1 )
            {
                ListViewItem lvi = lvMapping.SelectedItems[0];
                lvi.BeginEdit();
            }
        }

        /// <summary>
        /// If the item has changed after editing the label, flag the dialog as changed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lvMapping_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            this.Changed = true;
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Product Manager
United States United States
I've been programming in C, C++, Visual Basic and C# for over 35 years. I've worked at Sierra Systems, ViewStar, Mosaix, Lucent, Avaya, Avinon, Apptero, Serena and now Guidewire Software in various roles over my career.

Comments and Discussions