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.
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.
}
}
}