DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: A very simple help window  (Read 3,988 times)

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
A very simple help window
« on: December 31, 2012, 03:19 PM »
I put together the basics for a very simple help/dialog window, and thought that it might be useful for some others.

It's NOT complete/production, but is a good basic help for someone to get started on a better dialog. i.e. You can use it as a launch pad to create something much better than the typical lame YES/NO dialog.

The basics are that it opens a dialog and displays buttons for people to click. The number of buttons can be arbitrary, e.g. 1 or 100. Buttons all display semi-nicely in rows.

Likely it's not all that useful except as a launch pad for ideas.

[attachurl=#]

Here's the basic code:

Code: (csharp)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Help
{
    public partial class HelpWindow : Form
    {
        public string Html = string.Empty;
        public string[] ButtonTexts;
        public string Value = string.Empty;

        // Don't use the default contructor. Use the override. 
        //public HelpWindow()
        //{
        //    InitializeComponent();
        //}

        /// <summary>
        /// A simple override to let you set the HTML of the browser and create an arbitrary number of buttons.
        /// </summary>
        /// <param name="html">The HTML for the browser.</param>
        /// <param name="buttonTexts">The text for each button. The form returns the text value for the clicked button when it closes.</param>
        public HelpWindow(string html, string[] buttonTexts)
        {
            InitializeComponent();
            Html = html;
            ButtonTexts = buttonTexts;

            int lastLeft = 24;
            List<Size> sizes = new List<Size>();
            int newTop = 16;
            for (int i = 0; i < ButtonTexts.Length; i++)
            {
                // Create a new button - this could be beefed up with a new custom type or something.
                Button b = new Button();
                b.Text = ButtonTexts[i];
                Size s = TextRenderer.MeasureText(b.Text, b.Font);
                sizes.Add(s);
                b.Width = s.Width + 10;
                b.Height = s.Height + 8;

                // check the width and see if the next button will fit.
                int totalWidth = 24;
                foreach (Size sz in sizes)
                {
                    // Add up the widths and some padding.
                    totalWidth += sz.Width + 10; //   the width of all previous, and the current one
                    // check if that is too wide for the current form width
                    if (totalWidth >= scPanel.Panel2.Width - 44) // we want a bit extra width on the right side so it's not jam packed.
                    {
                        // adjust the top down 24 with 10 padding - like a line feed LF
                        newTop += s.Height + 8;
                        // clear the sizes as we're starting over now
                        sizes = new List<Size>();
                        // add the current button again as we'll need it again in the next loop iteration
                        sizes.Add(s);
                        // Adjust the left back - like a carriage return CR
                        lastLeft = 24;
                        // Put the splitter up a bit to automatically show the lower buttons.
                        scPanel.SplitterDistance -= 34;
                        break;
                    }
                }

                b.Top = newTop;
                b.Left = lastLeft;
                b.Click += new EventHandler(b_Click);
                scPanel.Panel2.Controls.Add(b);
                lastLeft = b.Left + b.Width + 16;
            }
        }

        void b_Click(object sender, EventArgs e)
        {
            // Set the value and close the form. 
            this.Value = ((Button)sender).Text;
            this.Close();
        }

        private void HelpWindow_Load(object sender, EventArgs e)
        {
            // This is where you should load the HTML into a browser control or something like that. It should be in Panel1.
        }

        private void HelpWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Not really needed here. Do any last minute processing if you like here. 
        }
    }
}
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: A very simple help window
« Reply #1 on: December 31, 2012, 04:35 PM »
Ah, I should also post an example usage:

Code: (csharp)
            Help.HelpWindow h = new Help.HelpWindow("The buttons below are dynamic, and you can have as many as you like. <br /><br />Clicking one of the buttons returns the value clicked.", new string[] { "farty party~!", "new year", "bye!" });
            h.ShowDialog();
            Console.WriteLine("You clicked: <br/ >\"<b>" + h.Value + "</b>\"");
            h.Dispose();
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Original address: https://www.donationcoder.com/forum/index.php?topic=33494.0