topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday December 13, 2024, 1:54 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: A very simple help window  (Read 3051 times)

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
A very simple help window
« on: December 31, 2012, 09:19 AM »
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.

* Help.zip (11.01 kB - downloaded 268 times.)

Here's the basic code:

Code: C# [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Help
  11. {
  12.     public partial class HelpWindow : Form
  13.     {
  14.         public string Html = string.Empty;
  15.         public string[] ButtonTexts;
  16.         public string Value = string.Empty;
  17.  
  18.         // Don't use the default contructor. Use the override.
  19.         //public HelpWindow()
  20.         //{
  21.         //    InitializeComponent();
  22.         //}
  23.  
  24.         /// <summary>
  25.         /// A simple override to let you set the HTML of the browser and create an arbitrary number of buttons.
  26.         /// </summary>
  27.         /// <param name="html">The HTML for the browser.</param>
  28.         /// <param name="buttonTexts">The text for each button. The form returns the text value for the clicked button when it closes.</param>
  29.         public HelpWindow(string html, string[] buttonTexts)
  30.         {
  31.             InitializeComponent();
  32.             Html = html;
  33.             ButtonTexts = buttonTexts;
  34.  
  35.             int lastLeft = 24;
  36.             List<Size> sizes = new List<Size>();
  37.             int newTop = 16;
  38.             for (int i = 0; i < ButtonTexts.Length; i++)
  39.             {
  40.                 // Create a new button - this could be beefed up with a new custom type or something.
  41.                 Button b = new Button();
  42.                 b.Text = ButtonTexts[i];
  43.                 Size s = TextRenderer.MeasureText(b.Text, b.Font);
  44.                 sizes.Add(s);
  45.                 b.Width = s.Width + 10;
  46.                 b.Height = s.Height + 8;
  47.  
  48.                 // check the width and see if the next button will fit.
  49.                 int totalWidth = 24;
  50.                 foreach (Size sz in sizes)
  51.                 {
  52.                     // Add up the widths and some padding.
  53.                     totalWidth += sz.Width + 10; //   the width of all previous, and the current one
  54.                     // check if that is too wide for the current form width
  55.                     if (totalWidth >= scPanel.Panel2.Width - 44) // we want a bit extra width on the right side so it's not jam packed.
  56.                     {
  57.                         // adjust the top down 24 with 10 padding - like a line feed LF
  58.                         newTop += s.Height + 8;
  59.                         // clear the sizes as we're starting over now
  60.                         sizes = new List<Size>();
  61.                         // add the current button again as we'll need it again in the next loop iteration
  62.                         sizes.Add(s);
  63.                         // Adjust the left back - like a carriage return CR
  64.                         lastLeft = 24;
  65.                         // Put the splitter up a bit to automatically show the lower buttons.
  66.                         scPanel.SplitterDistance -= 34;
  67.                         break;
  68.                     }
  69.                 }
  70.  
  71.                 b.Top = newTop;
  72.                 b.Left = lastLeft;
  73.                 b.Click += new EventHandler(b_Click);
  74.                 scPanel.Panel2.Controls.Add(b);
  75.                 lastLeft = b.Left + b.Width + 16;
  76.             }
  77.         }
  78.  
  79.         void b_Click(object sender, EventArgs e)
  80.         {
  81.             // Set the value and close the form.
  82.             this.Value = ((Button)sender).Text;
  83.             this.Close();
  84.         }
  85.  
  86.         private void HelpWindow_Load(object sender, EventArgs e)
  87.         {
  88.             // This is where you should load the HTML into a browser control or something like that. It should be in Panel1.
  89.         }
  90.  
  91.         private void HelpWindow_FormClosing(object sender, FormClosingEventArgs e)
  92.         {
  93.             // Not really needed here. Do any last minute processing if you like here.
  94.         }
  95.     }
  96. }
Slow Down Music - Where I commit thought crimes...

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

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: A very simple help window
« Reply #1 on: December 31, 2012, 10:35 AM »
Ah, I should also post an example usage:

Code: C# [Select]
  1. 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!" });
  2.             h.ShowDialog();
  3.             Console.WriteLine("You clicked: <br/ >\"<b>" + h.Value + "</b>\"");
  4.             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