using System.Linq; namespace RobvanderWoude { [System.Runtime.InteropServices.Guid( "72983C46-4D1F-47F7-AE13-C69B1EA3D56E" )] public interface MultipleChoiceBox_Interface { [System.Runtime.InteropServices.DispId( 1 )] void CheckUpdate( ); [System.Runtime.InteropServices.DispId( 2 )] string Credits( ); [System.Runtime.InteropServices.DispId( 3 )] string Help( int html = 0 ); [System.Runtime.InteropServices.DispId( 4 )] string ListProperties( ); [System.Runtime.InteropServices.DispId( 5 )] string SampleCode( ); [System.Runtime.InteropServices.DispId( 6 )] void Show( ); } [System.Runtime.InteropServices.Guid( "7569FB42-3E00-45E5-ADCA-67E231D51CDE" ), System.Runtime.InteropServices.InterfaceType( System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch )] public interface MultipleChoiceBox_Events { } [System.Runtime.InteropServices.Guid( "5FF30751-2030-437A-935C-44AE1E366FFC" ), System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.AutoDual ), System.Runtime.InteropServices.ComSourceInterfaces( typeof( MultipleChoiceBox_Events ) )] public class MultipleChoiceBox : MultipleChoiceBox_Interface { #region Default Values const string defaultcaptioncancel = "Cancel"; const string defaultcaptionok = "OK"; static readonly System.Drawing.FontFamily defaultfontfamily = System.Drawing.FontFamily.GenericSansSerif; const float defaultfontsize = 10; static readonly string defaulttitle = string.Format( "{0}, Version {1}", Global.Common.ProgramInfo.FileName, Global.Common.ProgramInfo.FileVersion ); const int defaultwindowheight = 90; const int defaultwindowwidth = 200; const float minimumfontsize = 6; const float maximumfontsize = 48; #endregion Default Values #region Controls static System.Timers.Timer timer; static System.Windows.Forms.Form multiplechoiceform; static System.Windows.Forms.GroupBox groupbox; #endregion Controls #region Methods public void CheckUpdate( ) { Global.Common.ProgramInfo.CheckUpdate( ); } public string Credits( ) { return Global.Common.Credits( ); } public string Help( int html = 0 ) { int col1width = 0; int col2width = 0; int col3width = 0; int col4width = 0; int col5width = 0; string help = string.Empty; string linetemplate = string.Empty; string separatorline = string.Empty; if ( html == 1 ) { help += "

Help for MultipleChoiceBox class

\n\n"; help += "

Present a dialog with a group of checkboxes, and return the selected items

\n\n"; help += "

COM ProgID: RobvanderWoude.MultipleChoiceBox

\n\n"; } else { help += "Help for MultipleChoiceBox class\n"; help += new string( '\u2500', help.Length ) + "\n\n"; help += "Present a dialog with a group of checkboxes, and return the selected items\n\n\n"; help += "COM ProgID: RobvanderWoude.MultipleChoiceBox\n\n\n"; } #region Properties if ( html == 1 ) { separatorline = string.Empty; linetemplate = "\n\t{0}\n\t{1}\n\t{2}\n\t{3}\n\t{4}\n\n"; help += "\n"; help += "\n"; help += "\t\n"; help += "\n"; help += "\n"; help += "\t\n"; help += "\t\n"; help += "\t\n"; help += "\t\n"; help += "\t\n"; help += "\n"; } else { col1width = 18; col2width = 50; col3width = 9; col4width = 9; col5width = 42; separatorline = "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u253C" + new string( '\u2500', 2 + col3width ) + "\u253C" + new string( '\u2500', 2 + col4width ) + "\u253C" + new string( '\u2500', 2 + col5width ) + "\u2524\n"; linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502 {3,-" + col4width.ToString( ) + "} \u2502 {4,-" + col5width.ToString( ) + "} \u2502\n"; help += "\u250C" + new string( '\u2500', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2510\n"; help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n"; help += string.Format( "\u2502 {0,-" + ( 12 + col1width + col2width + col3width + col4width + col5width ).ToString( ) + "} \u2502\n", "Properties (Note that all properties are string or integer, no booleans, objects or arrays)" ); help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n"; help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u2564" + new string( '\u2550', 2 + col2width ) + "\u2564" + new string( '\u2550', 2 + col3width ) + "\u2564" + new string( '\u2550', 2 + col4width ) + "\u2564" + new string( '\u2550', 2 + col5width ) + "\u2561\n"; help += string.Format( linetemplate, "Property Name", "Description", "Mandatory", "Read-Only", "Default (Allowed) Values" ); help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u256A" + new string( '\u2550', 2 + col2width ) + "\u256A" + new string( '\u2550', 2 + col3width ) + "\u256A" + new string( '\u2550', 2 + col4width ) + "\u256A" + new string( '\u2550', 2 + col5width ) + "\u2561\n"; } help += HelpTableRow( linetemplate, "captioncancel", Global.Common.Help.captioncancel, "no", "no", defaultcaptioncancel, html ); help += separatorline; help += HelpTableRow( linetemplate, "captionok", Global.Common.Help.captionok, "no", "no", defaultcaptionok, html ); help += separatorline; help += HelpTableRow( linetemplate, "columns", "The number of columns", "no", "no", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "debuginfo", Global.Common.Help.debuginfo, "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "defaultindex", "The index of the preselected item in the list", "no", "no", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "errors", Global.Common.Help.errors, "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "fontfamily", Global.Common.Help.fontfamily, "no", "no", defaultfontfamily.Name, html ); help += separatorline; help += HelpTableRow( linetemplate, "fontsize", Global.Common.Help.fontsize, "no", "no", string.Format( "{0} ({1}..{2})", defaultfontsize, minimumfontsize, maximumfontsize ), html ); help += separatorline; help += HelpTableRow( linetemplate, "left", Global.Common.Help.left, "no", "no", "Centered (0..screenwidth-windowwidth)", html ); help += separatorline; if ( html == 1 ) { string template = "\n\t\n\t\n\t\n\t\n\t\n\n"; help += string.Format( template, "list", "Semicolon-separated list of items", "* YES", "no", "* Set either \"list\" or \"listfile\" property" ); template = "\n\t\n\t\n\t\n\n"; help += string.Format( template, "listfile", "Path to file containing list", "no" ); } else { help += HelpTableRow( linetemplate, "list", "Semicolon-separated list of items", "", "no", "", html ); help += "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u2524 " + string.Format( "{0,-" + col3width + "}", "* YES" ) + " \u251C" + new string( '\u2500', 2 + col4width ) + "\u2524 " + string.Format( "{0,-" + col5width + "}", "* Set either \"list\" or \"listfile\" property" ) + " \u2502\n"; help += HelpTableRow( linetemplate, "listfile", "Path to file containing list", "", "no", new string( ' ', 6 ) + new string( '\u2550', 6 ) + new string( ' ', 8 ) + new string( '\u2550', 2 ), html ); } help += separatorline; help += HelpTableRow( linetemplate, "literal", Global.Common.Help.literal, "no", "no", "0: interpret \"\\t\" as tab and \"\\n\" as newline", html ); help += separatorline; help += HelpTableRow( linetemplate, "localizecaptions", Global.Common.Help.localizecaptions, "no", "no", "0: English captions", html ); help += separatorline; help += HelpTableRow( linetemplate, "modal", Global.Common.Help.modal, "no", "no", "1: always on top", html ); help += separatorline; help += HelpTableRow( linetemplate, "prompt", Global.Common.Help.prompt, "no", "no", "No prompt", html ); help += separatorline; help += HelpTableRow( linetemplate, "selectedindex", "Index of last selected radio button item", "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "selecteditem", "Text of last selected radio button item", "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeout", Global.Common.Help.timeout, "no", "no", "0: no timeout", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeoutelapsed", Global.Common.Help.timeoutelapsed, "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "title", Global.Common.Help.title, "no", "no", defaulttitle, html ); help += separatorline; help += HelpTableRow( linetemplate, "top", Global.Common.Help.top, "no", "no", "Centered (0..screenheight-windowheight)", html ); help += separatorline; help += HelpTableRow( linetemplate, "Version", Global.Common.Help.version, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "windowheight", "Height of dialog window", "no", "no", string.Format( "{0} ({0}..screenheight)", defaultwindowheight ), html ); help += separatorline; help += HelpTableRow( linetemplate, "windowwidth", "Width of dialog window", "no", "no", string.Format( "{0} ({0}..screenwidth)", defaultwindowwidth ), html ); if ( html == 1 ) { help += "
Properties (Note that all properties are string or integer, no booleans, objects or arrays)
Property NameDescriptionMandatoryRead-OnlyDefault (Allowed) Values
{0}{1}{2}{3}{4}
{0}{1}{2}
\n\n\n"; } else { help += "\u2514" + new string( '\u2500', 2 + col1width ) + "\u2534" + new string( '\u2500', 2 + col2width ) + "\u2534" + new string( '\u2500', 2 + col3width ) + "\u2534" + new string( '\u2500', 2 + col4width ) + "\u2534" + new string( '\u2500', 2 + col5width ) + "\u2518\n\n\n"; } #endregion Properties #region Methods if ( html == 1 ) { separatorline = string.Empty; linetemplate = "\n\t{0}\n\t{1}\n\t{2}\n\n"; help += "\n"; help += "\n"; help += "\t\n"; help += "\n"; help += "\n"; help += "\t\n"; help += "\t\n"; help += "\t\n"; help += "\n"; } else { col1width = 21; col2width = 74; col3width = 39; separatorline = "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u253C" + new string( '\u2500', 2 + col3width ) + "\u2524\n"; linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502\n"; help += "\u250C" + new string( '\u2500', 8 + col1width + col2width + col3width ) + "\u2510\n"; help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n"; help += string.Format( "\u2502 {0,-" + ( 6 + col1width + col2width + col3width ).ToString( ) + "} \u2502\n", "Methods" ); help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n"; help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u2564" + new string( '\u2550', 2 + col2width ) + "\u2564" + new string( '\u2550', 2 + col3width ) + "\u2561\n"; help += string.Format( linetemplate, "Method Name", "Description", "Requirements" ); help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u256A" + new string( '\u2550', 2 + col2width ) + "\u256A" + new string( '\u2550', 2 + col3width ) + "\u2561\n"; } help += HelpTableRow( linetemplate, "CheckUpdate", Global.Common.Help._checkupdate, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "Credits", Global.Common.Help._credits, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "Help", Global.Common.Help._help, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "ListProperties", Global.Common.Help._listproperties, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "SampleCode", Global.Common.Help._samplecode, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "Show", "Presents a dialog with a group of radio buttons based on the current property values, and saves the text for the selected item in the \"selecteditem\" property, and its index (in list) in the \"selectedindex\" property.", "", html ); if ( html == 1 ) { help += "
Methods
Method NameDescriptionRequirements
\n\n\n"; } else { help += "\u2514" + new string( '\u2500', 2 + col1width ) + "\u2534" + new string( '\u2500', 2 + col2width ) + "\u2534" + new string( '\u2500', 2 + col3width ) + "\u2518\n\n\n"; } #endregion Methods #region Example if ( html == 1 ) { help += "

VBScript usage example:

\n\n
";
			}
			else
			{
				help += "VBScript usage example:\n";
				help += new string( '\u2500', 23 ) + "\n\n";
			}

			help += SampleCode( );

			if ( html == 1 )
			{
				help += "
"; } help += "\n\n"; #endregion Example help = help.Replace( "\n", System.Environment.NewLine ); return help; } private string HelpTableRow( string template, string col1text, string col2text, string col3text, int html ) { if ( html == 1 ) { return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text ); } else { return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, "", "" ); } } private string HelpTableRow( string template, string col1text, string col2text, string col3text, string col4text, string col5text, int html ) { if ( html == 1 ) { return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text, col4text, col5text ); } else { return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, col4text, col5text ); } } public string ListProperties( ) { return Global.Common.Lists.Properties( this ); } public string SampleCode( ) { string code = "Set objMultipleChoiceBox = CreateObject( \"RobvanderWoude.MultipleChoiceBox\" )\n\n"; code += "With objMultipleChoiceBox\n"; code += "\t.list = \"Bungeejump from helicopter;Firewalk;Paraglide into an active volcan\"\n"; code += "\t.preselected = \"0;1;2\"\n"; code += "\t.prompt = \"15 seconds to pick your favorites:\"\n"; code += "\t.title = \"My Bucket List\"\n"; code += "\t.top = 200\n"; code += "\t.left = 300\n"; code += "\t.timeout = 15\n"; code += "\t.Show\n"; code += "\tWScript.Echo \"Selected bucket list items: \" & .selecteditems\n"; code += "\tWScript.Echo\n"; code += "\tWScript.Echo .ListProperties( )\n"; code += "End With\n\n"; code += "Set objMultipleChoiceBox = Nothing"; return code; } [System.STAThread] public void Show( ) { int tablength = 4; double cbsafetyfactor = 1.05; #region Timer _timeoutelapsed = false; if ( timeout > 0 ) { timer = new System.Timers.Timer( ); timer.Elapsed += new System.Timers.ElapsedEventHandler( MultipleChoiceBox_Class_Timer_Elapsed ); timer.Interval = _timeout; timer.Start( ); } #endregion Timer #region Main Form multiplechoiceform = new System.Windows.Forms.Form( ); multiplechoiceform.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; multiplechoiceform.MaximizeBox = false; multiplechoiceform.MinimizeBox = false; if ( left < 0 || top < 0 ) { multiplechoiceform.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; } else { multiplechoiceform.StartPosition = System.Windows.Forms.FormStartPosition.Manual; multiplechoiceform.Location = new System.Drawing.Point( left, top ); } multiplechoiceform.Text = title; multiplechoiceform.TopMost = topmost; #endregion Main Form #region Initial Sizes int horizontalmargin = 10; int verticalmargin = 10; int promptwidth = 0; int promptheight = 20; int cbtextheight = 0; // checkbox text height int cbtextwidth = 0; // checkbox text width int cbwidth = 15; // checkbox width without text int buttonheight = 25; int buttonwidth = 80; #endregion Initial Sizes #region Prompt System.Windows.Forms.Label labelprompt = new System.Windows.Forms.Label( ); if ( string.IsNullOrWhiteSpace( prompt ) ) { promptheight = -1 * verticalmargin; } else { // Calculate required height for single prompt line promptheight = System.Windows.Forms.TextRenderer.MeasureText( "Test", labelprompt.Font ).Height; // Replace tabs with spaces if ( prompt.IndexOf( "\\t", System.StringComparison.Ordinal ) > -1 ) { string tab = new string( ' ', tablength ); // First split the prompt on newlines string[] prompt2 = prompt.Split( new string[] { "\\n" }, System.StringSplitOptions.None ); for ( int i = 0; i < prompt2.Length; i++ ) { if ( prompt2[i].IndexOf( "\\t", System.StringComparison.Ordinal ) > -1 ) { // Split each "sub-line" of the prompt on tabs string[] prompt3 = prompt2[i].Split( new string[] { "\\t" }, System.StringSplitOptions.None ); // Each substring before a tab gets n spaces attached, and then is shortened to the greatest possible multiple of n for ( int j = 0; j < prompt3.Length - 1; j++ ) { prompt3[j] += tab; int length = prompt3[j].Length; length /= tablength; length *= tablength; prompt3[j] = prompt3[j].Substring( 0, length ); } prompt2[i] = string.Join( "", prompt3 ); } } prompt = string.Join( "\n", prompt2 ); } if ( !_literal ) { prompt = prompt.Replace( "\\n", "\n" ).Replace( "\\r", "\r" ).Replace( "\\t", "\t" ); } labelprompt.Text = prompt; string[] lines = prompt.Split( "\n".ToCharArray( ) ); int promptlines = lines.Length; foreach ( string line in lines ) { promptwidth = System.Math.Max( promptwidth, System.Windows.Forms.TextRenderer.MeasureText( line, labelprompt.Font ).Width ); } // Calculate required height for multiple line prompt promptheight = promptlines * promptheight; } #endregion Prompt #region Checkboxes System.Collections.Generic.List checkboxes = new System.Collections.Generic.List( ); for ( int i = 0; i < _list.Count; i++ ) { System.Windows.Forms.CheckBox checkbox = new System.Windows.Forms.CheckBox( ); checkbox.Text = _list[i]; checkbox.Checked = _preselected.Contains( i ); cbtextwidth = System.Math.Max( cbtextwidth, System.Convert.ToInt32( System.Windows.Forms.TextRenderer.MeasureText( _list[i], checkbox.Font ).Width * cbsafetyfactor ) ); cbtextheight = System.Math.Max( cbtextheight, System.Convert.ToInt32( System.Windows.Forms.TextRenderer.MeasureText( _list[i], checkbox.Font ).Height * cbsafetyfactor ) ); checkboxes.Add( checkbox ); } groupbox = new System.Windows.Forms.GroupBox( ); #endregion Checkboxes #region Buttons System.Windows.Forms.Button okButton = new System.Windows.Forms.Button( ); okButton.DialogResult = System.Windows.Forms.DialogResult.OK; okButton.Name = "okButton"; okButton.Text = captionok; System.Windows.Forms.Button cancelButton = new System.Windows.Forms.Button( ); cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; cancelButton.Name = "cancelButton"; cancelButton.Text = captioncancel; #endregion Buttons #region Calculate Window Layout if ( rows > 0 ) { rows = System.Math.Min( rows, _list.Count ); columns = (int) System.Math.Floor( (decimal) ( _list.Count + rows - 1 ) / rows ); } else if ( columns > 0 ) { columns = System.Math.Min( columns, _list.Count ); rows = (int) System.Math.Floor( (decimal) ( _list.Count + columns - 1 ) / columns ); } else { columns = 1; rows = _list.Count; } int cbgroupwidth = 0; int cbgroupheight = 0; int rowheight = 0; int colwidth = 0; if ( windowwidth != defaultwindowwidth ) { cbgroupwidth = windowwidth - 2 * horizontalmargin; colwidth = (int) System.Math.Floor( (decimal) ( cbgroupwidth - horizontalmargin - columns * ( cbwidth + horizontalmargin ) ) / columns ); } else { colwidth = cbtextwidth + cbwidth; cbgroupwidth = System.Math.Max( defaultwindowwidth - 2 * horizontalmargin, columns * ( colwidth + horizontalmargin ) + 2 * horizontalmargin ); colwidth = System.Math.Max( colwidth, System.Convert.ToInt32( ( cbgroupwidth - 2 * horizontalmargin ) / columns ) - horizontalmargin ); windowwidth = System.Math.Max( defaultwindowwidth, cbgroupwidth + 2 * horizontalmargin ); } if ( windowheight != defaultwindowheight ) { cbgroupheight = windowheight - promptheight - buttonheight - 4 * verticalmargin; rowheight = (int) System.Math.Floor( (decimal) ( cbgroupheight - 2 * verticalmargin ) / rows ); } else { rowheight = cbtextheight + verticalmargin; windowheight = System.Math.Max( defaultwindowheight, 6 * verticalmargin + promptheight + buttonheight + rows * rowheight ); cbgroupheight = rows * rowheight + 2 * verticalmargin; } #endregion Calculate Window Layout #region Check Available Group Box Space if ( cbgroupheight / rows < rowheight || cbgroupwidth / columns < colwidth ) { _errors.Add( timestamp + "Window size too small to display all checkboxes; increase window size, reduce or remove prompt, and/or change number of rows and columns" ); } #endregion Check Available Group Box Space #region Build Form System.Drawing.Size windowsize = new System.Drawing.Size( windowwidth, windowheight ); multiplechoiceform.ClientSize = windowsize; if ( !string.IsNullOrWhiteSpace( prompt ) ) { labelprompt.Size = new System.Drawing.Size( windowwidth - 2 * horizontalmargin, promptheight ); labelprompt.Location = new System.Drawing.Point( horizontalmargin, verticalmargin ); multiplechoiceform.Controls.Add( labelprompt ); } groupbox.Size = new System.Drawing.Size( cbgroupwidth, cbgroupheight ); groupbox.Location = new System.Drawing.Point( horizontalmargin, promptheight + 2 * verticalmargin ); foreach ( System.Windows.Forms.CheckBox checkbox in checkboxes ) { groupbox.Controls.Add( checkbox ); } multiplechoiceform.Controls.Add( groupbox ); for ( int row = 0; row < rows; row++ ) { for ( int column = 0; column < columns; column++ ) { int index = row * columns + column; if ( index < checkboxes.Count ) { int x = System.Convert.ToInt32( horizontalmargin + column * ( colwidth + horizontalmargin ) ); int y = System.Convert.ToInt32( verticalmargin + row * rowheight ); checkboxes[index].Size = new System.Drawing.Size( colwidth + horizontalmargin, rowheight ); checkboxes[index].Location = new System.Drawing.Point( x, y ); } } } okButton.Size = new System.Drawing.Size( buttonwidth, buttonheight ); okButton.Location = new System.Drawing.Point( windowwidth / 2 - horizontalmargin - buttonwidth, windowheight - buttonheight - verticalmargin ); multiplechoiceform.Controls.Add( okButton ); cancelButton.Size = new System.Drawing.Size( buttonwidth, buttonheight ); cancelButton.Location = new System.Drawing.Point( windowwidth / 2 + horizontalmargin, windowheight - buttonheight - verticalmargin ); multiplechoiceform.Controls.Add( cancelButton ); multiplechoiceform.AcceptButton = okButton; // OK on Enter multiplechoiceform.CancelButton = cancelButton; // Cancel on Esc #endregion Build Form #region Show Dialog multiplechoiceform.TopMost = topmost; System.Windows.Forms.DialogResult result = multiplechoiceform.ShowDialog( ); if ( !_timeoutelapsed ) { _selecteditems.Clear( ); _selectedindices.Clear( ); } if ( result == System.Windows.Forms.DialogResult.OK ) { for ( int i = 0; i < groupbox.Controls.Count; i++ ) { System.Windows.Forms.Control control = groupbox.Controls[i]; if ( control.GetType( ) == typeof( System.Windows.Forms.CheckBox ) ) { System.Windows.Forms.CheckBox cb = (System.Windows.Forms.CheckBox) control; if ( cb.Checked ) { _selecteditems.Add( cb.Text ); _selectedindices.Add( i ); } } } } #endregion Show Dialog } #endregion Methods #region Properties private string _captioncancel = defaultcaptioncancel; [System.Runtime.InteropServices.ComVisible( true )] public string captioncancel { get { if ( _localizecaptions && _captioncancel == defaultcaptioncancel ) { _captioncancel = Global.Common.Localization.Load( "user32.dll", 801, defaultcaptioncancel ); } return _captioncancel; } set { if ( string.IsNullOrWhiteSpace( value ) || value.Length > 20 ) { _captioncancel = defaultcaptioncancel; } else { _captioncancel = value; } //_debuginfo.Add() } } private string _captionok = defaultcaptionok; [System.Runtime.InteropServices.ComVisible( true )] public string captionok { get { if ( _localizecaptions && _captionok == defaultcaptionok ) { _captionok = Global.Common.Localization.Load( "user32.dll", 800, defaultcaptionok ); } return _captionok; } set { if ( string.IsNullOrWhiteSpace( value ) || value.Length > 20 ) { _captionok = defaultcaptionok; } else { _captionok = value; } } } private int _columns = 0; [System.Runtime.InteropServices.ComVisible( true )] public int columns { get { return _columns; } set { _columns = value; } } private System.Collections.Generic.List _debuginfo = new System.Collections.Generic.List( ); [System.Runtime.InteropServices.ComVisible( true )] public string debuginfo { get { return string.Join( "\n", _debuginfo.ToArray( ) ); } } private System.Collections.Generic.List _errors = new System.Collections.Generic.List( ); [System.Runtime.InteropServices.ComVisible( true )] public string errors { get { return string.Join( "\n", _errors.ToArray( ) ); } } private static bool _fixedpitchonly = false; [System.Runtime.InteropServices.ComVisible( true )] public int fixedpitchonly { get { return ( _fixedpitchonly ? 1 : 0 ); } set { _fixedpitchonly = ( value == 1 ); } } private System.Drawing.FontFamily _fontfamily = defaultfontfamily; [System.Runtime.InteropServices.ComVisible( true )] public string fontfamily { get { return _fontfamily.Name; } set { _fontfamily = Global.Common.Validate.FontFamily( value, defaultfontfamily ); } } private float _fontsize = defaultfontsize; [System.Runtime.InteropServices.ComVisible( true )] public int fontsize { get { return System.Convert.ToInt32( _fontsize ); } set { _fontsize = System.Convert.ToSingle( value ).LimitToRange( minimumfontsize, maximumfontsize ); } } private int _left = -1; [System.Runtime.InteropServices.ComVisible( true )] public int left { get { return _left; } set { if ( value < 0 ) { _left = 0; } else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - windowwidth ) { _left = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - windowwidth; } else { _left = value; } } } private System.Collections.Generic.List _list = new System.Collections.Generic.List( ); [System.Runtime.InteropServices.ComVisible( true )] public string list { get { return string.Join( ";", _list ); } set { _list = value.Split( ";\n".ToCharArray( ), System.StringSplitOptions.RemoveEmptyEntries ).Select( i => i.Trim( ) ).ToList( ); } } [System.Runtime.InteropServices.ComVisible( true )] public string listfile { get { return string.Join( ";", _list ); } set { if ( System.IO.File.Exists( value ) ) { try { System.IO.StreamReader sr = new System.IO.StreamReader( list ); string text = sr.ReadToEnd( ); sr.Close( ); if ( text.IndexOfAny( ";\n\r".ToCharArray( ) ) == -1 ) { _errors.Add( timestamp + "Invalid list file content" ); } _list = text.Split( ";\n\r".ToCharArray( ), System.StringSplitOptions.RemoveEmptyEntries ).Select( i => i.Trim( ) ).ToList( ); } catch ( System.Exception e ) { _errors.Add( timestamp + "Unable to open list file: " + e.Message ); } } else { _errors.Add( timestamp + "List file not found" ); } } } private bool _literal = false; [System.Runtime.InteropServices.ComVisible( true )] public int literal { get { return ( _literal ? 1 : 0 ); } set { _literal = ( value == 1 ); } } private bool _localizecaptions = false; [System.Runtime.InteropServices.ComVisible( true )] public int localizecaptions { get { return ( _localizecaptions ? 1 : 0 ); } set { _localizecaptions = ( value == 1 ); } } private bool topmost = true; [System.Runtime.InteropServices.ComVisible( true )] public int modal { get { return ( topmost ? 1 : 0 ); } set { topmost = ( value == 1 ); } } private System.Collections.Generic.List _preselected = new System.Collections.Generic.List( ); [System.Runtime.InteropServices.ComVisible( true )] public string preselected { get { return string.Join( ";", _preselected ); } set { if ( System.Text.RegularExpressions.Regex.IsMatch( value, @"^\d+(;\d+)*$" ) ) { _preselected.Clear( ); _preselected = value.Split( ";".ToCharArray( ) ).Select( v => int.Parse( v ) ).Distinct( ).ToList( ); } } } private string _prompt = string.Empty; [System.Runtime.InteropServices.ComVisible( true )] public string prompt { get { return _prompt; } set { if ( _literal ) { _prompt = value.Trim( ); } else { _prompt = value.Replace( "\\n", "\n" ).Replace( "\\t", "\t" ).Trim( ); } } } private int _rows = 0; [System.Runtime.InteropServices.ComVisible( true )] public int rows { get { return _rows; } set { _rows = value; } } private System.Collections.Generic.List _selectedindices = new System.Collections.Generic.List( ); [System.Runtime.InteropServices.ComVisible( true )] public string selectedindices { get { return string.Join( ";", _selectedindices ); } } private static System.Collections.Generic.List _selecteditems = new System.Collections.Generic.List( ); [System.Runtime.InteropServices.ComVisible( true )] public string selecteditems { get { return string.Join( ";", _selecteditems ); } } private int _timeout = 0; [System.Runtime.InteropServices.ComVisible( true )] public int timeout { get { return (int) ( _timeout / 1000 ); } set { if ( value < 0 || value > 3600 ) { _timeout = 0; } else { _timeout = value * 1000; } } } private bool _timeoutelapsed = false; [System.Runtime.InteropServices.ComVisible( true )] public int timeoutelapsed { get { return ( _timeoutelapsed ? 1 : 0 ); } } private static string timestamp => Global.Common.TimeStamp( ); private string _title = defaulttitle; [System.Runtime.InteropServices.ComVisible( true )] public string title { get { return _title; } set { if ( string.IsNullOrWhiteSpace( value ) ) { _title = defaulttitle; } else { _title = value.Trim( ); } } } private int _top = -1; [System.Runtime.InteropServices.ComVisible( true )] public int top { get { return _top; } set { if ( value < 0 ) { _top = 0; } else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - windowheight ) { _top = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - windowheight; } else { _top = value; } } } [System.Runtime.InteropServices.ComVisible( true )] public string Version { get { return Global.Common.ProgramInfo.FileVersion; } } private int _windowheight = defaultwindowheight; [System.Runtime.InteropServices.ComVisible( true )] public int windowheight { get { return _windowheight; } set { if ( value < defaultwindowheight ) { _windowheight = defaultwindowheight; } else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height ) { _windowheight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; } else { _windowheight = value; } } } private int _windowwidth = defaultwindowwidth; [System.Runtime.InteropServices.ComVisible( true )] public int windowwidth { get { return _windowwidth; } set { if ( value < defaultwindowwidth ) { _windowwidth = defaultwindowwidth; } else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width ) { _windowwidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; } else { _windowwidth = value; } } } #endregion Properties #region Event Handlers private void MultipleChoiceBox_Class_CheckedChanged( object sender, System.EventArgs e ) { _selecteditems.Clear( ); _selectedindices.Clear( ); foreach ( System.Windows.Forms.CheckBox cb in groupbox.Controls ) { if ( cb.Checked ) { _selecteditems.Add( cb.Text ); _selectedindices.Add( _list.IndexOf( cb.Text ) ); } } } private void MultipleChoiceBox_Class_Timer_Elapsed( object sender, System.Timers.ElapsedEventArgs e ) { _timeoutelapsed = true; foreach ( System.Windows.Forms.CheckBox cb in groupbox.Controls ) { if ( cb.Checked ) { _selecteditems.Add( cb.Text ); _selectedindices.Add( _list.IndexOf( cb.Text ) ); } } multiplechoiceform.Close( ); } #endregion Event Handlers } }