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.38";
  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.Length > 0 && 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.TabIndex = 0;
  524. 				maskedtextbox.Focus( );
  525. 			}
  526. 			else
  527. 			{
  528. 				textbox.BringToFront( ); // Bug workaround
  529. 				textbox.Select( 0, 0 ); // Move cursor to begin
  530. 				textbox.TabIndex = 0;
  531. 				textbox.Focus( );
  532. 			}
  533.  
  534. 			#endregion Define Form
  535.  
  536.  
  537. 			#region Show Dialog and Return Result
  538.  
  539. 			DialogResult result = inputBox.ShowDialog( );
  540. 			if ( result == DialogResult.OK )
  541. 			{
  542. 				int rc = ValidateAndShowResult( );
  543. 				return rc;
  544. 			}
  545. 			else
  546. 			{
  547. 				if ( timeoutelapsed )
  548. 				{
  549. 					ValidateAndShowResult( );
  550. 					return 3;
  551. 				}
  552. 				else
  553. 				{
  554. 					return 2;
  555. 				}
  556. 			}
  557.  
  558. 			#endregion Show Dialog and Return Result
  559. 		}
  560.  
  561.  
  562. 		#region Event Handlers
  563.  
  564. 		public static void Checkbox_Click( object sender, System.EventArgs e )
  565. 		{
  566. 			// Toggle between hidden and normal text
  567. 			if ( usemask )
  568. 			{
  569. 				if ( maskedtextbox.PasswordChar == '*' )
  570. 				{
  571. 					maskedtextbox.PasswordChar = '\0';
  572. 				}
  573. 				else
  574. 				{
  575. 					maskedtextbox.PasswordChar = '*';
  576. 				}
  577. 			}
  578. 			else
  579. 			{
  580. 				if ( textbox.PasswordChar == '*' )
  581. 				{
  582. 					textbox.PasswordChar = '\0';
  583. 				}
  584. 				else
  585. 				{
  586. 					textbox.PasswordChar = '*';
  587. 				}
  588. 			}
  589. 		}
  590.  
  591.  
  592. 		private static void Maskedtextbox_KeyUp( object sender, KeyEventArgs e )
  593. 		{
  594. 			maskedtextbox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
  595. 			currentinput = maskedtextbox.Text;
  596. 			if ( Regex.IsMatch( currentinput, ontheflypattern, casesensitivity ) )
  597. 			{
  598. 				previousinput = currentinput;
  599. 			}
  600. 			else
  601. 			{
  602. 				currentinput = previousinput;
  603. 			}
  604. 			if ( maskedtextbox.Text != currentinput )
  605. 			{
  606. 				maskedtextbox.Text = currentinput;
  607. 				maskedtextbox.TextMaskFormat = MaskFormat.IncludeLiterals;
  608. 				if ( currentinput.Length > 0 )
  609. 				{
  610. 					maskedtextbox.SelectionStart = maskedtextbox.Text.LastIndexOf( currentinput.Last<char>( ) ) + 1;
  611. 				}
  612. 				else
  613. 				{
  614. 					maskedtextbox.SelectionStart = 0;
  615. 				}
  616. 				maskedtextbox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
  617. 			}
  618. 		}
  619.  
  620.  
  621. 		private static void Textbox_KeyUp( object sender, KeyEventArgs e )
  622. 		{
  623. 			currentinput = textbox.Text;
  624. 			if ( Regex.IsMatch( currentinput, ontheflypattern, casesensitivity ) )
  625. 			{
  626. 				previousinput = currentinput;
  627. 			}
  628. 			else
  629. 			{
  630. 				currentinput = previousinput;
  631. 			}
  632. 			if ( textbox.Text != currentinput )
  633. 			{
  634. 				textbox.Text = currentinput;
  635. 				textbox.SelectionStart = currentinput.Length;
  636. 			}
  637. 		}
  638.  
  639.  
  640. 		public static void Timer_Elapsed( object sender, System.EventArgs e )
  641. 		{
  642. 			timeoutelapsed = true;
  643. 			Process.GetCurrentProcess( ).CloseMainWindow( );
  644. 		}
  645.  
  646. 		#endregion Event Handlers
  647.  
  648.  
  649. 		public static int HelpMessage( string subject )
  650. 		{
  651. 			switch ( subject.ToLower( ) )
  652. 			{
  653. 				case "mask":
  654. 					int col1perc = 13;
  655. 					Console.Error.Write( "Help for command line switch " );
  656. 					Console.ForegroundColor = bold;
  657. 					Console.Error.WriteLine( "/M:mask" );
  658. 					Console.ResetColor( );
  659. 					Console.Error.WriteLine( );
  660. 					Console.Error.Write( "The " );
  661. 					Console.ForegroundColor = bold;
  662. 					Console.Error.Write( "mask" );
  663. 					Console.ResetColor( );
  664. 					Console.Error.WriteLine( " \"language\" is based on the Masked Edit control in Visual Basic 6.0:" );
  665. 					if ( !bw )
  666. 					{
  667. 						Console.ForegroundColor = ConsoleColor.DarkGray;
  668. 					}
  669. 					string url1 = "http://msdn.microsoft.com/en-us/library/";
  670. 					string url2 = "system.windows.forms.maskedtextbox.mask.aspx#remarksToggle";
  671. 					if ( url1.Length + url2.Length > Console.WindowWidth )
  672. 					{
  673. 						Console.Error.WriteLine( url1 );
  674. 						Console.Error.WriteLine( url2 );
  675. 					}
  676. 					else
  677. 					{
  678. 						Console.Error.WriteLine( url1 + url2 );
  679. 					}
  680. 					Console.ResetColor( );
  681. 					Console.Error.WriteLine( );
  682. 					WriteTableRow( "Masking element", "Description", col1perc, true, true );
  683. 					WriteTableRow( "0", "Digit, required. This element will accept any single digit between 0 and 9.", col1perc );
  684. 					WriteTableRow( "9", "Digit or space, optional.", col1perc );
  685. 					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 );
  686. 					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 );
  687. 					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 );
  688. 					WriteTableRow( "&", "Character, required. Any non-control character. If ASCII only is set (/A), this element behaves like the \"A\" element.", col1perc );
  689. 					WriteTableRow( "C", "Character, optional. Any non-control character. If ASCII only is set (/A), this element behaves like the \"a\" element.", col1perc );
  690. 					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 );
  691. 					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 );
  692. 					WriteTableRow( ".", "Decimal placeholder.", col1perc );
  693. 					WriteTableRow( ",", "Thousands placeholder.", col1perc );
  694. 					WriteTableRow( ":", "Time separator.", col1perc );
  695. 					WriteTableRow( "/", "Date separator.", col1perc );
  696. 					WriteTableRow( "$", "Currency symbol.", col1perc );
  697. 					WriteTableRow( "<", "Shift down. Converts all characters that follow to lowercase.", col1perc );
  698. 					WriteTableRow( ">", "Shift up. Converts all characters that follow to uppercase.", col1perc );
  699. 					WriteTableRow( "|", "Disable a previous shift up or shift down.", col1perc );
  700. 					WriteTableRow( @"\", "Escape. Escapes a mask character, turning it into a literal. \"\\\\\" is the escape sequence for a backslash.", col1perc );
  701. 					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 );
  702. 					break;
  703. 				default:
  704. 					return ShowHelp( );
  705. 			}
  706. 			return 1;
  707. 		}
  708.  
  709.  
  710. 		public static int ShowHelp( params string[] errmsg )
  711. 		{
  712. 			/*
  713. 			InputBox,  Version 1.34
  714. 			Prompt for input (GUI)
  715.  
  716. 			Usage:   INPUTBOX  [ "prompt"  [ "title"  [ "default" ] ] ] [ options ]
  717.  
  718. 			Where:   "prompt"    is the text above the input field (use \n for new line)
  719. 			         "title"     is the caption in the title bar
  720. 			         "default"   is the default answer shown in the input field
  721.  
  722. 			Options: /A          accepts ASCII characters only (requires /M)
  723. 			         /B          use standard Black and white in console, no highlighting
  724. 			         /F:regex    use regex to filter input on-the-Fly (see Notes)
  725. 			         /H:height   sets the Height of the input box
  726. 			                     (default: 110; minimum: 110; maximum: screen height)
  727. 			         /I          regular expressions are case Insensitive
  728. 			                     (default: regular expressions are case sensitive)
  729. 			         /L[:string] use Localized or custom captions (see Notes)
  730. 			         /M:mask     accept input only if it matches mask (template)
  731. 			         /O          dialog window remains always On top
  732. 			         /N          Not filtered, only doublequotes are removed from input
  733. 			                     (default: remove & < > | ")
  734. 			         /P          hides (masks) the input text (for Passwords)
  735. 			         /R:regex    accept input only if it matches Regular expression regex
  736. 			         /S[:text]   inserts a checkbox "Show password" (or specified text)
  737. 			         /T[:sec]    sets the optional Timeout in seconds (default: 60)
  738. 			         /U          return Unmasked input, without literals (requires /M)
  739. 			                     (default: include literals in result)
  740. 			         /W:width    sets the Width of the input box
  741. 			                     (default: 200; minimum: 200; maximum: screen width)
  742.  
  743. 			Example: prompt for password
  744. 			InputBox.exe "Enter your password:" "Login" /S
  745.  
  746. 			Example: fixed length hexadecimal input (enter as a single command line)
  747. 			InputBox.exe "Enter a MAC address:" "MAC Address" "0022446688AACCEE"
  748. 			             /M:">CC\:CC\:CC\:CC\:CC\:CC" /R:"[\dA-F]{16}"
  749. 			             /F:"[\dA-F]{1,16}" /U /I
  750.  
  751. 			Notes:   For hidden input (/P and/or /S), "default" will be ignored.
  752. 			         With /F, regex must test the unmasked input (without literals), e.g.
  753. 			         /M:"CC:CC:CC:CC:CC:CC:CC:CC" /F:"[\dA-F]{0,16} /I" for MAC address.
  754. 			         With /R, regex is used to test input after OK is clicked;
  755. 			         with /F, regex is used to test input each time the input
  756. 			         changes, so regex must be able to cope with partial input;
  757. 			         e.g. /F:"[\dA-F]{0,16}" is OK, but /F:"[\dA-F]{16}" will fail.
  758. 			         Be careful with /N, use doublequotes for the "captured" result,
  759. 			         or redirect the result to a (temporary) file.
  760. 			         Show password (/S) implies hiding the input text (/P).
  761. 			         Use /M (without mask) to show detailed help on the mask language.
  762. 			         Use /L for Localized "OK" and "Cancel" button captions.
  763. 			         Custom captions require a string like /L:"OK=caption;Cancel=caption"
  764. 			         (button=caption pairs separated by semicolons, each button optional).
  765. 			         Text from input is written to Standard Out only if "OK" is clicked.
  766. 			         Return code is 0 for "OK", 1 for (command line) errors, 2 for
  767. 			         "Cancel", 3 on timeout, 4 if no regex or mask match.
  768.  
  769. 			Credits: On-the-fly form based on code by Gorkem Gencay on StackOverflow:
  770. 			         http://stackoverflow.com/questions/97097#17546909
  771. 			         Code to retrieve localized button captions by Martin Stoeckli:
  772. 			         http://martinstoeckli.ch/csharp/csharp.html#windows_text_resources
  773.  
  774. 			Written by Rob van der Woude
  775. 			http://www.robvanderwoude.com
  776. 			*/
  777.  
  778. 			if ( errmsg.Length > 0 )
  779. 			{
  780. 				List<string> errargs = new List<string>( errmsg );
  781. 				errargs.RemoveAt( 0 );
  782. 				Console.Error.WriteLine( );
  783. 				if ( !bw )
  784. 				{
  785. 					Console.ForegroundColor = ConsoleColor.Red;
  786. 				}
  787. 				Console.Error.Write( "ERROR:\t" );
  788. 				Console.ForegroundColor = bold;
  789. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  790. 				Console.ResetColor( );
  791. 			}
  792.  
  793. 			Console.Error.WriteLine( );
  794.  
  795. 			Console.Error.WriteLine( "InputBox,  Version {0}", progver );
  796.  
  797. 			Console.Error.WriteLine( "Prompt for input (GUI)" );
  798.  
  799. 			Console.Error.WriteLine( );
  800.  
  801. 			Console.Error.Write( "Usage:   " );
  802. 			Console.ForegroundColor = bold;
  803. 			Console.Error.WriteLine( "INPUTBOX  [ \"prompt\"  [ \"title\"  [ \"default\" ] ] ] [ options ]" );
  804. 			Console.ResetColor( );
  805.  
  806. 			Console.Error.WriteLine( );
  807.  
  808. 			Console.Error.Write( "Where:   " );
  809. 			Console.ForegroundColor = bold;
  810. 			Console.Error.Write( "\"prompt\"" );
  811. 			Console.ResetColor( );
  812. 			Console.Error.WriteLine( "    is the text above the input field (use \\n for new line)" );
  813.  
  814. 			Console.ForegroundColor = bold;
  815. 			Console.Error.Write( "         \"title\"" );
  816. 			Console.ResetColor( );
  817. 			Console.Error.WriteLine( "     is the caption in the title bar" );
  818.  
  819. 			Console.ForegroundColor = bold;
  820. 			Console.Error.Write( "         \"default\"" );
  821. 			Console.ResetColor( );
  822. 			Console.Error.WriteLine( "   is the default answer shown in the input field" );
  823.  
  824. 			Console.Error.WriteLine( );
  825.  
  826. 			Console.Error.Write( "Options: " );
  827. 			Console.ForegroundColor = bold;
  828. 			Console.Error.Write( "/A" );
  829. 			Console.ResetColor( );
  830. 			Console.Error.Write( "          accepts " );
  831. 			Console.ForegroundColor = bold;
  832. 			Console.Error.Write( "A" );
  833. 			Console.ResetColor( );
  834. 			Console.Error.Write( "SCII characters only (requires " );
  835. 			Console.ForegroundColor = bold;
  836. 			Console.Error.Write( "/M" );
  837. 			Console.ResetColor( );
  838. 			Console.Error.WriteLine( ")" );
  839.  
  840. 			Console.ForegroundColor = bold;
  841. 			Console.Error.Write( "         /B" );
  842. 			Console.ResetColor( );
  843. 			Console.Error.Write( "          use standard " );
  844. 			Console.ForegroundColor = bold;
  845. 			Console.Error.Write( "B" );
  846. 			Console.ResetColor( );
  847. 			Console.Error.WriteLine( "lack and white in console, no highlighting" );
  848.  
  849. 			Console.ForegroundColor = bold;
  850. 			Console.Error.Write( "         /F:regex" );
  851. 			Console.ResetColor( );
  852. 			Console.Error.Write( "    use " );
  853. 			Console.ForegroundColor = bold;
  854. 			Console.Error.Write( "regex" );
  855. 			Console.ResetColor( );
  856. 			Console.Error.Write( " to filter input on-the-" );
  857. 			Console.ForegroundColor = bold;
  858. 			Console.Error.Write( "F" );
  859. 			Console.ResetColor( );
  860. 			Console.Error.WriteLine( "ly (see Notes)" );
  861.  
  862. 			Console.ForegroundColor = bold;
  863. 			Console.Error.Write( "         /H:height" );
  864. 			Console.ResetColor( );
  865. 			Console.Error.Write( "   sets the " );
  866. 			Console.ForegroundColor = bold;
  867. 			Console.Error.Write( "H" );
  868. 			Console.ResetColor( );
  869. 			Console.Error.WriteLine( "eight of the input box" );
  870.  
  871. 			Console.Error.WriteLine( "                     (default: {0}; minimum: {0}; maximum: screen height)", defheight );
  872.  
  873. 			Console.ForegroundColor = bold;
  874. 			Console.Error.Write( "         /I" );
  875. 			Console.ResetColor( );
  876. 			Console.Error.Write( "          regular expressions are case " );
  877. 			Console.ForegroundColor = bold;
  878. 			Console.Error.Write( "I" );
  879. 			Console.ResetColor( );
  880. 			Console.Error.WriteLine( "nsensitive" );
  881.  
  882. 			Console.Error.WriteLine( "                     (default: regular expressions are case sensitive)" );
  883.  
  884. 			Console.ForegroundColor = bold;
  885. 			Console.Error.Write( "         /L[:string]" );
  886. 			Console.ResetColor( );
  887. 			Console.Error.Write( " use " );
  888. 			Console.ForegroundColor = bold;
  889. 			Console.Error.Write( "L" );
  890. 			Console.ResetColor( );
  891. 			Console.Error.WriteLine( "ocalized or custom captions (see Notes)" );
  892.  
  893. 			Console.ForegroundColor = bold;
  894. 			Console.Error.Write( "         /M:mask" );
  895. 			Console.ResetColor( );
  896. 			Console.Error.Write( "     accept input only if it matches " );
  897. 			Console.ForegroundColor = bold;
  898. 			Console.Error.WriteLine( "mask" );
  899. 			Console.ResetColor( );
  900.  
  901. 			Console.ForegroundColor = bold;
  902. 			Console.Error.Write( "         /N          N" );
  903. 			Console.ResetColor( );
  904. 			Console.Error.WriteLine( "ot filtered, only doublequotes are removed from input" );
  905.  
  906. 			Console.Error.Write( "                     (default: remove " );
  907. 			Console.ForegroundColor = bold;
  908. 			Console.Error.Write( "& < > | \"" );
  909. 			Console.ResetColor( );
  910. 			Console.Error.WriteLine( ")" );
  911.  
  912. 			Console.ForegroundColor = bold;
  913. 			Console.Error.Write( "         /O" );
  914. 			Console.ResetColor( );
  915. 			Console.Error.Write( "          dialog window remains always " );
  916. 			Console.ForegroundColor = bold;
  917. 			Console.Error.Write( "O" );
  918. 			Console.ResetColor( );
  919. 			Console.Error.WriteLine( "n top" );
  920.  
  921. 			Console.ForegroundColor = bold;
  922. 			Console.Error.Write( "         /P" );
  923. 			Console.ResetColor( );
  924. 			Console.Error.Write( "          hides (masks) the input text (for " );
  925. 			Console.ForegroundColor = bold;
  926. 			Console.Error.Write( "P" );
  927. 			Console.ResetColor( );
  928. 			Console.Error.WriteLine( "asswords)" );
  929.  
  930. 			Console.ForegroundColor = bold;
  931. 			Console.Error.Write( "         /R:regex" );
  932. 			Console.ResetColor( );
  933. 			Console.Error.Write( "    accept input only if it matches " );
  934. 			Console.ForegroundColor = bold;
  935. 			Console.Error.Write( "R" );
  936. 			Console.ResetColor( );
  937. 			Console.Error.Write( "egular expression " );
  938. 			Console.ForegroundColor = bold;
  939. 			Console.Error.WriteLine( "regex" );
  940. 			Console.ResetColor( );
  941.  
  942. 			Console.ForegroundColor = bold;
  943. 			Console.Error.Write( "         /S[:text]" );
  944. 			Console.ResetColor( );
  945. 			Console.Error.Write( "   inserts a checkbox \"" );
  946. 			Console.ForegroundColor = bold;
  947. 			Console.Error.Write( "S" );
  948. 			Console.ResetColor( );
  949. 			Console.Error.Write( "how password\" (or specified " );
  950. 			Console.ForegroundColor = bold;
  951. 			Console.Error.Write( "text" );
  952. 			Console.ResetColor( );
  953. 			Console.Error.WriteLine( ")" );
  954.  
  955. 			Console.ForegroundColor = bold;
  956. 			Console.Error.Write( "         /T[:sec]" );
  957. 			Console.ResetColor( );
  958. 			Console.Error.Write( "    sets the optional " );
  959. 			Console.ForegroundColor = bold;
  960. 			Console.Error.Write( "T" );
  961. 			Console.ResetColor( );
  962. 			Console.Error.Write( "imeout in " );
  963. 			Console.ForegroundColor = bold;
  964. 			Console.Error.Write( "sec" );
  965. 			Console.ResetColor( );
  966. 			Console.Error.WriteLine( "onds (default: {0})", deftimeout );
  967.  
  968. 			Console.ForegroundColor = bold;
  969. 			Console.Error.Write( "         /U" );
  970. 			Console.ResetColor( );
  971. 			Console.Error.Write( "          return " );
  972. 			Console.ForegroundColor = bold;
  973. 			Console.Error.Write( "U" );
  974. 			Console.ResetColor( );
  975. 			Console.Error.Write( "nmasked input, without literals (requires " );
  976. 			Console.ForegroundColor = bold;
  977. 			Console.Error.Write( "/M" );
  978. 			Console.ResetColor( );
  979. 			Console.Error.WriteLine( ")" );
  980.  
  981. 			Console.Error.WriteLine( "                     (default: include literals in result)" );
  982.  
  983. 			Console.ForegroundColor = bold;
  984. 			Console.Error.Write( "         /W:width" );
  985. 			Console.ResetColor( );
  986. 			Console.Error.Write( "    sets the " );
  987. 			Console.ForegroundColor = bold;
  988. 			Console.Error.Write( "W" );
  989. 			Console.ResetColor( );
  990. 			Console.Error.WriteLine( "idth of the input box" );
  991.  
  992. 			Console.Error.WriteLine( "                     (default: {0}; minimum: {0}; maximum: screen width)", defwidth );
  993.  
  994. 			Console.Error.WriteLine( );
  995.  
  996. 			Console.Error.WriteLine( "Example: prompt for password" );
  997. 			Console.ForegroundColor = bold;
  998.  
  999. 			Console.Error.WriteLine( "InputBox.exe \"Enter your password:\" \"Login\" /S" );
  1000. 			Console.ResetColor( );
  1001.  
  1002. 			Console.Error.WriteLine( );
  1003.  
  1004. 			Console.Error.WriteLine( "Example: fixed length hexadecimal input (enter as a single command line)" );
  1005.  
  1006. 			Console.ForegroundColor = bold;
  1007. 			Console.Error.WriteLine( "InputBox.exe \"Enter a MAC address:\" \"MAC Address\" \"0022446688AACCEE\"" );
  1008.  
  1009. 			Console.Error.WriteLine( "             /M:\">CC\\:CC\\:CC\\:CC\\:CC\\:CC\\:CC\\:CC\" /R:\"[\\dA-F]{16}\"" );
  1010.  
  1011. 			Console.Error.WriteLine( "             /F:\"[\\dA-F]{0,16}\" /U /I" );
  1012. 			Console.ResetColor( );
  1013.  
  1014. 			Console.Error.WriteLine( );
  1015.  
  1016. 			Console.Error.Write( "Notes:   For hidden input (" );
  1017. 			Console.ForegroundColor = bold;
  1018. 			Console.Error.Write( "/P" );
  1019. 			Console.ResetColor( );
  1020. 			Console.Error.Write( " and/or " );
  1021. 			Console.ForegroundColor = bold;
  1022. 			Console.Error.Write( "/S), " );
  1023. 			Console.ResetColor( );
  1024. 			Console.Error.Write( "), " );
  1025. 			Console.ForegroundColor = bold;
  1026. 			Console.Error.Write( "\"default\"" );
  1027. 			Console.ResetColor( );
  1028. 			Console.Error.WriteLine( " will be ignored." );
  1029.  
  1030. 			Console.Error.Write( "         With " );
  1031. 			Console.ForegroundColor = bold;
  1032. 			Console.Error.Write( "/F" );
  1033. 			Console.ResetColor( );
  1034. 			Console.Error.Write( ", " );
  1035. 			Console.ForegroundColor = bold;
  1036. 			Console.Error.Write( "regex" );
  1037. 			Console.ResetColor( );
  1038. 			Console.Error.Write( " must test the " );
  1039. 			Console.ForegroundColor = bold;
  1040. 			Console.Error.Write( "unmasked" );
  1041. 			Console.ResetColor( );
  1042. 			Console.Error.WriteLine( " input (without literals), e.g." );
  1043.  
  1044. 			Console.ForegroundColor = bold;
  1045. 			Console.Error.Write( "         /M:\"CC:CC:CC:CC:CC:CC:CC:CC\" /F:\"[\\dA-F]{0,16}\" /I" );
  1046. 			Console.ResetColor( );
  1047. 			Console.Error.WriteLine( " for MAC address." );
  1048.  
  1049. 			Console.Error.Write( "         With " );
  1050. 			Console.ForegroundColor = bold;
  1051. 			Console.Error.Write( "/R" );
  1052. 			Console.ResetColor( );
  1053. 			Console.Error.Write( ", " );
  1054. 			Console.ForegroundColor = bold;
  1055. 			Console.Error.Write( "regex" );
  1056. 			Console.ResetColor( );
  1057. 			Console.Error.WriteLine( " is used to test input after OK is clicked;" );
  1058.  
  1059. 			Console.Error.Write( "         with " );
  1060. 			Console.ForegroundColor = bold;
  1061. 			Console.Error.Write( "/F" );
  1062. 			Console.ResetColor( );
  1063. 			Console.Error.Write( ", " );
  1064. 			Console.ForegroundColor = bold;
  1065. 			Console.Error.Write( "regex" );
  1066. 			Console.ResetColor( );
  1067. 			Console.Error.WriteLine( " is used to test input each time the input" );
  1068.  
  1069. 			Console.Error.Write( "         changes, so " );
  1070. 			Console.ForegroundColor = bold;
  1071. 			Console.Error.Write( "regex" );
  1072. 			Console.ResetColor( );
  1073. 			Console.Error.WriteLine( " must be able to cope with partial input;" );
  1074.  
  1075. 			Console.Error.Write( "         e.g. " );
  1076. 			Console.ForegroundColor = bold;
  1077. 			Console.Error.Write( "/F:\"[\\dA-F]{0,16}\"" );
  1078. 			Console.ResetColor( );
  1079. 			Console.Error.Write( " is OK, but " );
  1080. 			Console.ForegroundColor = bold;
  1081. 			Console.Error.Write( "/F:\"[\\dA-F]{16}\"" );
  1082. 			Console.ResetColor( );
  1083. 			Console.Error.WriteLine( " will fail." );
  1084.  
  1085. 			Console.Error.Write( "         Be careful with " );
  1086. 			Console.ForegroundColor = bold;
  1087. 			Console.Error.Write( "/N" );
  1088. 			Console.ResetColor( );
  1089. 			Console.Error.WriteLine( ", use doublequotes for the \"captured\" result," );
  1090.  
  1091. 			Console.Error.WriteLine( "         or redirect the result to a (temporary) file." );
  1092.  
  1093. 			Console.Error.Write( "         Show password (" );
  1094. 			Console.ForegroundColor = bold;
  1095. 			Console.Error.Write( "/S" );
  1096. 			Console.ResetColor( );
  1097. 			Console.Error.Write( ") implies hiding the input text (" );
  1098. 			Console.ForegroundColor = bold;
  1099. 			Console.Error.Write( "/P" );
  1100. 			Console.ResetColor( );
  1101. 			Console.Error.WriteLine( ")." );
  1102.  
  1103. 			Console.Error.Write( "         Use " );
  1104. 			Console.ForegroundColor = bold;
  1105. 			Console.Error.Write( "/M" );
  1106. 			Console.ResetColor( );
  1107. 			Console.Error.Write( " (without " );
  1108. 			Console.ForegroundColor = bold;
  1109. 			Console.Error.Write( "mask" );
  1110. 			Console.ResetColor( );
  1111. 			Console.Error.Write( ") to show detailed help on the " );
  1112. 			Console.ForegroundColor = bold;
  1113. 			Console.Error.Write( "mask" );
  1114. 			Console.ResetColor( );
  1115. 			Console.Error.WriteLine( " language." );
  1116.  
  1117. 			Console.Error.Write( "         Use " );
  1118. 			Console.ForegroundColor = bold;
  1119. 			Console.Error.Write( "/L" );
  1120. 			Console.ResetColor( );
  1121. 			Console.Error.Write( " for " );
  1122. 			Console.ForegroundColor = bold;
  1123. 			Console.Error.Write( "L" );
  1124. 			Console.ResetColor( );
  1125. 			Console.Error.WriteLine( "ocalized \"OK\" and \"Cancel\" button captions." );
  1126.  
  1127. 			Console.Error.Write( "         Custom captions require a " );
  1128. 			Console.ForegroundColor = bold;
  1129. 			Console.Error.Write( "string" );
  1130. 			Console.ResetColor( );
  1131. 			Console.Error.Write( " like " );
  1132. 			Console.ForegroundColor = bold;
  1133. 			Console.Error.WriteLine( "/L:\"OK=caption;Cancel=caption\"" );
  1134. 			Console.ResetColor( );
  1135.  
  1136. 			Console.Error.WriteLine( "         (button=caption pairs separated by semicolons, each button optional)" );
  1137.  
  1138. 			Console.Error.WriteLine( "         Text from input is written to Standard Output only if \"OK\" is clicked." );
  1139.  
  1140. 			Console.Error.WriteLine( "         Return code is 0 for \"OK\", 1 for (command line) errors, 2 for" );
  1141.  
  1142. 			Console.Error.WriteLine( "         \"Cancel\", 3 on timeout, 4 if no regex or mask match." );
  1143.  
  1144. 			Console.Error.WriteLine( );
  1145.  
  1146. 			Console.Error.WriteLine( "Credits: On-the-fly form based on code by Gorkem Gencay on StackOverflow:" );
  1147.  
  1148. 			if ( !bw )
  1149. 			{
  1150. 				Console.ForegroundColor = ConsoleColor.DarkGray;
  1151. 			}
  1152. 			Console.Error.WriteLine( "         http://stackoverflow.com/questions/97097#17546909" );
  1153. 			Console.ResetColor( );
  1154.  
  1155. 			Console.Error.WriteLine( "         Code to retrieve localized button captions by Martin Stoeckli:" );
  1156.  
  1157. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  1158. 			Console.Error.WriteLine( "         http://martinstoeckli.ch/csharp/csharp.html#windows_text_resources" );
  1159. 			Console.ResetColor( );
  1160.  
  1161. 			Console.Error.WriteLine( );
  1162.  
  1163. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  1164.  
  1165. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  1166.  
  1167. 			return 1;
  1168. 		}
  1169.  
  1170.  
  1171. 		public static int ValidateAndShowResult( )
  1172. 		{
  1173. 			string input;
  1174. 			// Read input from MaskedTextBox or TextBox
  1175. 			if ( usemask )
  1176. 			{
  1177. 				if ( returnunmasked )
  1178. 				{
  1179. 					maskedtextbox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
  1180. 				}
  1181. 				else
  1182. 				{
  1183. 					maskedtextbox.TextMaskFormat = MaskFormat.IncludeLiterals;
  1184. 				}
  1185. 				input = maskedtextbox.Text;
  1186. 				// Check if input complies with mask
  1187. 				if ( !maskedtextbox.MaskCompleted )
  1188. 				{
  1189. 					return 4;
  1190. 				}
  1191. 			}
  1192. 			else
  1193. 			{
  1194. 				input = textbox.Text;
  1195. 			}
  1196.  
  1197. 			// Check if input complies with regex
  1198. 			if ( regexset && Regex.IsMatch( input, regexpattern, casesensitivity ) )
  1199. 			{
  1200. 				return 4;
  1201. 			}
  1202.  
  1203. 			// Remove ampersands and redirection symbols unless /N switch was used
  1204. 			if ( filtered )
  1205. 			{
  1206. 				input = Regex.Replace( input, @"[&<>|]", String.Empty );
  1207. 			}
  1208.  
  1209. 			// Remove doublequotes from output
  1210. 			input = input.Replace( "\"", "" );
  1211. 			Console.WriteLine( input );
  1212. 			return 0;
  1213. 		}
  1214.  
  1215.  
  1216. 		public static void WriteTableRow( string col1text, string col2text, int col1percentage, bool col1bold = true, bool col2bold = false )
  1217. 		{
  1218. 			// Wrap text to fit in 2 columns
  1219. 			oddrow = !oddrow;
  1220. 			int windowwidth = Console.WindowWidth;
  1221. 			int col1width = Convert.ToInt32( windowwidth * col1percentage / 100 );
  1222. 			int col2width = windowwidth - col1width - 5; // Column separator = 4, subtract 1 extra to prevent automatic line wrap
  1223. 			List<string> col1lines = new List<string>( );
  1224. 			List<string> col2lines = new List<string>( );
  1225. 			// Column 1
  1226. 			if ( col1text.Length > col1width )
  1227. 			{
  1228. 				Regex regex = new Regex( @".{1," + col1width + @"}(?=\s|$)" );
  1229. 				if ( regex.IsMatch( col1text ) )
  1230. 				{
  1231. 					MatchCollection matches = regex.Matches( col1text );
  1232. 					foreach ( Match match in matches )
  1233. 					{
  1234. 						col1lines.Add( match.ToString( ).Trim( ) );
  1235. 					}
  1236. 				}
  1237. 				else
  1238. 				{
  1239. 					while ( col1text.Length > 0 )
  1240. 					{
  1241. 						col1lines.Add( col1text.Trim( ).Substring( 0, Math.Min( col1width, col1text.Length ) ) );
  1242. 						col1text = col1text.Substring( Math.Min( col1width, col1text.Length ) ).Trim( );
  1243. 					}
  1244. 				}
  1245. 			}
  1246. 			else
  1247. 			{
  1248. 				col1lines.Add( col1text.Trim( ) );
  1249. 			}
  1250. 			// Column 2
  1251. 			if ( col2text.Length > col2width )
  1252. 			{
  1253. 				Regex regex = new Regex( @".{1," + col2width + @"}(?=\s|$)" );
  1254. 				if ( regex.IsMatch( col2text ) )
  1255. 				{
  1256. 					MatchCollection matches = regex.Matches( col2text );
  1257. 					foreach ( Match match in matches )
  1258. 					{
  1259. 						col2lines.Add( match.ToString( ).Trim( ) );
  1260. 					}
  1261. 				}
  1262. 				else
  1263. 				{
  1264. 					while ( col2text.Length > 0 )
  1265. 					{
  1266. 						col2lines.Add( col2text.Trim( ).Substring( 0, Math.Min( col2width, col2text.Length ) ) );
  1267. 						col2text = col2text.Substring( Math.Min( col2width, col2text.Length ) ).Trim( );
  1268. 					}
  1269. 				}
  1270. 			}
  1271. 			else
  1272. 			{
  1273. 				col2lines.Add( col2text.Trim( ) );
  1274. 			}
  1275. 			for ( int i = 0; i < Math.Max( col1lines.Count, col2lines.Count ); i++ )
  1276. 			{
  1277. 				if ( oddrow && !bw )
  1278. 				{
  1279. 					Console.BackgroundColor = ConsoleColor.DarkGray;
  1280. 				}
  1281. 				if ( col1bold || oddrow )
  1282. 				{
  1283. 					Console.ForegroundColor = bold;
  1284. 				}
  1285. 				Console.Write( "{0,-" + col1width + "}    ", ( i < col1lines.Count ? col1lines[i] : String.Empty ) );
  1286. 				Console.ResetColor( );
  1287. 				if ( oddrow && !bw )
  1288. 				{
  1289. 					Console.BackgroundColor = ConsoleColor.DarkGray;
  1290. 				}
  1291. 				if ( col2bold || oddrow )
  1292. 				{
  1293. 					Console.ForegroundColor = bold;
  1294. 				}
  1295. 				Console.WriteLine( "{0,-" + col2width + "}", ( i < col2lines.Count ? col2lines[i] : String.Empty ) );
  1296. 				Console.ResetColor( );
  1297. 			}
  1298. 		}
  1299.  
  1300.  
  1301. 		#region Get Localized Captions
  1302.  
  1303. 		// Code to retrieve localized captions by Martin Stoeckli
  1304. 		// http://martinstoeckli.ch/csharp/csharp.html#windows_text_resources
  1305.  
  1306. 		/// <summary>
  1307. 		/// Searches for a text resource in a Windows library.
  1308. 		/// Sometimes, using the existing Windows resources, you can make your code
  1309. 		/// language independent and you don't have to care about translation problems.
  1310. 		/// </summary>
  1311. 		/// <example>
  1312. 		///   btnCancel.Text = Load("user32.dll", 801, "Cancel");
  1313. 		///   btnYes.Text = Load("user32.dll", 805, "Yes");
  1314. 		/// </example>
  1315. 		/// <param name="libraryName">Name of the windows library like "user32.dll"
  1316. 		/// or "shell32.dll"</param>
  1317. 		/// <param name="ident">Id of the string resource.</param>
  1318. 		/// <param name="defaultText">Return this text, if the resource string could
  1319. 		/// not be found.</param>
  1320. 		/// <returns>Requested string if the resource was found,
  1321. 		/// otherwise the <paramref name="defaultText"/></returns>
  1322. 		public static string Load( string libraryName, UInt32 ident, string defaultText )
  1323. 		{
  1324. 			IntPtr libraryHandle = GetModuleHandle( libraryName );
  1325. 			if ( libraryHandle != IntPtr.Zero )
  1326. 			{
  1327. 				StringBuilder sb = new StringBuilder( 1024 );
  1328. 				int size = LoadString( libraryHandle, ident, sb, 1024 );
  1329. 				if ( size > 0 )
  1330. 					return sb.ToString( );
  1331. 			}
  1332. 			return defaultText;
  1333. 		}
  1334.  
  1335. 		[DllImport( "kernel32.dll", CharSet = CharSet.Auto )]
  1336. 		private static extern IntPtr GetModuleHandle( string lpModuleName );
  1337.  
  1338. 		[DllImport( "user32.dll", CharSet = CharSet.Auto )]
  1339. 		private static extern int LoadString( IntPtr hInstance, UInt32 uID, StringBuilder lpBuffer, Int32 nBufferMax );
  1340.  
  1341. 		#endregion Get Localized Captions
  1342. 	}
  1343. }
  1344.  

page last modified: 2024-04-16; loaded in 0.0261 seconds