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

page last uploaded: 2021-01-27, 16:12