namespace RobvanderWoude { [System.Runtime.InteropServices.Guid( "FDA07CE5-CC3B-416F-8AEE-B95A8623D58A" )] public interface MessageBox_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 ListMessageBoxButtons( ); [System.Runtime.InteropServices.DispId( 5 )] string ListMessageBoxIcons( ); [System.Runtime.InteropServices.DispId( 6 )] string ListMessageBoxOptions( ); [System.Runtime.InteropServices.DispId( 7 )] string ListProperties( ); [System.Runtime.InteropServices.DispId( 8 )] string SampleCode( ); [System.Runtime.InteropServices.DispId( 9 )] void Show( ); } [System.Runtime.InteropServices.Guid( "705402EE-8469-445A-8B6D-06092B68DE89" ), System.Runtime.InteropServices.InterfaceType( System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch )] public interface MessageBox_Events { } [System.Runtime.InteropServices.Guid( "734BD1C4-EBDB-4F64-8118-E0497756A7F4" ), System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.AutoDual ), System.Runtime.InteropServices.ComSourceInterfaces( typeof( MessageBox_Events ) )] public class MessageBox : MessageBox_Interface { #region Default values static readonly System.Windows.Forms.MessageBoxButtons defaultbuttonset = System.Windows.Forms.MessageBoxButtons.OK; const string defaultcaptioncancel = "Cancel"; const string defaultcaptionok = "OK"; static readonly System.Windows.Forms.MessageBoxIcon defaulticon = System.Windows.Forms.MessageBoxIcon.None; static readonly System.Windows.Forms.MessageBoxOptions defaultoptions = System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly | System.Windows.Forms.MessageBoxOptions.ServiceNotification; const string defaultmessage = ""; static readonly string defaulttitle = string.Format( "{0}, Version {1}", Global.Common.ProgramInfo.FileName, Global.Common.ProgramInfo.FileVersion ); #endregion Default values #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 MessageBox class

\n\n"; help += "

Present a message dialog, and return the caption of the button clicked

\n\n"; help += "

COM ProgID: RobvanderWoude.MessageBox

\n\n"; } else { help += "Help for MessageBox class\n"; help += new string( '\u2500', help.Length ) + "\n\n"; help += "Present a message dialog, and return the caption of the button clicked\n\n\n"; help += "COM ProgID: RobvanderWoude.MessageBox\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 = 20; col2width = 50; col3width = 9; col4width = 9; col5width = 40; 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, "buttons", "The buttons presented in the dialog", "no", "no", "OK (" + Global.Common.Lists.MessageBoxButtons( ).Replace( ";", ", " ) + ")", html ); help += separatorline; help += HelpTableRow( linetemplate, "buttonclicked", "The caption of the button that was clicked", "no", "no", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "defaultbutton", "The button that has focus when the dialog opens", "no", "no", "Button1 (Button1, Button2, Button3)", html ); help += separatorline; help += HelpTableRow( linetemplate, "debuginfo", Global.Common.Help.debuginfo, "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "errors", Global.Common.Help.errors, "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "icons", "The icon in the dialog", "no", "no", defaulticon.ToString( ) + " (" + Global.Common.Lists.MessageBoxIcons( ).Replace( ";", ", " ) + ")", html ); help += separatorline; help += HelpTableRow( linetemplate, "message", "The message to be displayed in the dialog", "no", "no", defaultmessage, html ); help += separatorline; help += HelpTableRow( linetemplate, "options", "The options for the dialog", "no", "no", defaultoptions.ToString( ) + " (" + Global.Common.Lists.MessageBoxOptions( ).Replace( ";", ", " ) + ")", html ); help += separatorline; help += HelpTableRow( linetemplate, "title", Global.Common.Help.title, "no", "no", defaulttitle, html ); help += separatorline; help += HelpTableRow( linetemplate, "Version", Global.Common.Help.version, "N/A", "YES", "", html ); if ( html == 1 ) { help += "
Properties (Note that all properties are string or integer, no booleans, objects or arrays)
Property NameDescriptionMandatoryRead-OnlyDefault (Allowed) Values
\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, "ListMessageBoxButtons", "Return a semicolon-separated list of available buttons", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "ListMessageBoxIcons", "Return a semicolon-separated list of available icons", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "ListMessageBoxOptions", "Return a semicolon-separated list of available options", "", 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 message dialog based on the current property values, and saves the caption of the button that is clicked in the \"buttonclicked\" 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 ListMessageBoxButtons( ) { return Global.Common.Lists.MessageBoxButtons( ); } public string ListMessageBoxIcons( ) { return Global.Common.Lists.MessageBoxIcons( ); } public string ListMessageBoxOptions( ) { return Global.Common.Lists.MessageBoxOptions( ); } public string ListProperties( ) { return Global.Common.Lists.Properties( this ); } public string SampleCode( ) { string code = "Set objMessageBox = CreateObject( \"RobvanderWoude.MessageBox\" )\n\n"; code += "With objMessageBox\n"; code += "\t.message = \"This MessageBox is more or less obsolete in VBScript,\" & vbCrLf _\n"; code += "\t & \"as it is almost identical to VBScript's own MsgBox,\" & vbCrLf _\n"; code += "\t & \"but it may be useful in other COM-aware scripting languages.\"\n"; code += "\t.title = \"MessageBox Demo\"\n"; code += "\t.buttons = \"OK\"\n"; code += "\t.icon = \"Information\"\n"; code += "\t.Show\n"; code += "\tWScript.Echo \"You clicked: \" & .buttonclicked\n"; code += "\tWScript.Echo\n"; code += "\tWScript.Echo .ListProperties( )\n"; code += "End With\n\n"; code += "Set objMessageBox = Nothing"; return code; } [System.STAThread] public void Show( ) { try { _buttonclicked = System.Windows.Forms.MessageBox.Show( message, title, _buttons, _icon, _defaultbutton, _options ); } catch ( System.Exception e ) { _errors.Add( timestamp + "Error while trying to show dialog: " + e.Message ); } } #endregion Methods #region Properties private System.Windows.Forms.MessageBoxButtons _buttons = defaultbuttonset; [System.Runtime.InteropServices.ComVisible( true )] public string buttons { get { return _buttons.ToString( ); } set { if ( !System.Enum.TryParse( value, true, out _buttons ) ) { _debuginfo.Add( string.Format( "{0}Unable to make sense of specified button \"{1}\", using default \"{2}\" instead", timestamp, value, defaultbuttonset.ToString( ) ) ); _buttons = defaultbuttonset; } } } private System.Windows.Forms.DialogResult _buttonclicked; [System.Runtime.InteropServices.ComVisible( true )] public string buttonclicked { get { return _buttonclicked.ToString( ); } } private System.Windows.Forms.MessageBoxDefaultButton _defaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button1; [System.Runtime.InteropServices.ComVisible( true )] public int defaultbutton { get { if ( int.TryParse( _defaultbutton.ToString( ).Substring( 6 ), out int test ) ) { return test; } else { _errors.Add( timestamp + "Unable to determine default button" ); return 0; } } set { if ( !System.Enum.TryParse( "Button" + value.ToString( ), true, out _defaultbutton ) ) { _debuginfo.Add( string.Format( "{0}Unable to make sense of default button \"Button{1}\", using \"Button1\" instead", timestamp, value.ToString( ) ) ); _defaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button1; } } } 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 System.Windows.Forms.MessageBoxIcon _icon = defaulticon; [System.Runtime.InteropServices.ComVisible( true )] public string icon { get { return _icon.ToString( ); } set { if ( !System.Enum.TryParse( value, true, out _icon ) ) { _debuginfo.Add( string.Format( "{0}Unable to make sense of specified icon \"{1}\", using default \"{2}\" instead", timestamp, value, defaulticon.ToString( ) ) ); _icon = defaulticon; } } } private string _message = defaultmessage; [System.Runtime.InteropServices.ComVisible( true )] public string message { get { return _message; } set { _message = value.Trim( ); } } private System.Windows.Forms.MessageBoxOptions _options = defaultoptions; [System.Runtime.InteropServices.ComVisible( true )] public string options { get { return _options.ToString( ); } set { if ( System.Enum.TryParse( value, true, out System.Windows.Forms.MessageBoxOptions test ) ) { _options = test | defaultoptions; } else { _debuginfo.Add( string.Format( "{0}Unable to make sense of MessageBoxOption \"{1}\", using default \"{2}\" instead", timestamp, value, defaultoptions ) ); _options = defaultoptions; } } } 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 ) || value.Length > 40 ) { _debuginfo.Add( timestamp + "Specified title is ignored because it is empty or too long, using default title instead" ); _title = defaulttitle; } else { _title = value.Trim( ); } } } [System.Runtime.InteropServices.ComVisible( true )] public string Version { get { return Global.Common.ProgramInfo.FileVersion; } } #endregion Properties } }