Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for inputbox.cs

(view source code of inputbox.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Timers;
  10. using System.Windows.Forms;
  11.  
  12.  
  13. namespace RobvanderWoude
  14. {
  15. 	class InputBox
  16. 	{
  17. 		public const string progver = "1.35";
  18.  
  19. 		#region Global Variables
  20.  
  21. 		public const int defheight = 110;
  22. 		public const int deftimeout = 60;
  23. 		public const int defwidth = 200;
  24. 		public const string defaulttitle = "? 2018 Rob van der Woude";
  25.  
  26. 		public static ConsoleColor bold = ConsoleColor.White;
  27. 		public static MaskedTextBox maskedtextbox;
  28. 		public static TextBox textbox;
  29. 		public static RegexOptions casesensitivity = RegexOptions.None;
  30. 		public static bool asciionly = false;
  31. 		public static bool bw = false;
  32. 		public static bool filtered = true;
  33. 		public static bool oddrow = false;
  34. 		public static bool ontheflyregexset = false;
  35. 		public static bool password = false;
  36. 		public static bool timeoutelapsed = false;
  37. 		public static bool usemask = false;
  38. 		public static bool regexset = false;
  39. 		public static bool returnunmasked = false;
  40. 		public static string currentinput = String.Empty;
  41. 		public static string defanswer = "Default answer";
  42. 		public static string ontheflypattern = ".*";
  43. 		public static string previousinput = String.Empty;
  44. 		public static string regexpattern = ".*";
  45.  
  46. 		#endregion Global Variables
  47.  
  48.  
  49. 		[STAThread]
  50. 		static int Main( string[] args )
  51. 		{
  52. 			// Based on code by Gorkem Gencay on StackOverflow.com:
  53. 			// http://stackoverflow.com/questions/97097/what-is-the-c-sharp-version-of-vb-nets-inputdialog#17546909
  54.  
  55. 			#region Initialize variables
  56.  
  57. 			const string deftitle = "Title";
  58. 			const string deftext = "Prompt";
  59.  
  60. 			bool heightset = false;
  61. 			bool showpassword = false;
  62. 			bool timeoutset = false;
  63. 			bool widthset = false;
  64. 			string input = string.Empty;
  65. 			string mask = String.Empty;
  66. 			string showpasswordprompt = "Show password";
  67. 			string text = deftext;
  68. 			string title = deftitle;
  69. 			int height = defheight;
  70. 			int timeout = 0;
  71. 			int width = defwidth;
  72. 			string cancelcaption = "&Cancel";
  73. 			string okcaption = "&OK";
  74. 			string localizationstring = String.Empty;
  75. 			bool localizedcaptionset = false;
  76.  
  77. 			#endregion Initialize variables
  78.  
  79.  
  80. 			#region Command Line Parsing
  81.  
  82. 			if ( args.Length == 0 )
  83. 			{
  84. 				return ShowHelp( );
  85. 			}
  86.  
  87. 			foreach ( string arg in args )
  88. 			{
  89. 				if ( arg == "/?" )
  90. 				{
  91. 					return ShowHelp( );
  92. 				}
  93. 			}
  94.  
  95. 			text = String.Empty;
  96. 			title = String.Empty;
  97. 			defanswer = String.Empty;
  98.  
  99. 			foreach ( string arg in args )
  100. 			{
  101. 				if ( arg[0] == '/' )
  102. 				{
  103. 					if ( arg.Length == 1 )
  104. 					{
  105. 						return ShowHelp( );
  106. 					}
  107. 					else if ( arg.Length == 2 )
  108. 					{
  109. 						switch ( arg.ToString( ).ToUpper( ) )
  110. 						{
  111. 							case "/A":
  112. 								if ( asciionly )
  113. 								{
  114. 									return ShowHelp( "Duplicate command line switch /A" );
  115. 								}
  116. 								asciionly = true;
  117. 								break;
  118. 							case "/B":
  119. 								if ( bw )
  120. 								{
  121. 									return ShowHelp( "Duplicate command line switch /B" );
  122. 								}
  123. 								bw = true;
  124. 								bold = Console.ForegroundColor;
  125. 								break;
  126. 							case "/I":
  127. 								if ( casesensitivity == RegexOptions.IgnoreCase )
  128. 								{
  129. 									return ShowHelp( "Duplicate command line switch /I" );
  130. 								}
  131. 								casesensitivity = RegexOptions.IgnoreCase;
  132. 								break;
  133. 							case "/L":
  134. 								if ( localizedcaptionset )
  135. 								{
  136. 									return ShowHelp( "Duplicate command line switch /L" );
  137. 								}
  138. 								localizedcaptionset = true;
  139. 								break;
  140. 							case "/M":
  141. 								return HelpMessage( "mask" );
  142. 							case "/N":
  143. 								if ( !filtered )
  144. 								{
  145. 									return ShowHelp( "Duplicate command line switch /N" );
  146. 								}
  147. 								filtered = false;
  148. 								break;
  149. 							case "/P":
  150. 								if ( password )
  151. 								{
  152. 									return ShowHelp( "Duplicate command line switch /P" );
  153. 								}
  154. 								password = true;
  155. 								break;
  156. 							case "/S":
  157. 								if ( showpassword )
  158. 								{
  159. 									return ShowHelp( "Duplicate command line switch /S" );
  160. 								}
  161. 								showpassword = true;
  162. 								break;
  163. 							case "/T":
  164. 								if ( timeoutset )
  165. 								{
  166. 									return ShowHelp( "Duplicate command line switch /T" );
  167. 								}
  168. 								timeout = deftimeout;
  169. 								timeoutset = true;
  170. 								break;
  171. 							case "/U":
  172. 								if ( returnunmasked )
  173. 								{
  174. 									return ShowHelp( "Duplicate command line switch /U" );
  175. 								}
  176. 								returnunmasked = true;
  177. 								break;
  178. 							default:
  179. 								return ShowHelp( "Invalid command line switch {0}", arg );
  180. 						}
  181. 					}
  182. 					else if ( arg.Length > 3 && arg[2] == ':' )
  183. 					{
  184. 						switch ( arg.Substring( 0, 3 ).ToUpper( ) )
  185. 						{
  186. 							case "/F:":
  187. 								if ( ontheflyregexset )
  188. 								{
  189. 									return ShowHelp( "Duplicate command line switch /F" );
  190. 								}
  191. 								ontheflypattern = String.Format( "^{0}$", arg.Substring( 3 ) );
  192. 								ontheflyregexset = true;
  193. 								break;
  194. 							case "/H:":
  195. 								if ( heightset )
  196. 								{
  197. 									return ShowHelp( "Duplicate command line switch /H" );
  198. 								}
  199. 								try
  200. 								{
  201. 									height = Convert.ToInt32( arg.Substring( 3 ) );
  202. 									if ( height < defheight || height > Screen.PrimaryScreen.Bounds.Height )
  203. 									{
  204. 										return ShowHelp( "Invalid screen height: \"{0}\"\n\tHeight must be an integer between {1} and {2} (screen height)", arg.Substring( 3 ), defheight.ToString( ), Screen.PrimaryScreen.Bounds.Height.ToString( ) );
  205. 									}
  206. 									heightset = true;
  207. 								}
  208. 								catch ( FormatException e )
  209. 								{
  210. 									return ShowHelp( "Invalid height: \"{0}\"\n\t{1}", arg.Substring( 3 ), e.Message );
  211. 								}
  212. 								break;
  213. 							case "/L:":
  214. 								if ( localizedcaptionset )
  215. 								{
  216. 									return ShowHelp( "Duplicate command line switch /L" );
  217. 								}
  218. 								localizedcaptionset = true;
  219. 								localizationstring = arg.Substring( 3 );
  220. 								break;
  221. 							case "/M:":
  222. 								if ( usemask )
  223. 								{
  224. 									return ShowHelp( "Duplicate command line switch /M" );
  225. 								}
  226. 								mask = arg.Substring( 3 ).Trim( "\"".ToCharArray( ) );
  227. 								if ( String.IsNullOrWhiteSpace( mask ) )
  228. 								{
  229. 									ShowHelp( "No mask specified with /M" );
  230. 									Console.WriteLine( "\n\n" );
  231. 									return HelpMessage( "mask" );
  232. 								}
  233. 								usemask = true;
  234. 								break;
  235. 							case "/R:":
  236. 								if ( regexset )
  237. 								{
  238. 									return ShowHelp( "Duplicate command line switch /R" );
  239. 								}
  240. 								regexpattern = arg.Substring( 3 );
  241. 								regexset = true;
  242. 								break;
  243. 							case "/S:":
  244. 								if ( showpassword )
  245. 								{
  246. 									return ShowHelp( "Duplicate command line switch /S" );
  247. 								}
  248. 								showpassword = true;
  249. 								showpasswordprompt = arg.Substring( 3 );
  250. 								break;
  251. 							case "/T:":
  252. 								if ( timeoutset )
  253. 								{
  254. 									return ShowHelp( "Duplicate command line switch /T" );
  255. 								}
  256. 								try
  257. 								{
  258. 									timeout = Convert.ToInt32( arg.Substring( 3 ) ) * 1000;
  259. 									if ( timeout < 1000 )
  260. 									{
  261. 										return ShowHelp( "Invalid timeout: \"{0}\"\n\tTimeout value must be a positive integer, at least 1.", arg.Substring( 3 ) );
  262. 									}
  263. 									timeoutset = true;
  264. 								}
  265. 								catch ( FormatException e )
  266. 								{
  267. 									return ShowHelp( "Invalid timeout: \"{0}\"\n\t{1}", arg.Substring( 3 ), e.Message );
  268. 								}
  269. 								break;
  270. 							case "/W:":
  271. 								if ( widthset )
  272. 								{
  273. 									return ShowHelp( "Duplicate command line switch /W" );
  274. 								}
  275. 								try
  276. 								{
  277. 									width = Convert.ToInt32( arg.Substring( 3 ) );
  278. 									if ( width < defwidth || width > Screen.PrimaryScreen.Bounds.Width )
  279. 									{
  280. 										return ShowHelp( "Invalid screen width: \"{0}\"\n\tWidth must be an integer between {1} and {2} (screen width)", arg.Substring( 3 ), defwidth.ToString( ), Screen.PrimaryScreen.Bounds.Width.ToString( ) );
  281. 									}
  282. 									widthset = true;
  283. 								}
  284. 								catch ( FormatException e )
  285. 								{
  286. 									return ShowHelp( "Invalid width: \"{0}\"\n\t{1}", arg.Substring( 3 ), e.Message );
  287. 								}
  288. 								break;
  289. 							default:
  290. 								return ShowHelp( "Invalid command line switch \"{0}\"", arg );
  291. 						}
  292. 					}
  293. 					else
  294. 					{
  295. 						return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  296. 					}
  297. 				}
  298. 				else
  299. 				{
  300. 					if ( String.IsNullOrWhiteSpace( text ) )
  301. 					{
  302. 						text = arg;
  303. 					}
  304. 					else if ( String.IsNullOrWhiteSpace( title ) )
  305. 					{
  306. 						title = arg;
  307. 					}
  308. 					else if ( String.IsNullOrWhiteSpace( defanswer ) )
  309. 					{
  310. 						defanswer = arg;
  311. 					}
  312. 					else
  313. 					{
  314. 						return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  315. 					}
  316. 				}
  317. 			}
  318.  
  319. 			// Default title if none specified
  320. 			if ( String.IsNullOrWhiteSpace( title ) )
  321. 			{
  322. 				title = defaulttitle;
  323. 			}
  324.  
  325. 			// "Bold" color depends on /BW
  326. 			if ( bw )
  327. 			{
  328. 				bold = Console.ForegroundColor;
  329. 			}
  330. 			else
  331. 			{
  332. 				bold = ConsoleColor.White;
  333. 			}
  334.  
  335. 			// Switch /A requires /M
  336. 			if ( asciionly && !usemask )
  337. 			{
  338. 				return ShowHelp( "Command line switch /A (ASCII only) can only be used together with /M" );
  339. 			}
  340.  
  341. 			// Switch /S implies /P
  342. 			if ( showpassword )
  343. 			{
  344. 				password = true;
  345. 			}
  346.  
  347. 			// Set timer if /T:timeout was specified
  348. 			if ( timeoutset )
  349. 			{
  350. 				System.Timers.Timer timer = new System.Timers.Timer( );
  351. 				timer.Elapsed += new ElapsedEventHandler( timer_Elapsed );
  352. 				timer.Interval = timeout;
  353. 				timer.Start( );
  354. 			}
  355.  
  356. 			// For /S (Show password checkbox) add 25 px to window height unless height is specified
  357. 			if ( showpassword && !heightset )
  358. 			{
  359. 				height += 25;
  360. 			}
  361.  
  362. 			#endregion Command Line Parsing
  363.  
  364.  
  365. 			#region Set Localized Captions
  366.  
  367. 			if ( localizedcaptionset )
  368. 			{
  369. 				cancelcaption = Load( "user32.dll", 801, cancelcaption );
  370. 				okcaption = Load( "user32.dll", 800, okcaption );
  371. 				if ( !String.IsNullOrWhiteSpace( localizationstring ) )
  372. 				{
  373. 					string pattern = @"^((OK|Cancel)=[^;\""]*;)*((OK|Cancel)=[^;\""]*);?$";
  374. 					Regex regex = new Regex( pattern, RegexOptions.IgnoreCase );
  375. 					if ( regex.IsMatch( localizationstring ) )
  376. 					{
  377. 						string[] locstrings = localizationstring.Split( ";".ToCharArray( ) );
  378. 						foreach ( string locstring in locstrings )
  379. 						{
  380. 							string key = locstring.Substring( 0, locstring.IndexOf( '=' ) );
  381. 							string val = locstring.Substring( Math.Min( locstring.IndexOf( '=' ) + 1, locstring.Length - 1 ) );
  382. 							if ( !String.IsNullOrWhiteSpace( val ) )
  383. 							{
  384. 								switch ( key.ToUpper( ) )
  385. 								{
  386. 									case "OK":
  387. 										okcaption = val;
  388. 										break;
  389. 									case "CANCEL":
  390. 										cancelcaption = val;
  391. 										break;
  392. 									default:
  393. 										return ShowHelp( "Invalid localization key \"{0}\"", key );
  394. 								}
  395. 							}
  396. 						}
  397. 					}
  398. 					else
  399. 					{
  400. 						return ShowHelp( "Invalid localization string:\n\t{0}", localizationstring );
  401. 					}
  402. 				}
  403. 			}
  404.  
  405. 			#endregion Set Localized Captions
  406.  
  407.  
  408. 			#region Define Form
  409.  
  410. 			Size size = new Size( width, height );
  411. 			Form inputBox = new Form( );
  412.  
  413. 			inputBox.FormBorderStyle = FormBorderStyle.FixedDialog;
  414. 			inputBox.MaximizeBox = false;
  415. 			inputBox.MinimizeBox = false;
  416. 			inputBox.StartPosition = FormStartPosition.CenterParent;
  417. 			inputBox.ClientSize = size;
  418. 			inputBox.Text = title;
  419.  
  420. 			Label labelPrompt = new Label( );
  421. 			labelPrompt.Size = new Size( width - 20, height - 90 );
  422. 			labelPrompt.Location = new Point( 10, 10 );
  423. 			labelPrompt.Text = text.Replace( "\\n", "\n" );
  424. 			inputBox.Controls.Add( labelPrompt );
  425.  
  426. 			textbox = new TextBox( );
  427. 			textbox.Size = new Size( width - 20, 25 );
  428. 			if ( showpassword )
  429. 			{
  430. 				textbox.Location = new Point( 10, height - 100 );
  431. 			}
  432. 			else
  433. 			{
  434. 				textbox.Location = new Point( 10, height - 75 );
  435. 			}
  436. 			if ( password )
  437. 			{
  438. 				textbox.PasswordChar = '*';
  439. 				if ( showpassword )
  440. 				{
  441. 					// Insert a checkbox with label "Show password" 25 px below the textbox
  442. 					CheckBox checkbox = new CheckBox( );
  443. 					checkbox.Checked = false;
  444. 					checkbox.Location = new Point( 11, textbox.Location.Y + 25 );
  445. 					checkbox.Width = inputBox.Width - 22;
  446. 					checkbox.Click += new EventHandler( checkbox_Click );
  447. 					checkbox.Text = showpasswordprompt;
  448. 					inputBox.Controls.Add( checkbox );
  449. 				}
  450. 			}
  451. 			else
  452. 			{
  453. 				textbox.Text = defanswer;
  454. 			}
  455.  
  456. 			maskedtextbox = new MaskedTextBox( );
  457. 			maskedtextbox.Mask = mask;
  458. 			maskedtextbox.Location = textbox.Location;
  459. 			maskedtextbox.PasswordChar = textbox.PasswordChar;
  460. 			maskedtextbox.Text = textbox.Text;
  461. 			maskedtextbox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals; // return only the raw input
  462. 			maskedtextbox.Size = textbox.Size;
  463. 			maskedtextbox.AsciiOnly = asciionly;
  464.  
  465. 			if ( usemask )
  466. 			{
  467. 				maskedtextbox.KeyUp += new KeyEventHandler( maskedtextbox_KeyUp );
  468. 				inputBox.Controls.Add( maskedtextbox );
  469. 			}
  470. 			else
  471. 			{
  472. 				textbox.KeyUp += new KeyEventHandler( textbox_KeyUp );
  473. 				inputBox.Controls.Add( textbox );
  474. 			}
  475.  
  476. 			Button okButton = new Button( );
  477. 			okButton.DialogResult = DialogResult.OK;
  478. 			okButton.Name = "okButton";
  479. 			okButton.Size = new Size( 80, 25 );
  480. 			okButton.Text = okcaption;
  481. 			okButton.Location = new Point( width / 2 - 10 - 80, height - 40 );
  482. 			inputBox.Controls.Add( okButton );
  483.  
  484. 			Button cancelButton = new Button( );
  485. 			cancelButton.DialogResult = DialogResult.Cancel;
  486. 			cancelButton.Name = "cancelButton";
  487. 			cancelButton.Size = new Size( 80, 25 );
  488. 			cancelButton.Text = cancelcaption;
  489. 			cancelButton.Location = new Point( width / 2 + 10, height - 40 );
  490. 			inputBox.Controls.Add( cancelButton );
  491.  
  492. 			inputBox.AcceptButton = okButton;  // OK on Enter
  493. 			inputBox.CancelButton = cancelButton; // Cancel on Esc
  494. 			inputBox.Activate( );
  495. 			inputBox.BringToFront( );
  496. 			inputBox.Focus( );
  497.  
  498. 			if ( usemask )
  499. 			{
  500. 				maskedtextbox.BringToFront( ); // Bug workaround
  501. 				maskedtextbox.Select( 0, 0 ); // Move cursor to begin
  502. 				maskedtextbox.Focus( );
  503. 			}
  504. 			else
  505. 			{
  506. 				textbox.BringToFront( ); // Bug workaround
  507. 				textbox.Select( 0, 0 ); // Move cursor to begin
  508. 				textbox.Focus( );
  509. 			}
  510.  
  511. 			#endregion Define Form
  512.  
  513.  
  514. 			#region Show Dialog and Return Result
  515.  
  516. 			DialogResult result = inputBox.ShowDialog( );
  517. 			if ( result == DialogResult.OK )
  518. 			{
  519. 				int rc = ValidateAndShowResult( );
  520. 				return rc;
  521. 			}
  522. 			else
  523. 			{
  524. 				if ( timeoutelapsed )
  525. 				{
  526. 					ValidateAndShowResult( );
  527. 					return 3;
  528. 				}
  529. 				else
  530. 				{
  531. 					return 2;
  532. 				}
  533. 			}
  534.  
  535. 			#endregion Show Dialog and Return Result
  536. 		}
  537.  
  538.  
  539. 		#region Event Handlers
  540.  
  541. 		public static void checkbox_Click( object sender, System.EventArgs e )
  542. 		{
  543. 			// Toggle between hidden and normal text
  544. 			if ( usemask )
  545. 			{
  546. 				if ( maskedtextbox.PasswordChar == '*' )
  547. 				{
  548. 					maskedtextbox.PasswordChar = '\0';
  549. 				}
  550. 				else
  551. 				{
  552. 					maskedtextbox.PasswordChar = '*';
  553. 				}
  554. 			}
  555. 			else
  556. 			{
  557. 				if ( textbox.PasswordChar == '*' )
  558. 				{
  559. 					textbox.PasswordChar = '\0';
  560. 				}
  561. 				else
  562. 				{
  563. 					textbox.PasswordChar = '*';
  564. 				}
  565. 			}
  566. 		}
  567.  
  568.  
  569. 		private static void maskedtextbox_KeyUp( object sender, KeyEventArgs e )
  570. 		{
  571. 			maskedtextbox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
  572. 			currentinput = maskedtextbox.Text;
  573. 			if ( Regex.IsMatch( currentinput, ontheflypattern, casesensitivity ) )
  574. 			{
  575. 				previousinput = currentinput;
  576. 			}
  577. 			else
  578. 			{
  579. 				currentinput = previousinput;
  580. 			}
  581. 			if ( maskedtextbox.Text != currentinput )
  582. 			{
  583. 				maskedtextbox.Text = currentinput;
  584. 				maskedtextbox.TextMaskFormat = MaskFormat.IncludeLiterals;
  585. 				if ( currentinput.Length > 0 )
  586. 				{
  587. 					maskedtextbox.SelectionStart = maskedtextbox.Text.LastIndexOf( currentinput.Last<char>( ) ) + 1;
  588. 				}
  589. 				else
  590. 				{
  591. 					maskedtextbox.SelectionStart = 0;
  592. 				}
  593. 				maskedtextbox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
  594. 			}
  595. 		}
  596.  
  597.  
  598. 		private static void textbox_KeyUp( object sender, KeyEventArgs e )
  599. 		{
  600. 			currentinput = textbox.Text;
  601. 			if ( Regex.IsMatch( currentinput, ontheflypattern, casesensitivity ) )
  602. 			{
  603. 				previousinput = currentinput;
  604. 			}
  605. 			else
  606. 			{
  607. 				currentinput = previousinput;
  608. 			}
  609. 			if ( textbox.Text != currentinput )
  610. 			{
  611. 				textbox.Text = currentinput;
  612. 				textbox.SelectionStart = currentinput.Length;
  613. 			}
  614. 		}
  615.  
  616.  
  617. 		public static void timer_Elapsed( object sender, System.EventArgs e )
  618. 		{
  619. 			timeoutelapsed = true;
  620. 			Process.GetCurrentProcess( ).CloseMainWindow( );
  621. 		}
  622.  
  623. 		#endregion Event Handlers
  624.  
  625.  
  626. 		public static int HelpMessage( string subject )
  627. 		{
  628. 			switch ( subject.ToLower( ) )
  629. 			{
  630. 				case "mask":
  631. 					int col1perc = 13;
  632. 					Console.Error.Write( "Help for command line switch " );
  633. 					Console.ForegroundColor = bold;
  634. 					Console.Error.WriteLine( "/M:mask" );
  635. 					Console.ResetColor( );
  636. 					Console.Error.WriteLine( );
  637. 					Console.Error.Write( "The " );
  638. 					Console.ForegroundColor = bold;
  639. 					Console.Error.Write( "mask" );
  640. 					Console.ResetColor( );
  641. 					Console.Error.WriteLine( " \"language\" is based on the Masked Edit control in Visual Basic 6.0:" );
  642. 					if ( !bw )
  643. 					{
  644. 						Console.ForegroundColor = ConsoleColor.DarkGray;
  645. 					}
  646. 					string url1 = "http://msdn.microsoft.com/en-us/library/";
  647. 					string url2 = "system.windows.forms.maskedtextbox.mask.aspx#remarksToggle";
  648. 					if ( url1.Length + url2.Length > Console.WindowWidth )
  649. 					{
  650. 						Console.Error.WriteLine( url1 );
  651. 						Console.Error.WriteLine( url2 );
  652. 					}
  653. 					else
  654. 					{
  655. 						Console.Error.WriteLine( url1 + url2 );
  656. 					}
  657. 					Console.ResetColor( );
  658. 					Console.Error.WriteLine( );
  659. 					WriteTableRow( "Masking element", "Description", col1perc, true, true );
  660. 					WriteTableRow( "0", "Digit, required. This element will accept any single digit between 0 and 9.", col1perc );
  661. 					WriteTableRow( "9", "Digit or space, optional.", col1perc );
  662. 					WriteTableRow( "#", "Digit or space, optional. If this position is blank in the mask, it will be rendered as a space in the Text property. Plus (+) and minus (-) signs are allowed.", col1perc );
  663. 					WriteTableRow( "L", "Letter, required. Restricts input to the ASCII letters a-z and A-Z. This mask element is equivalent to [a-zA-Z] in regular expressions.", col1perc );
  664. 					WriteTableRow( "?", "Letter, optional. Restricts input to the ASCII letters a-z and A-Z. This mask element is equivalent to [a-zA-Z]? in regular expressions.", col1perc );
  665. 					WriteTableRow( "&", "Character, required. Any non-control character. If ASCII only is set (/A), this element behaves like the \"A\" element.", col1perc );
  666. 					WriteTableRow( "C", "Character, optional. Any non-control character. If ASCII only is set (/A), this element behaves like the \"a\" element.", col1perc );
  667. 					WriteTableRow( "A", "Alphanumeric, required. If ASCII only is set (/A), the only characters it will accept are the ASCII letters a-z and A-Z and numbers. This mask element behaves like the \"&\" element.", col1perc );
  668. 					WriteTableRow( "a", "Alphanumeric, optional. If ASCII only is set (/A), the only characters it will accept are the ASCII letters a-z and A-Z and numbers. This mask element behaves like the \"C\" element.", col1perc );
  669. 					WriteTableRow( ".", "Decimal placeholder.", col1perc );
  670. 					WriteTableRow( ",", "Thousands placeholder.", col1perc );
  671. 					WriteTableRow( ":", "Time separator.", col1perc );
  672. 					WriteTableRow( "/", "Date separator.", col1perc );
  673. 					WriteTableRow( "$", "Currency symbol.", col1perc );
  674. 					WriteTableRow( "<", "Shift down. Converts all characters that follow to lowercase.", col1perc );
  675. 					WriteTableRow( ">", "Shift up. Converts all characters that follow to uppercase.", col1perc );
  676. 					WriteTableRow( "|", "Disable a previous shift up or shift down.", col1perc );
  677. 					WriteTableRow( @"\", "Escape. Escapes a mask character, turning it into a literal. \"\\\\\" is the escape sequence for a backslash.", col1perc );
  678. 					WriteTableRow( "All other characters", "Literals. All non-mask elements will appear as themselves within MaskedTextBox. Literals always occupy a static position in the mask at run time, and cannot be moved or deleted by the user.", col1perc );
  679. 					break;
  680. 				default:
  681. 					return ShowHelp( );
  682. 			}
  683. 			return 1;
  684. 		}
  685.  
  686.  
  687. 		public static int ShowHelp( params string[] errmsg )
  688. 		{
  689. 			/*
  690. 			InputBox,  Version 1.34
  691. 			Prompt for input (GUI)
  692.  
  693. 			Usage:   INPUTBOX  [ "prompt"  [ "title"  [ "default" ] ] ] [ options ]
  694.  
  695. 			Where:   "prompt"    is the text above the input field (use \n for new line)
  696. 			         "title"     is the caption in the title bar
  697. 			         "default"   is the default answer shown in the input field
  698.  
  699. 			Options: /A          accepts ASCII characters only (requires /M)
  700. 			         /B          use standard Black and white in console, no highlighting
  701. 			         /F:regex    use regex to filter input on-the-Fly (see Notes)
  702. 			         /H:height   sets the Height of the input box
  703. 			                     (default: 110; minimum: 110; maximum: screen height)
  704. 			         /I          regular expressions are case Insensitive
  705. 			                     (default: regular expressions are case sensitive)
  706. 			         /L[:string] use Localized or custom captions (see Notes)
  707. 			         /M:mask     accept input only if it matches mask (template)
  708. 			         /N          Not filtered, only doublequotes are removed from input
  709. 			                     (default: remove & < > | ")
  710. 			         /P          hides (masks) the input text (for Passwords)
  711. 			         /R:regex    accept input only if it matches Regular expression regex
  712. 			         /S[:text]   inserts a checkbox "Show password" (or specified text)
  713. 			         /T[:sec]    sets the optional Timeout in seconds (default: 60)
  714. 			         /U          return Unmasked input, without literals (requires /M)
  715. 			                     (default: include literals in result)
  716. 			         /W:width    sets the Width of the input box
  717. 			                     (default: 200; minimum: 200; maximum: screen width)
  718.  
  719. 			Example: prompt for password
  720. 			InputBox.exe "Enter your password:" "Login" /S
  721.  
  722. 			Example: fixed length hexadecimal input (enter as a single command line)
  723. 			InputBox.exe "Enter a MAC address:" "MAC Address" "0022446688AACCEE"
  724. 			             /M:">CC\:CC\:CC\:CC\:CC\:CC" /R:"[\dA-F]{16}"
  725. 			             /F:"[\dA-F]{1,16}" /U /I
  726.  
  727. 			Notes:   For hidden input (/P and/or /S), "default" will be ignored.
  728. 			         With /F, regex must test the unmasked input (without literals), e.g.
  729. 			         /M:"CC:CC:CC:CC:CC:CC:CC:CC" /F:"[\dA-F]{0,16} /I" for MAC address.
  730. 			         With /R, regex is used to test input after OK is clicked;
  731. 			         with /F, regex is used to test input each time the input
  732. 			         changes, so regex must be able to cope with partial input;
  733. 			         e.g. /F:"[\dA-F]{0,16}" is OK, but /F:"[\dA-F]{16}" will fail.
  734. 			         Be careful with /N, use doublequotes for the "captured" result,
  735. 			         or redirect the result to a (temporary) file.
  736. 			         Show password (/S) implies hiding the input text (/P).
  737. 			         Use /M (without mask) to show detailed help on the mask language.
  738. 			         Use /L for Localized "OK" and "Cancel" button captions.
  739. 			         Custom captions require a string like /L:"OK=caption;Cancel=caption"
  740. 			         (button=caption pairs separated by semicolons, each button optional).
  741. 			         Text from input is written to Standard Out only if "OK" is clicked.
  742. 			         Return code is 0 for "OK", 1 for (command line) errors, 2 for
  743. 			         "Cancel", 3 on timeout, 4 if no regex or mask match.
  744.  
  745. 			Credits: On-the-fly form based on code by Gorkem Gencay on StackOverflow:
  746. 			         http://stackoverflow.com/questions/97097#17546909
  747. 			         Code to retrieve localized button captions by Martin Stoeckli:
  748. 			         http://martinstoeckli.ch/csharp/csharp.html#windows_text_resources
  749.  
  750. 			Written by Rob van der Woude
  751. 			http://www.robvanderwoude.com
  752. 			*/
  753.  
  754. 			if ( errmsg.Length > 0 )
  755. 			{
  756. 				List<string> errargs = new List<string>( errmsg );
  757. 				errargs.RemoveAt( 0 );
  758. 				Console.Error.WriteLine( );
  759. 				if ( !bw )
  760. 				{
  761. 					Console.ForegroundColor = ConsoleColor.Red;
  762. 				}
  763. 				Console.Error.Write( "ERROR:\t" );
  764. 				Console.ForegroundColor = bold;
  765. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  766. 				Console.ResetColor( );
  767. 			}
  768.  
  769. 			Console.Error.WriteLine( );
  770.  
  771. 			Console.Error.WriteLine( "InputBox,  Version {0}", progver );
  772.  
  773. 			Console.Error.WriteLine( "Prompt for input (GUI)" );
  774.  
  775. 			Console.Error.WriteLine( );
  776.  
  777. 			Console.Error.Write( "Usage:   " );
  778. 			Console.ForegroundColor = bold;
  779. 			Console.Error.WriteLine( "INPUTBOX  [ \"prompt\"  [ \"title\"  [ \"default\" ] ] ] [ options ]" );
  780. 			Console.ResetColor( );
  781.  
  782. 			Console.Error.WriteLine( );
  783.  
  784. 			Console.Error.Write( "Where:   " );
  785. 			Console.ForegroundColor = bold;
  786. 			Console.Error.Write( "\"prompt\"" );
  787. 			Console.ResetColor( );
  788. 			Console.Error.WriteLine( "    is the text above the input field (use \\n for new line)" );
  789.  
  790. 			Console.ForegroundColor = bold;
  791. 			Console.Error.Write( "         \"title\"" );
  792. 			Console.ResetColor( );
  793. 			Console.Error.WriteLine( "     is the caption in the title bar" );
  794.  
  795. 			Console.ForegroundColor = bold;
  796. 			Console.Error.Write( "         \"default\"" );
  797. 			Console.ResetColor( );
  798. 			Console.Error.WriteLine( "   is the default answer shown in the input field" );
  799.  
  800. 			Console.Error.WriteLine( );
  801.  
  802. 			Console.Error.Write( "Options: " );
  803. 			Console.ForegroundColor = bold;
  804. 			Console.Error.Write( "/A" );
  805. 			Console.ResetColor( );
  806. 			Console.Error.Write( "          accepts " );
  807. 			Console.ForegroundColor = bold;
  808. 			Console.Error.Write( "A" );
  809. 			Console.ResetColor( );
  810. 			Console.Error.Write( "SCII characters only (requires " );
  811. 			Console.ForegroundColor = bold;
  812. 			Console.Error.Write( "/M" );
  813. 			Console.ResetColor( );
  814. 			Console.Error.WriteLine( ")" );
  815.  
  816. 			Console.ForegroundColor = bold;
  817. 			Console.Error.Write( "         /B" );
  818. 			Console.ResetColor( );
  819. 			Console.Error.Write( "          use standard " );
  820. 			Console.ForegroundColor = bold;
  821. 			Console.Error.Write( "B" );
  822. 			Console.ResetColor( );
  823. 			Console.Error.WriteLine( "lack and white in console, no highlighting" );
  824.  
  825. 			Console.ForegroundColor = bold;
  826. 			Console.Error.Write( "         /F:regex" );
  827. 			Console.ResetColor( );
  828. 			Console.Error.Write( "    use " );
  829. 			Console.ForegroundColor = bold;
  830. 			Console.Error.Write( "regex" );
  831. 			Console.ResetColor( );
  832. 			Console.Error.Write( " to filter input on-the-" );
  833. 			Console.ForegroundColor = bold;
  834. 			Console.Error.Write( "F" );
  835. 			Console.ResetColor( );
  836. 			Console.Error.WriteLine( "ly (see Notes)" );
  837.  
  838. 			Console.ForegroundColor = bold;
  839. 			Console.Error.Write( "         /H:height" );
  840. 			Console.ResetColor( );
  841. 			Console.Error.Write( "   sets the " );
  842. 			Console.ForegroundColor = bold;
  843. 			Console.Error.Write( "H" );
  844. 			Console.ResetColor( );
  845. 			Console.Error.WriteLine( "eight of the input box" );
  846.  
  847. 			Console.Error.WriteLine( "                     (default: {0}; minimum: {0}; maximum: screen height)", defheight );
  848.  
  849. 			Console.ForegroundColor = bold;
  850. 			Console.Error.Write( "         /I" );
  851. 			Console.ResetColor( );
  852. 			Console.Error.Write( "          regular expressions are case " );
  853. 			Console.ForegroundColor = bold;
  854. 			Console.Error.Write( "I" );
  855. 			Console.ResetColor( );
  856. 			Console.Error.WriteLine( "nsensitive" );
  857.  
  858. 			Console.Error.WriteLine( "                     (default: regular expressions are case sensitive)" );
  859.  
  860. 			Console.ForegroundColor = bold;
  861. 			Console.Error.Write( "         /L[:string]" );
  862. 			Console.ResetColor( );
  863. 			Console.Error.Write( " use " );
  864. 			Console.ForegroundColor = bold;
  865. 			Console.Error.Write( "L" );
  866. 			Console.ResetColor( );
  867. 			Console.Error.WriteLine( "ocalized or custom captions (see Notes)" );
  868.  
  869. 			Console.ForegroundColor = bold;
  870. 			Console.Error.Write( "         /M:mask" );
  871. 			Console.ResetColor( );
  872. 			Console.Error.Write( "     accept input only if it matches " );
  873. 			Console.ForegroundColor = bold;
  874. 			Console.Error.WriteLine( "mask" );
  875. 			Console.ResetColor( );
  876.  
  877. 			Console.ForegroundColor = bold;
  878. 			Console.Error.Write( "         /N          N" );
  879. 			Console.ResetColor( );
  880. 			Console.Error.WriteLine( "ot filtered, only doublequotes are removed from input" );
  881.  
  882. 			Console.Error.Write( "                     (default: remove " );
  883. 			Console.ForegroundColor = bold;
  884. 			Console.Error.Write( "& < > | \"" );
  885. 			Console.ResetColor( );
  886. 			Console.Error.WriteLine( ")" );
  887.  
  888. 			Console.ForegroundColor = bold;
  889. 			Console.Error.Write( "         /P" );
  890. 			Console.ResetColor( );
  891. 			Console.Error.Write( "          hides (masks) the input text (for " );
  892. 			Console.ForegroundColor = bold;
  893. 			Console.Error.Write( "P" );
  894. 			Console.ResetColor( );
  895. 			Console.Error.WriteLine( "asswords)" );
  896.  
  897. 			Console.ForegroundColor = bold;
  898. 			Console.Error.Write( "         /R:regex" );
  899. 			Console.ResetColor( );
  900. 			Console.Error.Write( "    accept input only if it matches " );
  901. 			Console.ForegroundColor = bold;
  902. 			Console.Error.Write( "R" );
  903. 			Console.ResetColor( );
  904. 			Console.Error.Write( "egular expression " );
  905. 			Console.ForegroundColor = bold;
  906. 			Console.Error.WriteLine( "regex" );
  907. 			Console.ResetColor( );
  908.  
  909. 			Console.ForegroundColor = bold;
  910. 			Console.Error.Write( "         /S[:text]" );
  911. 			Console.ResetColor( );
  912. 			Console.Error.Write( "   inserts a checkbox \"" );
  913. 			Console.ForegroundColor = bold;
  914. 			Console.Error.Write( "S" );
  915. 			Console.ResetColor( );
  916. 			Console.Error.Write( "how password\" (or specified " );
  917. 			Console.ForegroundColor = bold;
  918. 			Console.Error.Write( "text" );
  919. 			Console.ResetColor( );
  920. 			Console.Error.WriteLine( ")" );
  921.  
  922. 			Console.ForegroundColor = bold;
  923. 			Console.Error.Write( "         /T[:sec]" );
  924. 			Console.ResetColor( );
  925. 			Console.Error.Write( "    sets the optional " );
  926. 			Console.ForegroundColor = bold;
  927. 			Console.Error.Write( "T" );
  928. 			Console.ResetColor( );
  929. 			Console.Error.Write( "imeout in " );
  930. 			Console.ForegroundColor = bold;
  931. 			Console.Error.Write( "sec" );
  932. 			Console.ResetColor( );
  933. 			Console.Error.WriteLine( "onds (default: {0})", deftimeout );
  934.  
  935. 			Console.ForegroundColor = bold;
  936. 			Console.Error.Write( "         /U" );
  937. 			Console.ResetColor( );
  938. 			Console.Error.Write( "          return " );
  939. 			Console.ForegroundColor = bold;
  940. 			Console.Error.Write( "U" );
  941. 			Console.ResetColor( );
  942. 			Console.Error.Write( "nmasked input, without literals (requires " );
  943. 			Console.ForegroundColor = bold;
  944. 			Console.Error.Write( "/M" );
  945. 			Console.ResetColor( );
  946. 			Console.Error.WriteLine( ")" );
  947.  
  948. 			Console.Error.WriteLine( "                     (default: include literals in result)" );
  949.  
  950. 			Console.ForegroundColor = bold;
  951. 			Console.Error.Write( "         /W:width" );
  952. 			Console.ResetColor( );
  953. 			Console.Error.Write( "    sets the " );
  954. 			Console.ForegroundColor = bold;
  955. 			Console.Error.Write( "W" );
  956. 			Console.ResetColor( );
  957. 			Console.Error.WriteLine( "idth of the input box" );
  958.  
  959. 			Console.Error.WriteLine( "                     (default: {0}; minimum: {0}; maximum: screen width)", defwidth );
  960.  
  961. 			Console.Error.WriteLine( );
  962.  
  963. 			Console.Error.WriteLine( "Example: prompt for password" );
  964. 			Console.ForegroundColor = bold;
  965.  
  966. 			Console.Error.WriteLine( "InputBox.exe \"Enter your password:\" \"Login\" /S" );
  967. 			Console.ResetColor( );
  968.  
  969. 			Console.Error.WriteLine( );
  970.  
  971. 			Console.Error.WriteLine( "Example: fixed length hexadecimal input (enter as a single command line)" );
  972.  
  973. 			Console.ForegroundColor = bold;
  974. 			Console.Error.WriteLine( "InputBox.exe \"Enter a MAC address:\" \"MAC Address\" \"0022446688AACCEE\"" );
  975.  
  976. 			Console.Error.WriteLine( "             /M:\">CC\\:CC\\:CC\\:CC\\:CC\\:CC\\:CC\\:CC\" /R:\"[\\dA-F]{16}\"" );
  977.  
  978. 			Console.Error.WriteLine( "             /F:\"[\\dA-F]{0,16}\" /U /I" );
  979. 			Console.ResetColor( );
  980.  
  981. 			Console.Error.WriteLine( );
  982.  
  983. 			Console.Error.Write( "Notes:   For hidden input (" );
  984. 			Console.ForegroundColor = bold;
  985. 			Console.Error.Write( "/P" );
  986. 			Console.ResetColor( );
  987. 			Console.Error.Write( " and/or " );
  988. 			Console.ForegroundColor = bold;
  989. 			Console.Error.Write( "/S), " );
  990. 			Console.ResetColor( );
  991. 			Console.Error.Write( "), " );
  992. 			Console.ForegroundColor = bold;
  993. 			Console.Error.Write( "\"default\"" );
  994. 			Console.ResetColor( );
  995. 			Console.Error.WriteLine( " will be ignored." );
  996.  
  997. 			Console.Error.Write( "         With " );
  998. 			Console.ForegroundColor = bold;
  999. 			Console.Error.Write( "/F" );
  1000. 			Console.ResetColor( );
  1001. 			Console.Error.Write( ", " );
  1002. 			Console.ForegroundColor = bold;
  1003. 			Console.Error.Write( "regex" );
  1004. 			Console.ResetColor( );
  1005. 			Console.Error.Write( " must test the " );
  1006. 			Console.ForegroundColor = bold;
  1007. 			Console.Error.Write( "unmasked" );
  1008. 			Console.ResetColor( );
  1009. 			Console.Error.WriteLine( " input (without literals), e.g." );
  1010.  
  1011. 			Console.ForegroundColor = bold;
  1012. 			Console.Error.Write( "         /M:\"CC:CC:CC:CC:CC:CC:CC:CC\" /F:\"[\\dA-F]{0,16}\" /I" );
  1013. 			Console.ResetColor( );
  1014. 			Console.Error.WriteLine( " for MAC address." );
  1015.  
  1016. 			Console.Error.Write( "         With " );
  1017. 			Console.ForegroundColor = bold;
  1018. 			Console.Error.Write( "/R" );
  1019. 			Console.ResetColor( );
  1020. 			Console.Error.Write( ", " );
  1021. 			Console.ForegroundColor = bold;
  1022. 			Console.Error.Write( "regex" );
  1023. 			Console.ResetColor( );
  1024. 			Console.Error.WriteLine( " is used to test input after OK is clicked;" );
  1025.  
  1026. 			Console.Error.Write( "         with " );
  1027. 			Console.ForegroundColor = bold;
  1028. 			Console.Error.Write( "/F" );
  1029. 			Console.ResetColor( );
  1030. 			Console.Error.Write( ", " );
  1031. 			Console.ForegroundColor = bold;
  1032. 			Console.Error.Write( "regex" );
  1033. 			Console.ResetColor( );
  1034. 			Console.Error.WriteLine( " is used to test input each time the input" );
  1035.  
  1036. 			Console.Error.Write( "         changes, so " );
  1037. 			Console.ForegroundColor = bold;
  1038. 			Console.Error.Write( "regex" );
  1039. 			Console.ResetColor( );
  1040. 			Console.Error.WriteLine( " must be able to cope with partial input;" );
  1041.  
  1042. 			Console.Error.Write( "         e.g. " );
  1043. 			Console.ForegroundColor = bold;
  1044. 			Console.Error.Write( "/F:\"[\\dA-F]{0,16}\"" );
  1045. 			Console.ResetColor( );
  1046. 			Console.Error.Write( " is OK, but " );
  1047. 			Console.ForegroundColor = bold;
  1048. 			Console.Error.Write( "/F:\"[\\dA-F]{16}\"" );
  1049. 			Console.ResetColor( );
  1050. 			Console.Error.WriteLine( " will fail." );
  1051.  
  1052. 			Console.Error.Write( "         Be careful with " );
  1053. 			Console.ForegroundColor = bold;
  1054. 			Console.Error.Write( "/N" );
  1055. 			Console.ResetColor( );
  1056. 			Console.Error.WriteLine( ", use doublequotes for the \"captured\" result," );
  1057.  
  1058. 			Console.Error.WriteLine( "         or redirect the result to a (temporary) file." );
  1059.  
  1060. 			Console.Error.Write( "         Show password (" );
  1061. 			Console.ForegroundColor = bold;
  1062. 			Console.Error.Write( "/S" );
  1063. 			Console.ResetColor( );
  1064. 			Console.Error.Write( ") implies hiding the input text (" );
  1065. 			Console.ForegroundColor = bold;
  1066. 			Console.Error.Write( "/P" );
  1067. 			Console.ResetColor( );
  1068. 			Console.Error.WriteLine( ")." );
  1069.  
  1070. 			Console.Error.Write( "         Use " );
  1071. 			Console.ForegroundColor = bold;
  1072. 			Console.Error.Write( "/M" );
  1073. 			Console.ResetColor( );
  1074. 			Console.Error.Write( " (without " );
  1075. 			Console.ForegroundColor = bold;
  1076. 			Console.Error.Write( "mask" );
  1077. 			Console.ResetColor( );
  1078. 			Console.Error.Write( ") to show detailed help on the " );
  1079. 			Console.ForegroundColor = bold;
  1080. 			Console.Error.Write( "mask" );
  1081. 			Console.ResetColor( );
  1082. 			Console.Error.WriteLine( " language." );
  1083.  
  1084. 			Console.Error.Write( "         Use " );
  1085. 			Console.ForegroundColor = bold;
  1086. 			Console.Error.Write( "/L" );
  1087. 			Console.ResetColor( );
  1088. 			Console.Error.Write( " for " );
  1089. 			Console.ForegroundColor = bold;
  1090. 			Console.Error.Write( "L" );
  1091. 			Console.ResetColor( );
  1092. 			Console.Error.WriteLine( "ocalized \"OK\" and \"Cancel\" button captions." );
  1093.  
  1094. 			Console.Error.Write( "         Custom captions require a " );
  1095. 			Console.ForegroundColor = bold;
  1096. 			Console.Error.Write( "string" );
  1097. 			Console.ResetColor( );
  1098. 			Console.Error.Write( " like " );
  1099. 			Console.ForegroundColor = bold;
  1100. 			Console.Error.WriteLine( "/L:\"OK=caption;Cancel=caption\"" );
  1101. 			Console.ResetColor( );
  1102.  
  1103. 			Console.Error.WriteLine( "         (button=caption pairs separated by semicolons, each button optional)" );
  1104.  
  1105. 			Console.Error.WriteLine( "         Text from input is written to Standard Output only if \"OK\" is clicked." );
  1106.  
  1107. 			Console.Error.WriteLine( "         Return code is 0 for \"OK\", 1 for (command line) errors, 2 for" );
  1108.  
  1109. 			Console.Error.WriteLine( "         \"Cancel\", 3 on timeout, 4 if no regex or mask match." );
  1110.  
  1111. 			Console.Error.WriteLine( );
  1112.  
  1113. 			Console.Error.WriteLine( "Credits: On-the-fly form based on code by Gorkem Gencay on StackOverflow:" );
  1114.  
  1115. 			if ( !bw )
  1116. 			{
  1117. 				Console.ForegroundColor = ConsoleColor.DarkGray;
  1118. 			}
  1119. 			Console.Error.WriteLine( "         http://stackoverflow.com/questions/97097#17546909" );
  1120. 			Console.ResetColor( );
  1121.  
  1122. 			Console.Error.WriteLine( "         Code to retrieve localized button captions by Martin Stoeckli:" );
  1123.  
  1124. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  1125. 			Console.Error.WriteLine( "         http://martinstoeckli.ch/csharp/csharp.html#windows_text_resources" );
  1126. 			Console.ResetColor( );
  1127.  
  1128. 			Console.Error.WriteLine( );
  1129.  
  1130. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  1131.  
  1132. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  1133.  
  1134. 			return 1;
  1135. 		}
  1136.  
  1137.  
  1138. 		public static int ValidateAndShowResult( )
  1139. 		{
  1140. 			string input = String.Empty;
  1141. 			// Read input from MaskedTextBox or TextBox
  1142. 			if ( usemask )
  1143. 			{
  1144. 				if ( returnunmasked )
  1145. 				{
  1146. 					maskedtextbox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
  1147. 				}
  1148. 				else
  1149. 				{
  1150. 					maskedtextbox.TextMaskFormat = MaskFormat.IncludeLiterals;
  1151. 				}
  1152. 				input = maskedtextbox.Text;
  1153. 				// Check if input complies with mask
  1154. 				if ( !maskedtextbox.MaskCompleted )
  1155. 				{
  1156. 					return 4;
  1157. 				}
  1158. 			}
  1159. 			else
  1160. 			{
  1161. 				input = textbox.Text;
  1162. 			}
  1163.  
  1164. 			// Check if input complies with regex
  1165. 			if ( regexset && Regex.IsMatch( input, regexpattern, casesensitivity ) )
  1166. 			{
  1167. 				return 4;
  1168. 			}
  1169.  
  1170. 			// Remove ampersands and redirection symbols unless /N switch was used
  1171. 			if ( filtered )
  1172. 			{
  1173. 				input = Regex.Replace( input, @"[&<>|]", String.Empty );
  1174. 			}
  1175.  
  1176. 			// Remove doublequotes from output
  1177. 			input = input.Replace( "\"", "" );
  1178. 			Console.WriteLine( input );
  1179. 			return 0;
  1180. 		}
  1181.  
  1182.  
  1183. 		public static void WriteTableRow( string col1text, string col2text, int col1percentage, bool col1bold = true, bool col2bold = false )
  1184. 		{
  1185. 			// Wrap text to fit in 2 columns
  1186. 			oddrow = !oddrow;
  1187. 			int windowwidth = Console.WindowWidth;
  1188. 			int col1width = Convert.ToInt32( windowwidth * col1percentage / 100 );
  1189. 			int col2width = windowwidth - col1width - 5; // Column separator = 4, subtract 1 extra to prevent automatic line wrap
  1190. 			List<string> col1lines = new List<string>( );
  1191. 			List<string> col2lines = new List<string>( );
  1192. 			// Column 1
  1193. 			if ( col1text.Length > col1width )
  1194. 			{
  1195. 				Regex regex = new Regex( @".{1," + col1width + @"}(?=\s|$)" );
  1196. 				if ( regex.IsMatch( col1text ) )
  1197. 				{
  1198. 					MatchCollection matches = regex.Matches( col1text );
  1199. 					foreach ( Match match in matches )
  1200. 					{
  1201. 						col1lines.Add( match.ToString( ).Trim( ) );
  1202. 					}
  1203. 				}
  1204. 				else
  1205. 				{
  1206. 					while ( col1text.Length > 0 )
  1207. 					{
  1208. 						col1lines.Add( col1text.Trim( ).Substring( 0, Math.Min( col1width, col1text.Length ) ) );
  1209. 						col1text = col1text.Substring( Math.Min( col1width, col1text.Length ) ).Trim( );
  1210. 					}
  1211. 				}
  1212. 			}
  1213. 			else
  1214. 			{
  1215. 				col1lines.Add( col1text.Trim( ) );
  1216. 			}
  1217. 			// Column 2
  1218. 			if ( col2text.Length > col2width )
  1219. 			{
  1220. 				Regex regex = new Regex( @".{1," + col2width + @"}(?=\s|$)" );
  1221. 				if ( regex.IsMatch( col2text ) )
  1222. 				{
  1223. 					MatchCollection matches = regex.Matches( col2text );
  1224. 					foreach ( Match match in matches )
  1225. 					{
  1226. 						col2lines.Add( match.ToString( ).Trim( ) );
  1227. 					}
  1228. 				}
  1229. 				else
  1230. 				{
  1231. 					while ( col2text.Length > 0 )
  1232. 					{
  1233. 						col2lines.Add( col2text.Trim( ).Substring( 0, Math.Min( col2width, col2text.Length ) ) );
  1234. 						col2text = col2text.Substring( Math.Min( col2width, col2text.Length ) ).Trim( );
  1235. 					}
  1236. 				}
  1237. 			}
  1238. 			else
  1239. 			{
  1240. 				col2lines.Add( col2text.Trim( ) );
  1241. 			}
  1242. 			for ( int i = 0; i < Math.Max( col1lines.Count, col2lines.Count ); i++ )
  1243. 			{
  1244. 				if ( oddrow && !bw )
  1245. 				{
  1246. 					Console.BackgroundColor = ConsoleColor.DarkGray;
  1247. 				}
  1248. 				if ( col1bold || oddrow )
  1249. 				{
  1250. 					Console.ForegroundColor = bold;
  1251. 				}
  1252. 				Console.Write( "{0,-" + col1width + "}    ", ( i < col1lines.Count ? col1lines[i] : String.Empty ) );
  1253. 				Console.ResetColor( );
  1254. 				if ( oddrow && !bw )
  1255. 				{
  1256. 					Console.BackgroundColor = ConsoleColor.DarkGray;
  1257. 				}
  1258. 				if ( col2bold || oddrow )
  1259. 				{
  1260. 					Console.ForegroundColor = bold;
  1261. 				}
  1262. 				Console.WriteLine( "{0,-" + col2width + "}", ( i < col2lines.Count ? col2lines[i] : String.Empty ) );
  1263. 				Console.ResetColor( );
  1264. 			}
  1265. 		}
  1266.  
  1267.  
  1268. 		#region Get Localized Captions
  1269.  
  1270. 		// Code to retrieve localized captions by Martin Stoeckli
  1271. 		// http://martinstoeckli.ch/csharp/csharp.html#windows_text_resources
  1272.  
  1273. 		/// <summary>
  1274. 		/// Searches for a text resource in a Windows library.
  1275. 		/// Sometimes, using the existing Windows resources, you can make your code
  1276. 		/// language independent and you don't have to care about translation problems.
  1277. 		/// </summary>
  1278. 		/// <example>
  1279. 		///   btnCancel.Text = Load("user32.dll", 801, "Cancel");
  1280. 		///   btnYes.Text = Load("user32.dll", 805, "Yes");
  1281. 		/// </example>
  1282. 		/// <param name="libraryName">Name of the windows library like "user32.dll"
  1283. 		/// or "shell32.dll"</param>
  1284. 		/// <param name="ident">Id of the string resource.</param>
  1285. 		/// <param name="defaultText">Return this text, if the resource string could
  1286. 		/// not be found.</param>
  1287. 		/// <returns>Requested string if the resource was found,
  1288. 		/// otherwise the <paramref name="defaultText"/></returns>
  1289. 		public static string Load( string libraryName, UInt32 ident, string defaultText )
  1290. 		{
  1291. 			IntPtr libraryHandle = GetModuleHandle( libraryName );
  1292. 			if ( libraryHandle != IntPtr.Zero )
  1293. 			{
  1294. 				StringBuilder sb = new StringBuilder( 1024 );
  1295. 				int size = LoadString( libraryHandle, ident, sb, 1024 );
  1296. 				if ( size > 0 )
  1297. 					return sb.ToString( );
  1298. 			}
  1299. 			return defaultText;
  1300. 		}
  1301.  
  1302. 		[DllImport( "kernel32.dll", CharSet = CharSet.Auto )]
  1303. 		private static extern IntPtr GetModuleHandle( string lpModuleName );
  1304.  
  1305. 		[DllImport( "user32.dll", CharSet = CharSet.Auto )]
  1306. 		private static extern int LoadString( IntPtr hInstance, UInt32 uID, StringBuilder lpBuffer, Int32 nBufferMax );
  1307.  
  1308. 		#endregion Get Localized Captions
  1309. 	}
  1310. }
  1311.  

page last uploaded: 2018-12-04, 10:47