view ServerMonitor/Forms/QuickHelpForm.cs @ 14:2db36ab759de

Add comments.
author Brad Greco <brad@bgreco.net>
date Mon, 22 Apr 2019 21:10:42 -0400
parents 3e1a2131f897
children
line wrap: on
line source

´╗┐using System;
using System.Drawing;
using System.Windows.Forms;

namespace ServerMonitorApp
{
    /// <summary>Form for showing help text in a floating popup.</summary>
    public partial class QuickHelpForm : Form
    {
        private string Rtf;

        public QuickHelpForm()
        {
            InitializeComponent();
        }

        /// <summary>Creates a help popup form with the provided text.</summary>
        /// <param name="rtf">Text to show in the popup in RTF format.</param>
        public QuickHelpForm(string rtf)
        {
            InitializeComponent();
            Rtf = rtf;
        }

        private void QuickHelpForm_Load(object sender, EventArgs e)
        {
            // Subscribe to the owner's notifications that the location of
            // the Help button that triggered this popup has changed.
            (Owner as CheckForm).HelpLocationChanged += Owner_HelpLocationChanged;
            // Subscribe to this event before setting the text box's text
            // so we can resize the form to match the text size.
            HelpTextBox.ContentsResized += HelpTextBox_ContentsResized;
            HelpTextBox.Rtf = Rtf;
        }

        /// <summary>Resizes the popup form to fit the displayed text.</summary>
        private void HelpTextBox_ContentsResized(object sender, ContentsResizedEventArgs e)
        {
            Height = e.NewRectangle.Height + 12;
        }

        /// <summary>Moves the popup form to always appear near the Help button that triggered it.</summary>
        private void Owner_HelpLocationChanged(object sender, HelpLocationChangedEventArgs e)
        {
            Point location = e.HelpLocation;
            location.Offset(24, 0);
            Location = location;
        }

        // Hides the popup form when ESC is pressed.
        private void Control_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
        }

        // Hides the popup form when it is clicked.
        private void Control_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}