using System.Linq; namespace RobvanderWoude { [System.Runtime.InteropServices.Guid( "C2173D12-E144-43AB-9764-0BA52FD7F926" )] public interface InputBox_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 MaskLanguageTable( ); [System.Runtime.InteropServices.DispId( 6 )] string SampleCode( ); [System.Runtime.InteropServices.DispId( 7 )] void Show( ); } [System.Runtime.InteropServices.Guid( "5CCE42CF-C0A6-4819-B29E-F4A3806C1BFD" ), System.Runtime.InteropServices.InterfaceType( System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch )] public interface InputBox_Events { } [System.Runtime.InteropServices.Guid( "E6F8076F-5145-4CB2-9ACF-597A98B4F5A1" ), System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.AutoDual ), System.Runtime.InteropServices.ComSourceInterfaces( typeof( InputBox_Events ) )] public class InputBox : InputBox_Interface { #region Default values const string defaultcaptioncancel = "Cancel"; const string defaultcaptionok = "OK"; const string defaultcaptionshowpassword = "Show password"; static readonly System.Drawing.FontFamily defaultfontfamily = System.Drawing.FontFamily.GenericSansSerif; const int defaultfontsize = 10; const string defaultmask = ""; const char defaultpasswordchar = '*'; const string defaultprompt = "Prompt"; const int defaulttimeout = 0; const string defaulttitle = "Title"; const int defaultwindowheight = 110; const int defaultwindowwidth = 200; const float maximumfontsize = 48; const float minimumfontsize = 6; #endregion Default values #region Controls System.Windows.Forms.Form inputboxform; static System.Windows.Forms.TextBox textbox; static System.Windows.Forms.MaskedTextBox maskedtextbox; static System.Windows.Forms.CheckBox showpasswordcheckbox; System.Timers.Timer timer; #endregion Controls static string previousinput = string.Empty; #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 InputBox class

\n\n"; help += "

Present a prompt for input dialog, and return the text entered

\n\n"; help += "

COM ProgID: RobvanderWoude.InputBox

\n\n"; } else { help += "Help for InputBox class\n"; help += new string( '\u2500', help.Length ) + "\n\n"; help += "Present a prompt for input dialog, and return the text entered\n\n\n"; help += "COM ProgID: RobvanderWoude.InputBox\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, "allowshowpassword", "Add \"Show password\" checkbox (0=false; 1=true)", "no", "no", "0: no checkbox", html ); help += separatorline; help += HelpTableRow( linetemplate, "asciionly", "Allow only ASCII characters (0=false; 1=true)", "no", "no", "0: allow all", html ); help += separatorline; 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, "captionshowpassword", "Text for \"Show password\" checkbox", "no", "no", defaultcaptionshowpassword, 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, "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, "hidepassword", "Hide input, for password dialogs (0=false; 1=true)", "no", "no", "0: show input", html ); help += separatorline; help += HelpTableRow( linetemplate, "initialinput", "Initial input (e.g. expected value)", "no", "no", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "input", "Input as typed in text field", "no", "no", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "left", Global.Common.Help.left, "no", "no", "Centered (0..screenwidth-windowwidth)", html ); help += separatorline; help += HelpTableRow( linetemplate, "localizecaptions", Global.Common.Help.localizecaptions, "no", "no", "0: English captions", html ); help += separatorline; help += HelpTableRow( linetemplate, "mask", "Optional mask to apply to input", "no", "no", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "maskedinput", "Masked input, i.e. including mask literals", "no", "no", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "modal", Global.Common.Help.modal, "no", "no", "1: always on top", html ); help += separatorline; help += HelpTableRow( linetemplate, "passwordchar", "Character used to hide input", "no", "no", "*", html ); help += separatorline; help += HelpTableRow( linetemplate, "prompt", Global.Common.Help.prompt, "no", "no", defaultprompt, html ); help += separatorline; help += HelpTableRow( linetemplate, "regexcasesensitive", "Case sensitive regexes (0=false; 1=true)", "no", "no", "1: case sensitive", html ); help += separatorline; help += HelpTableRow( linetemplate, "regexendresult", "Regex pattern applied to submitted input", "no", "no", ".*", html ); help += separatorline; help += HelpTableRow( linetemplate, "regexonthefly", "Regex pattern applied at eacht change of input", "no", "no", ".*", html ); help += separatorline; help += HelpTableRow( linetemplate, "showpassword", "\"Show password\" checkbox status (1=checked)", "no", "no", "0: not checked", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeout", Global.Common.Help.timeout, "no", "no", defaulttimeout.ToString( ), 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, "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "windowheight", Global.Common.Help.windowheight, "no", "no", string.Format( "{0} ({0}..screenheight)", defaultwindowheight ), html ); help += separatorline; help += HelpTableRow( linetemplate, "windowwidth", Global.Common.Help.windowwidth, "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
\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 = 18; col2width = 74; col3width = 42; 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, "MaskLanguageTable", "Returns a table with an overview of the MaskedTextBox's masking language", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "SampleCode", Global.Common.Help._samplecode, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "Show", "Presents a prompt for input dialog based on the current property values, and if \"OK\" is clicked, saves the entered input in the \"input\" property.\nIf \"mask\" is specified, a masked textbox will be used, and if the entered text complies to the mask, it will be saved unmasked (without the mask literals) in the \"input\" property and masked (including mask literals) in the \"maskedinput\" property.\nIf \"Cancel\" is clicked, or if the entered input does not comply to the mask or does not match the regular expressions, \"input\" and \"maskedinput\" will both be empty.", "", 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 MaskLanguageTable( ) { return Global.Common.Lists.MaskLanguageTable( ); } public string SampleCode( ) { string code = "Set objInputBox = CreateObject( \"RobvanderWoude.InputBox\" )\n\n"; code += "With objInputBox\n"; code += "\t.prompt = \"Enter a MAC address\"\n"; code += "\t.title = \"MAC Address\"\n"; code += "\t.initialinput = \"0022446688AACCEE\"\n"; code += "\t.mask = \">CC\\:CC\\:CC\\:CC\\:CC\\:CC\\:CC\\:CC\"\n"; code += "\t.regexendresult = \"[\\dA - F]{16}\"\n"; code += "\t.regexonthefly = \"[\\dA-F]{0,16}\"\n"; code += "\t.regexcasesensitive = 0\n"; code += "\t.timeout = 120\n"; code += "\t.Show\n"; code += "\tWScript.Echo \"New MAC address: \" & .input & \" (\" & .maskedinput & \")\"\n"; code += "\tWScript.Echo\n"; code += "\tWScript.Echo .ListProperties( )\n"; code += "End With\n\n"; code += "Set objInputBox = Nothing"; return code; } public void Show( ) { #region Apply Settings if ( _hidepassword ) { _passwordchar = '*'; } else { _passwordchar = '\0'; } System.Drawing.Size textboxsize = new System.Drawing.Size( windowwidth - 20, 25 ); if ( _showpassword ) { textboxlocation = new System.Drawing.Point( 10, windowheight - 100 ); } else { textboxlocation = new System.Drawing.Point( 10, windowheight - 75 ); } _input = initialinput; #endregion Apply Settings #region Timer try { _timeoutelapsed = false; if ( timeout > 0 ) { timer = new System.Timers.Timer( ); timer.Elapsed += new System.Timers.ElapsedEventHandler( InputBox_Timer_Elapsed ); timer.Interval = timeout * 1000; timer.Start( ); } } catch ( System.Exception e ) { _errors.Add( timestamp + "Error while trying to create dialog: " + e.Message ); } #endregion Timer #region Build Form try { inputboxform = new System.Windows.Forms.Form( ); inputboxform.ClientSize = new System.Drawing.Size( windowwidth, windowheight ); inputboxform.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; inputboxform.Font = new System.Drawing.Font( _fontfamily, _fontsize, System.Drawing.FontStyle.Regular ); inputboxform.MaximizeBox = false; inputboxform.MinimizeBox = false; if ( left < 0 || top < 0 ) { inputboxform.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; } else { inputboxform.StartPosition = System.Windows.Forms.FormStartPosition.Manual; inputboxform.Location = new System.Drawing.Point( left, top ); } inputboxform.Text = title; inputboxform.TopMost = topmost; System.Windows.Forms.Label labelprompt = new System.Windows.Forms.Label( ); labelprompt.Size = new System.Drawing.Size( windowwidth - 20, windowheight - 90 ); labelprompt.Location = new System.Drawing.Point( 10, 10 ); labelprompt.Text = prompt.Replace( "\\n", "\n" ); inputboxform.Controls.Add( labelprompt ); if ( ValidateMask( mask ) ) { #region Add MaskedTextBox _debuginfo.Add( timestamp + "Mask validated, adding MaskedTextBox to form" ); maskedtextbox = new System.Windows.Forms.MaskedTextBox( ); maskedtextbox.AsciiOnly = _asciionly; maskedtextbox.Location = textboxlocation; maskedtextbox.Mask = mask; maskedtextbox.PasswordChar = _passwordchar; maskedtextbox.Text = input; maskedtextbox.TextMaskFormat = System.Windows.Forms.MaskFormat.IncludeLiterals; _maskedinput = maskedtextbox.Text; maskedtextbox.Size = textboxsize; maskedtextbox.KeyUp += new System.Windows.Forms.KeyEventHandler( InputBox_MaskedTextbox_KeyUp ); inputboxform.Controls.Add( maskedtextbox ); #endregion Add MaskedTextBox } else { #region Add TextBox _debuginfo.Add( timestamp + "Mask not validated, adding TextBox to form" ); textbox = new System.Windows.Forms.TextBox( ); textbox.PasswordChar = _passwordchar; textbox.Size = textboxsize; textbox.Location = textboxlocation; if ( _hidepassword ) { _debuginfo.Add( timestamp + "Hide password" ); if ( _allowshowpassword ) { _debuginfo.Add( timestamp + "Allow showing hidden password" ); // Insert a checkbox with label "Show password" 25 px below the textbox showpasswordcheckbox = new System.Windows.Forms.CheckBox( ); showpasswordcheckbox.Checked = _showpassword; showpasswordcheckbox.Location = new System.Drawing.Point( 11, textbox.Location.Y + 25 ); showpasswordcheckbox.Width = inputboxform.Width - 22; showpasswordcheckbox.Click += new System.EventHandler( InputBox_ShowPasswordCheckbox_Click ); showpasswordcheckbox.Text = captionshowpassword; inputboxform.Controls.Add( showpasswordcheckbox ); } } else { textbox.Text = input; } textbox.KeyUp += new System.Windows.Forms.KeyEventHandler( InputBox_TextBox_KeyUp ); inputboxform.Controls.Add( textbox ); #endregion Add TextBox } #region Add Buttons System.Windows.Forms.Button okButton = new System.Windows.Forms.Button( ); okButton.DialogResult = System.Windows.Forms.DialogResult.OK; okButton.Name = "okButton"; okButton.Size = new System.Drawing.Size( 80, 25 ); okButton.Text = captionok; okButton.Location = new System.Drawing.Point( windowwidth / 2 - 10 - 80, windowheight - 40 ); inputboxform.Controls.Add( okButton ); System.Windows.Forms.Button cancelButton = new System.Windows.Forms.Button( ); cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; cancelButton.Name = "cancelButton"; cancelButton.Size = new System.Drawing.Size( 80, 25 ); cancelButton.Text = captioncancel; cancelButton.Location = new System.Drawing.Point( windowwidth / 2 + 10, windowheight - 40 ); inputboxform.Controls.Add( cancelButton ); #endregion Add Buttons inputboxform.AcceptButton = okButton; // OK on Enter inputboxform.CancelButton = cancelButton; // Cancel on Esc inputboxform.Activate( ); inputboxform.BringToFront( ); inputboxform.Focus( ); if ( ValidateMask( mask ) ) { maskedtextbox.BringToFront( ); // Bug workaround maskedtextbox.Select( 0, 0 ); // Move cursor to begin maskedtextbox.Focus( ); } else { textbox.BringToFront( ); // Bug workaround textbox.Select( 0, 0 ); // Move cursor to begin textbox.Focus( ); } } catch ( System.Exception e ) { _errors.Add( timestamp + "Error while trying to create dialog: " + e.Message ); } #endregion Build Form #region Show Dialog and Return Result try { System.Windows.Forms.DialogResult result = inputboxform.ShowDialog( ); if ( !_timeoutelapsed && result == System.Windows.Forms.DialogResult.Cancel ) { _input = string.Empty; } } catch ( System.Exception e ) { _errors.Add( timestamp + "Error while trying to show dialog: " + e.Message ); } #endregion Show Dialog and Return Result } public static void ValidateAndSaveResult( ) { // Read input from MaskedTextBox or TextBox if ( ValidateMask( _mask ) ) { _debuginfo.Add( timestamp + "Mask validated" ); _debuginfo.Add( timestamp + "Retrieving unmasked input" ); maskedtextbox.TextMaskFormat = System.Windows.Forms.MaskFormat.ExcludePromptAndLiterals; _input = maskedtextbox.Text; _debuginfo.Add( timestamp + "Unmasked input: " + _input ); _debuginfo.Add( timestamp + "Retrieving masked input" ); maskedtextbox.TextMaskFormat = System.Windows.Forms.MaskFormat.IncludeLiterals; _maskedinput = maskedtextbox.Text; _debuginfo.Add( timestamp + "Unmasked input: " + _maskedinput ); // Check if input complies with mask if ( !maskedtextbox.MaskCompleted ) { _errors.Add( timestamp + "Input did not comply with mask" ); _input = string.Empty; _maskedinput = string.Empty; } } else { _input = textbox.Text; _maskedinput = _input; } // Check if input complies with regex if ( !string.IsNullOrWhiteSpace( _regexendresult ) && !System.Text.RegularExpressions.Regex.IsMatch( _input, _regexendresult, _regexcasesensitivity ) ) { _errors.Add( string.Format( "{0}Input \"{1}\" did not match regular expression \"{2}\"", timestamp, _input, _regexendresult ) ); _input = string.Empty; _maskedinput = string.Empty; } } public static bool ValidateMask( string mask ) { return ( mask.IndexOfAny( "09#L?&CAa.,:/&><|\\".ToCharArray( ) ) > -1 ); } #endregion Methods #region Properties private bool _allowshowpassword = false; [System.Runtime.InteropServices.ComVisible( true )] public int allowshowpassword { get { return ( _allowshowpassword ? 1 : 0 ); } set { _allowshowpassword = ( value == 1 ); } } private bool _asciionly = false; [System.Runtime.InteropServices.ComVisible( true )] public int asciionly { get { return ( _asciionly ? 1 : 0 ); } set { _asciionly = ( value == 1 ); } } 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 ) { _debuginfo.Add( timestamp + "Caption for \"Cancel\" button set to default because the value is too long or empty" ); _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 ) { _debuginfo.Add( timestamp + "Caption for \"OK\" button set to default because the value is too long or empty" ); _captionok = defaultcaptionok; } else { _captionok = value.Trim( ); } } } private string _captionshowpassword = defaultcaptionshowpassword; [System.Runtime.InteropServices.ComVisible( true )] public string captionshowpassword { get { return _captionshowpassword; } set { if ( string.IsNullOrWhiteSpace( value ) || value.Length > 40 ) { _debuginfo.Add( timestamp + "Caption for \"Show password\" set to default because the value is too long or empty" ); _captionshowpassword = defaultcaptionshowpassword; } else { _captionshowpassword = value.Trim( ); } } } private static 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 static 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.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 bool _hidepassword = false; [System.Runtime.InteropServices.ComVisible( true )] public int hidepassword { get { return ( _hidepassword ? 1 : 0 ); } set { _hidepassword = ( value == 1 ); } } private string _initialinput = string.Empty; [System.Runtime.InteropServices.ComVisible( true )] public string initialinput { get { return _initialinput; } set { _initialinput = value; } } private static string _input = string.Empty; [System.Runtime.InteropServices.ComVisible( true )] public string input { get { return _input; } } 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 bool _localizecaptions = false; [System.Runtime.InteropServices.ComVisible( true )] public int localizecaptions { get { return ( _localizecaptions ? 1 : 0 ); } set { _localizecaptions = ( value == 1 ); } } private static string _mask = string.Empty; [System.Runtime.InteropServices.ComVisible( true )] public string mask { get { return _mask; } set { if ( ValidateMask( value ) ) { _mask = value; } else { _mask = string.Empty; } } } private static string _maskedinput = string.Empty; [System.Runtime.InteropServices.ComVisible( true )] public string maskedinput { get { return _maskedinput; } } private bool topmost = true; [System.Runtime.InteropServices.ComVisible( true )] public int modal { get { return ( topmost ? 1 : 0 ); } set { topmost = ( value == 1 ); } } private static char _passwordchar = defaultpasswordchar; [System.Runtime.InteropServices.ComVisible( true )] public string passwordchar { get { if ( _passwordchar == '\0' ) { return string.Empty; } else { return System.Convert.ToString( _passwordchar ); } } set { if ( string.IsNullOrEmpty( value ) ) { _debuginfo.Add( timestamp + "Empty password character not supported, using default * instead" ); _passwordchar = defaultpasswordchar; } else { _debuginfo.Add( timestamp + "New password character set: \"" + passwordchar + "\"" ); _passwordchar = value[0]; } } } private string _prompt = defaultprompt; [System.Runtime.InteropServices.ComVisible( true )] public string prompt { get { return _prompt; } set { _prompt = value.Trim( ); } } private static System.Text.RegularExpressions.RegexOptions _regexcasesensitivity = System.Text.RegularExpressions.RegexOptions.None; [System.Runtime.InteropServices.ComVisible( true )] public int regexcasesensitive { get { if ( _regexcasesensitivity == System.Text.RegularExpressions.RegexOptions.None ) { return 0; } else { return 1; } } set { if ( value == 1 ) { _regexcasesensitivity = System.Text.RegularExpressions.RegexOptions.IgnoreCase; } else { _regexcasesensitivity = System.Text.RegularExpressions.RegexOptions.None; } } } private static string _regexendresult = ".*"; [System.Runtime.InteropServices.ComVisible( true )] public string regexendresult { get { return _regexendresult; } set { if ( Global.Common.TestIfRegexPattern( value ) ) { _regexendresult = value; } } } private static string _regexonthefly = ".*"; [System.Runtime.InteropServices.ComVisible( true )] public string regexonthefly { get { return _regexonthefly; } set { if ( Global.Common.TestIfRegexPattern( value ) ) { _regexonthefly = value; } } } private bool _showpassword = false; [System.Runtime.InteropServices.ComVisible( true )] public int showpassword { get { return ( _showpassword ? 1 : 0 ); } set { _showpassword = ( value == 1 ); } } private System.Drawing.Point textboxlocation { get; set; } private int _timeout = 0; [System.Runtime.InteropServices.ComVisible( true )] public int timeout { get { return _timeout; } set { if ( value < 0 || value > 3600 ) { _timeout = 0; } else { _timeout = value; } } } 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 static 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 static 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 InputBox_Timer_Elapsed( object sender, System.Timers.ElapsedEventArgs e ) { _timeoutelapsed = true; ValidateAndSaveResult( ); inputboxform.Close( ); } private void InputBox_TextBox_KeyUp( object sender, System.Windows.Forms.KeyEventArgs e ) { _input = textbox.Text; if ( System.Text.RegularExpressions.Regex.IsMatch( input, regexonthefly, _regexcasesensitivity ) ) { previousinput = input; } else { _input = previousinput; } if ( textbox.Text != input ) { textbox.Text = input; textbox.SelectionStart = input.Length; } } private void InputBox_MaskedTextbox_KeyUp( object sender, System.Windows.Forms.KeyEventArgs e ) { maskedtextbox.TextMaskFormat = System.Windows.Forms.MaskFormat.ExcludePromptAndLiterals; _input = maskedtextbox.Text; if ( System.Text.RegularExpressions.Regex.IsMatch( input, regexonthefly, _regexcasesensitivity ) ) { previousinput = input; } else { _input = previousinput; } if ( maskedtextbox.Text != input ) { maskedtextbox.Text = input; if ( input.Length > 0 ) { maskedtextbox.SelectionStart = maskedtextbox.Text.LastIndexOf( input.Last( ) ) + 1; } else { maskedtextbox.SelectionStart = 0; } } maskedtextbox.TextMaskFormat = System.Windows.Forms.MaskFormat.IncludeLiterals; _maskedinput = maskedtextbox.Text; maskedtextbox.TextMaskFormat = System.Windows.Forms.MaskFormat.ExcludePromptAndLiterals; } private void InputBox_ShowPasswordCheckbox_Click( object sender, System.EventArgs e ) { // Toggle between hidden and normal text if ( _passwordchar == '*' ) { _passwordchar = '\0'; } else { _passwordchar = '*'; } if ( ValidateMask( mask ) ) { maskedtextbox.PasswordChar = _passwordchar; } else { textbox.PasswordChar = _passwordchar; } } #endregion Event Handlers } }