tbf/Workplace/Forms/WizardMenu.cs

91 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Workplace.Resources;
namespace Workplace.Forms
{
public enum Selection
{
Submenu,
Leave,
Back,
Invalid,
}
public class WizardMenu
{
public readonly string DirPath;
public readonly ListBox ListBox;
public readonly string Title;
public readonly string[] Options;
public Selection Selection
{
get
{
if (ListBox.SelectedIndex == Options.Length)
{
return Selection.Back;
}
if (ListBox.SelectedIndex < 0 || ListBox.SelectedIndex > Options.Length || !Directory.Exists(SelectedPath))
{
return Selection.Invalid;
}
if (File.Exists(SelectedFile))
{
return Selection.Leave;
}
return Selection.Submenu;
}
}
/// Use the follwong properties only when Selection is Submenu or Leave
public string SelectedOption { get { return Options[ListBox.SelectedIndex]; } }
public string SelectedPath { get { return String.Format(@"{0}\{1}", DirPath, Options[ListBox.SelectedIndex]); } }
public string SelectedFile { get { return String.Format(@"{0}\{1}\weight.txt", DirPath, Options[ListBox.SelectedIndex]); } }
public WizardMenu(string dirPath, string title, int height)
{
DirPath = dirPath;
Title = title;
ListBox = new ListBox();
int maxWidth = 0;
Options = Directory.Exists(dirPath) ? Directory.GetDirectories(dirPath) : new string[0];
for (int i = 0; i < Options.Length; i++)
{
Options[i] = Path.GetFileName(Options[i]);
ListBox.Items.Add(Options[i]);
Size size = TextRenderer.MeasureText(Options[i], ListBox.Font);
if (size.Width > maxWidth) maxWidth = size.Width;
}
ListBox.Items.Add(string.Format("<{0}>", Strings.back));
ListBox.Width = maxWidth + 20;
ListBox.Height = height;
}
public void Enable(EventHandler eventHandler)
{
ListBox.Enabled = true;
ListBox.SelectedIndexChanged += eventHandler;
}
public void Disable(EventHandler eventHandler)
{
ListBox.Enabled = false;
ListBox.SelectedIndexChanged -= eventHandler;
}
}
}