Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for dialogboxes_dropdownbox.cs

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

  1. using System.Linq;
  2.  
  3.  
  4. namespace RobvanderWoude
  5. {
  6. 	[System.Runtime.InteropServices.Guid( "A201493F-EB79-4928-98A0-CA1BA5BF3B6B" )]
  7. 	public interface DropDownBox_Interface
  8. 	{
  9. 		[System.Runtime.InteropServices.DispId( 1 )]
  10. 		void CheckUpdate( );
  11.  
  12. 		[System.Runtime.InteropServices.DispId( 2 )]
  13. 		string Credits( );
  14.  
  15. 		[System.Runtime.InteropServices.DispId( 3 )]
  16. 		string Help( int html = 0 );
  17.  
  18. 		[System.Runtime.InteropServices.DispId( 4 )]
  19. 		string ListProperties( );
  20.  
  21. 		[System.Runtime.InteropServices.DispId( 5 )]
  22. 		string SampleCode( );
  23.  
  24. 		[System.Runtime.InteropServices.DispId( 6 )]
  25. 		void Show( );
  26. 	}
  27.  
  28.  
  29. 	[System.Runtime.InteropServices.Guid( "BD2C3B92-F797-4D5B-B809-D64B13CD27CF" ), System.Runtime.InteropServices.InterfaceType( System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch )]
  30. 	public interface DropDownBox_Events
  31. 	{
  32. 	}
  33.  
  34.  
  35. 	[System.Runtime.InteropServices.Guid( "72B657DB-DEE3-45D4-9907-354CCBE01D7D" ), System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.AutoDual ), System.Runtime.InteropServices.ComSourceInterfaces( typeof( DropDownBox_Events ) )]
  36. 	public class DropDownBox : DropDownBox_Interface
  37. 	{
  38. 		#region Default Values
  39.  
  40. 		const string defaultcaptioncancel = "Cancel";
  41. 		const string defaultcaptionok = "OK";
  42. 		static readonly System.Drawing.FontFamily defaultfontfamily = System.Drawing.FontFamily.GenericSansSerif;
  43. 		const float defaultfontsize = 10;
  44. 		static readonly string defaulttitle = string.Format( "{0},  Version {1}", Global.Common.ProgramInfo.FileName, Global.Common.ProgramInfo.FileVersion );
  45. 		const int defaultwindowheight = 90;
  46. 		const int defaultwindowwidth = 200;
  47. 		const float minimumfontsize = 6;
  48. 		const float maximumfontsize = 48;
  49.  
  50. 		#endregion Default Values
  51.  
  52.  
  53. 		static System.Collections.Generic.List<string> dropdownlist = new System.Collections.Generic.List<string>( );
  54. 		static System.Timers.Timer timer;
  55. 		static System.Windows.Forms.Form dropdownboxform;
  56. 		static System.Windows.Forms.ComboBox combobox;
  57.  
  58.  
  59. 		#region Methods
  60.  
  61. 		public void CheckUpdate( )
  62. 		{
  63. 			Global.Common.ProgramInfo.CheckUpdate( );
  64. 		}
  65.  
  66.  
  67. 		public string Credits( )
  68. 		{
  69. 			return Global.Common.Credits( );
  70. 		}
  71.  
  72.  
  73. 		public string Help( int html = 0 )
  74. 		{
  75. 			int col1width = 0;
  76. 			int col2width = 0;
  77. 			int col3width = 0;
  78. 			int col4width = 0;
  79. 			int col5width = 0;
  80. 			string help = string.Empty;
  81. 			string linetemplate = string.Empty;
  82. 			string separatorline = string.Empty;
  83.  
  84. 			if ( html == 1 )
  85. 			{
  86. 				help += "<h1>Help for DropDownBox class</h1>\n\n";
  87. 				help += "<p>Present a dropdown list of choices, and return the selected value</p>\n\n";
  88. 				help += "<h2>COM ProgID: RobvanderWoude.DropDownBox</h2>\n\n";
  89. 			}
  90. 			else
  91. 			{
  92. 				help += "Help for DropDownBox class\n";
  93. 				help += new string( '\u2500', help.Length ) + "\n\n";
  94. 				help += "Present a dropdown list of choices, and return the selected value\n\n\n";
  95. 				help += "COM ProgID: RobvanderWoude.DropDownBox\n\n\n";
  96. 			}
  97.  
  98. 			#region Properties
  99.  
  100. 			if ( html == 1)
  101. 			{
  102. 				separatorline = string.Empty;
  103. 				linetemplate = "<tr>\n\t<td>{0}</td>\n\t<td>{1}</td>\n\t<td>{2}</td>\n\t<td>{3}</td>\n\t<td>{4}</td>\n</tr>\n";
  104.  
  105. 				help += "<table>\n";
  106. 				help += "<tr>\n";
  107. 				help += "\t<th colspan=\"5\">Properties (Note that all properties are string or integer, no booleans, objects or arrays)</th>\n";
  108. 				help += "</tr>\n";
  109.  
  110. 				help += "<tr>\n";
  111. 				help += "\t<th>Property Name</th>\n";
  112. 				help += "\t<th>Description</th>\n";
  113. 				help += "\t<th>Mandatory</th>\n";
  114. 				help += "\t<th>Read-Only</th>\n";
  115. 				help += "\t<th>Default (Allowed) Values</th>\n";
  116. 				help += "</tr>\n";
  117. 			}
  118. 			else
  119. 			{
  120. 				col1width = 16;
  121. 				col2width = 50;
  122. 				col3width = 9;
  123. 				col4width = 9;
  124. 				col5width = 44;
  125.  
  126. 				separatorline = "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u253C" + new string( '\u2500', 2 + col3width ) + "\u253C" + new string( '\u2500', 2 + col4width ) + "\u253C" + new string( '\u2500', 2 + col5width ) + "\u2524\n";
  127. 				linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502 {3,-" + col4width.ToString( ) + "} \u2502 {4,-" + col5width.ToString( ) + "} \u2502\n";
  128.  
  129. 				help += "\u250C" + new string( '\u2500', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2510\n";
  130. 				help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n";
  131. 				help += string.Format( "\u2502 {0,-" + ( 12 + col1width + col2width + col3width + col4width + col5width ).ToString( ) + "} \u2502\n", "Properties        (Note that all properties are string or integer, no booleans, objects or arrays)" );
  132. 				help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n";
  133. 				help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u2564" + new string( '\u2550', 2 + col2width ) + "\u2564" + new string( '\u2550', 2 + col3width ) + "\u2564" + new string( '\u2550', 2 + col4width ) + "\u2564" + new string( '\u2550', 2 + col5width ) + "\u2561\n";
  134. 				help += string.Format( linetemplate, "Property Name", "Description", "Mandatory", "Read-Only", "Default (Allowed) Values" );
  135. 				help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u256A" + new string( '\u2550', 2 + col2width ) + "\u256A" + new string( '\u2550', 2 + col3width ) + "\u256A" + new string( '\u2550', 2 + col4width ) + "\u256A" + new string( '\u2550', 2 + col5width ) + "\u2561\n";
  136. 			}
  137.  
  138. 			help += HelpTableRow( linetemplate, "captioncancel", Global.Common.Help.captioncancel, "no", "no", defaultcaptioncancel, html );
  139. 			help += separatorline;
  140.  
  141. 			help += HelpTableRow( linetemplate, "captionok", Global.Common.Help.captionok, "no", "no", defaultcaptionok, html );
  142. 			help += separatorline;
  143.  
  144. 			help += HelpTableRow( linetemplate, "debuginfo", Global.Common.Help.debuginfo, "N/A", "YES", "", html );
  145. 			help += separatorline;
  146.  
  147. 			help += HelpTableRow( linetemplate, "defaultindex", "Index of initially selected item in dropdown", "no", "no", "-1: none selected", html );
  148. 			help += separatorline;
  149.  
  150. 			help += HelpTableRow( linetemplate, "errors", Global.Common.Help.errors, "N/A", "YES", "", html );
  151. 			help += separatorline;
  152.  
  153. 			help += HelpTableRow( linetemplate, "fontfamily", Global.Common.Help.fontfamily, "no", "no", defaultfontfamily.Name, html );
  154. 			help += separatorline;
  155.  
  156. 			help += HelpTableRow( linetemplate, "fontsize", Global.Common.Help.fontsize, "no", "no", string.Format( "{0} ({1}..{2})", defaultfontsize, minimumfontsize, maximumfontsize ), html );
  157. 			help += separatorline;
  158.  
  159. 			help += HelpTableRow( linetemplate, "left", Global.Common.Help.left, "no", "no", "Centered (0..screenwidth-windowwidth)", html );
  160. 			help += separatorline;
  161.  
  162. 			if ( html == 1 )
  163. 			{
  164. 				string template = "<tr>\n\t<td>{0}</td>\n\t<td>{1}</td>\n\t<td colspan=\"2\" style=\"vertical-align: middle;\">{2}</td>\n\t<td>{3}</td>\n\t<td colspan=\"2\" style=\"vertical-align: middle;\">{4}</td>\n</tr>\n";
  165. 				help += string.Format( template, "list", "Semicolon-separated list of items for dropdown", "* YES", "no", "* Set either \"list\" or \"listfile\" property" );
  166. 				template = "<tr>\n\t<td>{0}</td>\n\t<td>{1}</td>\n\t<td>{2}</td>\n</tr>\n";
  167. 				help += string.Format( template, "listfile", "Path to file containing list", "no" );
  168. 			}
  169. 			else
  170. 			{
  171. 				help += HelpTableRow( linetemplate, "list", "Semicolon-separated list of items for dropdown", "", "no", "", html );
  172. 				help += "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u2524 " + string.Format( "{0,-" + col3width + "}", "* YES" ) + " \u251C" + new string( '\u2500', 2 + col4width ) + "\u2524 " + string.Format( "{0,-" + col5width + "}", "* Set either \"list\" or \"listfile\" property" ) + " \u2502\n";
  173. 				help += HelpTableRow( linetemplate, "listfile", "Path to file containing list", "", "no", new string( ' ', 6 ) + new string( '\u2550', 6 ) + new string( ' ', 8 ) + new string( '\u2550', 2 ), html );
  174. 			}
  175. 			help += separatorline;
  176.  
  177. 			help += HelpTableRow( linetemplate, "literal", Global.Common.Help.literal, "no", "no", "0: interpret \"\\t\" as tab and \"\\n\" as newline", html );
  178. 			help += separatorline;
  179.  
  180. 			help += HelpTableRow( linetemplate, "localizecaptions", Global.Common.Help.localizecaptions, "no", "no", "0: English captions", html );
  181. 			help += separatorline;
  182.  
  183. 			help += HelpTableRow( linetemplate, "modal", Global.Common.Help.modal, "no", "no", "1: always on top", html );
  184. 			help += separatorline;
  185.  
  186. 			help += HelpTableRow( linetemplate, "prompt", Global.Common.Help.prompt, "no", "no", "No prompt", html );
  187. 			help += separatorline;
  188.  
  189. 			help += HelpTableRow( linetemplate, "selectedindex", "Index of last selected dropdown item", "N/A", "YES", "", html );
  190. 			help += separatorline;
  191.  
  192. 			help += HelpTableRow( linetemplate, "selecteditem", "Text of last selected dropdown item", "N/A", "YES", "", html );
  193. 			help += separatorline;
  194.  
  195. 			help += HelpTableRow( linetemplate, "timeout", Global.Common.Help.timeout, "no", "no", "0: no timeout", html );
  196. 			help += separatorline;
  197.  
  198. 			help += HelpTableRow( linetemplate, "timeoutelapsed", Global.Common.Help.timeoutelapsed, "N/A", "YES", "", html );
  199. 			help += separatorline;
  200.  
  201. 			help += HelpTableRow( linetemplate, "title", Global.Common.Help.title, "no", "no", defaulttitle, html );
  202. 			help += separatorline;
  203.  
  204. 			help += HelpTableRow( linetemplate, "top", Global.Common.Help.top, "no", "no", "Centered (0..screenheight-windowheight)", html );
  205. 			help += separatorline;
  206.  
  207. 			help += HelpTableRow( linetemplate, "Version", Global.Common.Help.version, "", html );
  208. 			help += separatorline;
  209.  
  210. 			help += HelpTableRow( linetemplate, "windowheight", "Height of dialog window", "no", "no", string.Format( "{0} ({0}..screenheight)", defaultwindowheight ), html );
  211. 			help += separatorline;
  212.  
  213. 			help += HelpTableRow( linetemplate, "windowwidth", "Width of dialog window", "no", "no", string.Format( "{0} ({0}..screenwidth)", defaultwindowwidth ), html );
  214.  
  215. 			if ( html == 1 )
  216. 			{
  217. 				help += "</table>\n\n\n";
  218. 			}
  219. 			else
  220. 			{
  221. 				help += "\u2514" + new string( '\u2500', 2 + col1width ) + "\u2534" + new string( '\u2500', 2 + col2width ) + "\u2534" + new string( '\u2500', 2 + col3width ) + "\u2534" + new string( '\u2500', 2 + col4width ) + "\u2534" + new string( '\u2500', 2 + col5width ) + "\u2518\n\n\n";
  222. 			}
  223.  
  224.  
  225. 			#endregion Properties
  226.  
  227.  
  228. 			#region Methods
  229.  
  230. 			if ( html == 1 )
  231. 			{
  232. 				separatorline = string.Empty;
  233. 				linetemplate = "<tr>\n\t<td>{0}</td>\n\t<td>{1}</td>\n\t<td>{2}</td>\n</tr>\n";
  234.  
  235. 				help += "<table>\n";
  236. 				help += "<tr>\n";
  237. 				help += "\t<th colspan=\"3\">Methods</th>\n";
  238. 				help += "</tr>\n";
  239.  
  240. 				help += "<tr>\n";
  241. 				help += "\t<th>Method Name</th>\n";
  242. 				help += "\t<th>Description</th>\n";
  243. 				help += "\t<th>Requirements</th>\n";
  244. 				help += "</tr>\n";
  245. 			}
  246. 			else
  247. 			{
  248. 				col1width = 16;
  249. 				col2width = 74;
  250. 				col3width = 44;
  251.  
  252. 				separatorline = "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u253C" + new string( '\u2500', 2 + col3width ) + "\u2524\n";
  253. 				linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502\n";
  254.  
  255. 				help += "\u250C" + new string( '\u2500', 8 + col1width + col2width + col3width ) + "\u2510\n";
  256. 				help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n";
  257. 				help += string.Format( "\u2502 {0,-" + ( 6 + col1width + col2width + col3width ).ToString( ) + "} \u2502\n", "Methods" );
  258. 				help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n";
  259. 				help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u2564" + new string( '\u2550', 2 + col2width ) + "\u2564" + new string( '\u2550', 2 + col3width ) + "\u2561\n";
  260. 				help += string.Format( linetemplate, "Method Name", "Description", "Requirements" );
  261. 				help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u256A" + new string( '\u2550', 2 + col2width ) + "\u256A" + new string( '\u2550', 2 + col3width ) + "\u2561\n";
  262. 			}
  263.  
  264. 			help += HelpTableRow( linetemplate, "CheckUpdate", Global.Common.Help._checkupdate, "", html );
  265. 			help += separatorline;
  266.  
  267. 			help += HelpTableRow( linetemplate, "Credits", Global.Common.Help._credits, "", html );
  268. 			help += separatorline;
  269.  
  270. 			help += HelpTableRow( linetemplate, "Help", Global.Common.Help._help, "", html );
  271. 			help += separatorline;
  272.  
  273. 			help += HelpTableRow( linetemplate, "ListProperties", Global.Common.Help._listproperties, "", html );
  274. 			help += separatorline;
  275.  
  276. 			help += HelpTableRow( linetemplate, "SampleCode", Global.Common.Help._samplecode, "", html );
  277. 			help += separatorline;
  278.  
  279. 			help += HelpTableRow( linetemplate, "Show", "Presents a dropdown dialog box based on the current property values, and if \"OK\" is clicked, saves the last selected item's index in \"selectedindex\" property, and its text value in \"selecteditem\" property (if \"Cancel\" is clicked, \"selectedindex\" is set to -1 and \"selecteditem\" to an empty string).", "Either \"list\" or \"listfile\" property must be set", html );
  280.  
  281. 			if ( html == 1 )
  282. 			{
  283. 				help += "</table>\n\n\n";
  284. 			}
  285. 			else
  286. 			{
  287. 				help += "\u2514" + new string( '\u2500', 2 + col1width ) + "\u2534" + new string( '\u2500', 2 + col2width ) + "\u2534" + new string( '\u2500', 2 + col3width ) + "\u2518\n\n\n";
  288. 			}
  289.  
  290. 			#endregion Methods
  291.  
  292.  
  293. 			#region Example
  294.  
  295. 			if ( html == 1 )
  296. 			{
  297. 				help += "<h2>VBScript usage example:</h2>\n\n<pre>";
  298. 			}
  299. 			else
  300. 			{
  301. 				help += "VBScript usage example:\n";
  302. 				help += new string( '\u2500', 23 ) + "\n\n";
  303. 			}
  304.  
  305. 			help += SampleCode( );
  306.  
  307. 			if ( html == 1 )
  308. 			{
  309. 				help += "</pre>";
  310. 			}
  311.  
  312. 			help += "\n\n";
  313.  
  314. 			#endregion Example
  315.  
  316.  
  317. 			help = help.Replace( "\n", System.Environment.NewLine );
  318. 			return help;
  319. 		}
  320.  
  321.  
  322. 		private string HelpTableRow( string template, string col1text, string col2text, string col3text, int html )
  323. 		{
  324. 			if ( html == 1 )
  325. 			{
  326. 				return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text );
  327. 			}
  328. 			else
  329. 			{
  330. 				return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, "", "" );
  331. 			}
  332. 		}
  333.  
  334.  
  335. 		private string HelpTableRow( string template, string col1text, string col2text, string col3text, string col4text, string col5text, int html )
  336. 		{
  337. 			if ( html == 1 )
  338. 			{
  339. 				return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text, col4text, col5text );
  340. 			}
  341. 			else
  342. 			{
  343. 				return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, col4text, col5text );
  344. 			}
  345. 		}
  346.  
  347.  
  348. 		public string ListProperties( )
  349. 		{
  350. 			return Global.Common.Lists.Properties( this );
  351. 		}
  352.  
  353.  
  354. 		public string SampleCode( )
  355. 		{
  356. 			string code = "Set objDropDownBox = CreateObject( \"RobvanderWoude.DropDownBox\" )\n\n";
  357. 			code += "With objDropDownBox\n";
  358. 			code += "\t.list         = \"Arizona;Texas;California;Washington;Montana;Utah\"\n";
  359. 			code += "\t.defaultindex = 4\n";
  360. 			code += "\t.prompt       = \"You have 15 seconds to select a state\"\n";
  361. 			code += "\t.top          = 200\n";
  362. 			code += "\t.left         = 300\n";
  363. 			code += "\t.timeout      = 15\n";
  364. 			code += "\t.Show\n";
  365. 			code += "\tWScript.Echo \"Selected state: \" & .selecteditem\n";
  366. 			code += "End With\n\n";
  367. 			code += "Set objDropDownBox = Nothing";
  368. 			return code;
  369. 		}
  370.  
  371.  
  372. 		[System.STAThread]
  373. 		public void Show( )
  374. 		{
  375. 			if ( dropdownlist.Count == 0 )
  376. 			{
  377. 				_errors.Add( Global.Common.TimeStamp( ) + "Show( ): No list specified" );
  378. 				_selectedindex = -1;
  379. 				_selecteditem = string.Empty;
  380. 			}
  381. 			else
  382. 			{
  383. 				_selectedindex = defaultindex;
  384.  
  385.  
  386. 				#region Timer
  387.  
  388. 				_timeoutelapsed = false;
  389. 				if ( _timeout > 0 )
  390. 				{
  391. 					timer = new System.Timers.Timer( );
  392. 					timer.Elapsed += new System.Timers.ElapsedEventHandler( DropDownBox_Class_Timer_Elapsed );
  393. 					timer.Interval = timeout * 1000;
  394. 					timer.Start( );
  395. 				}
  396.  
  397. 				#endregion Timer
  398.  
  399.  
  400. 				#region Build Form
  401.  
  402. 				dropdownboxform = new System.Windows.Forms.Form( );
  403. 				System.Drawing.Size size = new System.Drawing.Size( windowwidth, windowheight );
  404. 				dropdownboxform.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  405. 				dropdownboxform.MaximizeBox = false;
  406. 				dropdownboxform.MinimizeBox = false;
  407. 				dropdownboxform.ClientSize = size;
  408. 				dropdownboxform.Text = title;
  409. 				dropdownboxform.TopMost = topmost;
  410. 				dropdownboxform.Font = new System.Drawing.Font( _fontfamily.Name, _fontsize );
  411. 				if ( left == -1 || top == -1 )
  412. 				{
  413. 					dropdownboxform.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  414. 				}
  415. 				else
  416. 				{
  417. 					dropdownboxform.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
  418. 					dropdownboxform.Location = new System.Drawing.Point( left, top );
  419. 				}
  420. 				_debuginfo.Add( timestamp + $"startposition = {dropdownboxform.StartPosition.ToString( )}; top = {top}; left = {left}; location = {dropdownboxform.Location.X},{dropdownboxform.Location.Y}" );
  421.  
  422. 				if ( !string.IsNullOrWhiteSpace( prompt ) )
  423. 				{
  424. 					System.Windows.Forms.Label labelprompt = new System.Windows.Forms.Label( );
  425. 					labelprompt.Size = new System.Drawing.Size( size.Width - 20, 20 );
  426. 					labelprompt.Location = new System.Drawing.Point( 10, 10 );
  427. 					labelprompt.Text = prompt.Replace( "\\n", "\n" ).Replace( "\\r", "\r" );
  428. 					if ( windowheight == defaultwindowheight )
  429. 					{
  430. 						// Add 20 to window height to allow space for prompt, IF and only IF the heigt wasn't set at a non-default value
  431. 						size = new System.Drawing.Size( size.Width, size.Height + 20 );
  432. 					}
  433. 					dropdownboxform.ClientSize = size;
  434. 					labelprompt.Size = new System.Drawing.Size( size.Width - 20, size.Height - 90 );
  435. 					dropdownboxform.Controls.Add( labelprompt );
  436. 				}
  437.  
  438. 				combobox = new System.Windows.Forms.ComboBox( );
  439. 				combobox.Size = new System.Drawing.Size( size.Width - 20, 25 );
  440. 				combobox.Location = new System.Drawing.Point( 10, size.Height - 70 );
  441. 				combobox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Append;
  442. 				combobox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
  443. 				combobox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  444. 				combobox.SelectedIndexChanged += new System.EventHandler( DropDownBox_Class_ComboBox_SelectedIndexChanged );
  445. 				dropdownboxform.Controls.Add( combobox );
  446.  
  447. 				System.Windows.Forms.Button okButton = new System.Windows.Forms.Button( );
  448. 				okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
  449. 				okButton.Name = "okButton";
  450. 				okButton.Size = new System.Drawing.Size( 80, 25 );
  451. 				okButton.Text = captionok;
  452. 				okButton.Location = new System.Drawing.Point( size.Width / 2 - 10 - 80, size.Height - 35 );
  453. 				dropdownboxform.Controls.Add( okButton );
  454.  
  455. 				System.Windows.Forms.Button cancelButton = new System.Windows.Forms.Button( );
  456. 				cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  457. 				cancelButton.Name = "cancelButton";
  458. 				cancelButton.Size = new System.Drawing.Size( 80, 25 );
  459. 				cancelButton.Text = captioncancel;
  460. 				cancelButton.Location = new System.Drawing.Point( size.Width / 2 + 10, size.Height - 35 );
  461. 				dropdownboxform.Controls.Add( cancelButton );
  462.  
  463. 				dropdownboxform.AcceptButton = okButton;  // OK on Enter
  464. 				dropdownboxform.CancelButton = cancelButton; // Cancel on Esc
  465. 				dropdownboxform.Activate( );
  466.  
  467. 				#endregion Build Form
  468.  
  469.  
  470. 				#region Populate Dropdown
  471.  
  472. 				foreach ( string item in dropdownlist )
  473. 				{
  474. 					combobox.Items.Add( item );
  475. 				}
  476.  
  477. 				// Preselect an item
  478. 				combobox.SelectedIndex = _defaultindex;
  479.  
  480. 				#endregion Populate Dropdown
  481.  
  482.  
  483. 				_selectedindex = combobox.SelectedIndex;
  484. 				_selecteditem = combobox.Text;
  485.  
  486.  
  487. 				#region Show Dialog and Save Result
  488.  
  489. 				System.Windows.Forms.DialogResult result = dropdownboxform.ShowDialog( );
  490. 				if ( !_timeoutelapsed && result == System.Windows.Forms.DialogResult.Cancel )
  491. 				{
  492. 					_selectedindex = -1;
  493. 					_selecteditem = string.Empty;
  494. 				}
  495.  
  496. 				#endregion Show Dialog and Save Result
  497. 			}
  498. 		}
  499.  
  500. 		#endregion Methods
  501.  
  502.  
  503. 		#region Properties
  504.  
  505. 		private string _captioncancel = defaultcaptioncancel;
  506. 		[System.Runtime.InteropServices.ComVisible( true )]
  507. 		public string captioncancel
  508. 		{
  509. 			get
  510. 			{
  511. 				if ( _localizecaptions && _captioncancel == defaultcaptioncancel )
  512. 				{
  513. 					_captioncancel = Global.Common.Localization.Load( "user32.dll", 801, defaultcaptioncancel );
  514. 				}
  515. 				return _captioncancel;
  516. 			}
  517. 			set
  518. 			{
  519. 				if ( string.IsNullOrWhiteSpace( value ) || value.Length > 20 )
  520. 				{
  521. 					_captioncancel = defaultcaptioncancel;
  522. 				}
  523. 				else
  524. 				{
  525. 					_captioncancel = value;
  526. 				}
  527. 				//_debuginfo.Add()
  528. 			}
  529. 		}
  530.  
  531.  
  532. 		private string _captionok = defaultcaptionok;
  533. 		[System.Runtime.InteropServices.ComVisible( true )]
  534. 		public string captionok
  535. 		{
  536. 			get
  537. 			{
  538. 				if ( _localizecaptions && _captionok == defaultcaptionok )
  539. 				{
  540. 					_captionok = Global.Common.Localization.Load( "user32.dll", 800, defaultcaptionok );
  541. 				}
  542. 				return _captionok;
  543. 			}
  544. 			set
  545. 			{
  546. 				if ( string.IsNullOrWhiteSpace( value ) || value.Length > 20 )
  547. 				{
  548. 					_captionok = defaultcaptionok;
  549. 				}
  550. 				else
  551. 				{
  552. 					_captionok = value;
  553. 				}
  554. 			}
  555. 		}
  556.  
  557.  
  558. 		private System.Collections.Generic.List<string> _debuginfo = new System.Collections.Generic.List<string>( );
  559. 		[System.Runtime.InteropServices.ComVisible( true )]
  560. 		public string debuginfo
  561. 		{
  562. 			get
  563. 			{
  564. 				return string.Join( "\n", _debuginfo.ToArray( ) );
  565. 			}
  566. 		}
  567.  
  568.  
  569. 		private int _defaultindex = -1;
  570. 		[System.Runtime.InteropServices.ComVisible( true )]
  571. 		public int defaultindex
  572. 		{
  573. 			get
  574. 			{
  575. 				return _defaultindex;
  576. 			}
  577. 			set
  578. 			{
  579. 				if ( value < -1 )
  580. 				{
  581. 					_debuginfo.Add( timestamp + "defaultindex < -1" );
  582. 					_defaultindex = -1;
  583. 				}
  584. 				else if ( value >= dropdownlist.Count )
  585. 				{
  586. 					_debuginfo.Add( timestamp + "dropdownlist.Count = " + dropdownlist.Count );
  587. 					_defaultindex = -1;
  588. 				}
  589. 				else
  590. 				{
  591. 					_defaultindex = value;
  592. 				}
  593. 				_debuginfo.Add( timestamp + "defaultindex = " + value );
  594. 				_selectedindex = _defaultindex;
  595. 			}
  596. 		}
  597.  
  598.  
  599. 		private System.Collections.Generic.List<string> _errors = new System.Collections.Generic.List<string>( );
  600. 		[System.Runtime.InteropServices.ComVisible( true )]
  601. 		public string errors
  602. 		{
  603. 			get
  604. 			{
  605. 				return string.Join( "\n", _errors.ToArray( ) );
  606. 			}
  607. 		}
  608.  
  609.  
  610. 		private System.Drawing.FontFamily _fontfamily = defaultfontfamily;
  611. 		[System.Runtime.InteropServices.ComVisible( true )]
  612. 		public string fontfamily
  613. 		{
  614. 			get
  615. 			{
  616. 				return _fontfamily.Name;
  617. 			}
  618. 			set
  619. 			{
  620. 				_fontfamily = Global.Common.Validate.FontFamily( value, defaultfontfamily );
  621. 			}
  622. 		}
  623.  
  624.  
  625. 		private float _fontsize = defaultfontsize;
  626. 		[System.Runtime.InteropServices.ComVisible( true )]
  627. 		public int fontsize
  628. 		{
  629. 			get
  630. 			{
  631. 				return System.Convert.ToInt32( _fontsize );
  632. 			}
  633. 			set
  634. 			{
  635. 				_fontsize = System.Convert.ToSingle( value ).LimitToRange( minimumfontsize, maximumfontsize );
  636. 			}
  637. 		}
  638.  
  639.  
  640. 		private int _left = -1;
  641. 		[System.Runtime.InteropServices.ComVisible( true )]
  642. 		public int left
  643. 		{
  644. 			get
  645. 			{
  646. 				return _left;
  647. 			}
  648. 			set
  649. 			{
  650. 				if ( value < 0 )
  651. 				{
  652. 					_left = 0;
  653. 				}
  654. 				else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - windowwidth )
  655. 				{
  656. 					_left = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - windowwidth;
  657. 				}
  658. 				else
  659. 				{
  660. 					_left = value;
  661. 				}
  662. 			}
  663. 		}
  664.  
  665.  
  666. 		private string _list = string.Empty;
  667. 		[System.Runtime.InteropServices.ComVisible( true )]
  668. 		public string list
  669. 		{
  670. 			get
  671. 			{
  672. 				return _list;
  673. 			}
  674. 			set
  675. 			{
  676. 				_list = value;
  677. 				int errorcount = _errors.Count;
  678. 				string[] dropdownlistitems = null;
  679. 				if ( _list.IndexOf( ';' ) == -1 )
  680. 				{
  681. 					_errors.Add( Global.Common.TimeStamp( ) + "Invalid list argument" );
  682. 				}
  683. 				else
  684. 				{
  685. 					dropdownlistitems = _list.Split( ";".ToCharArray( ), System.StringSplitOptions.RemoveEmptyEntries );
  686. 				}
  687. 				if ( errorcount == _errors.Count )
  688. 				{
  689. 					dropdownlist = dropdownlistitems.ToList<string>( );
  690. 					if ( defaultindex > dropdownlist.Count )
  691. 					{
  692. 						defaultindex = -1;
  693. 					}
  694. 				}
  695. 				else
  696. 				{
  697. 					_list = string.Empty;
  698. 					dropdownlist.Clear( );
  699. 				}
  700. 			}
  701. 		}
  702.  
  703.  
  704. 		private string _listfile = string.Empty;
  705. 		[System.Runtime.InteropServices.ComVisible( true )]
  706. 		public string listfile
  707. 		{
  708. 			get
  709. 			{
  710. 				return _listfile;
  711. 			}
  712. 			set
  713. 			{
  714. 				_listfile = value;
  715. 				int errorcount = _errors.Count;
  716. 				string[] dropdownlistitems = null;
  717. 				if ( System.IO.File.Exists( _list ) )
  718. 				{
  719. 					try
  720. 					{
  721. 						System.IO.StreamReader sr = new System.IO.StreamReader( list );
  722. 						string text = sr.ReadToEnd( );
  723. 						sr.Close( );
  724. 						if ( text.IndexOfAny( ";\n\r".ToCharArray( ) ) == -1 )
  725. 						{
  726. 							_errors.Add( Global.Common.TimeStamp( ) + "Invalid list file content" );
  727. 						}
  728. 						dropdownlistitems = text.Split( ";\n\r".ToCharArray( ), System.StringSplitOptions.RemoveEmptyEntries );
  729. 					}
  730. 					catch ( System.Exception e )
  731. 					{
  732. 						_errors.Add( Global.Common.TimeStamp( ) + "Unable to open list file: " + e.Message );
  733. 					}
  734.  
  735. 				}
  736. 				else
  737. 				{
  738. 					_errors.Add( Global.Common.TimeStamp( ) + "List file not found" );
  739. 				}
  740. 				if ( errorcount == _errors.Count )
  741. 				{
  742. 					dropdownlist = dropdownlistitems.ToList<string>( );
  743. 					if ( defaultindex > dropdownlist.Count )
  744. 					{
  745. 						defaultindex = -1;
  746. 					}
  747. 				}
  748. 				else
  749. 				{
  750. 					_listfile = string.Empty;
  751. 					dropdownlist.Clear( );
  752. 				}
  753. 			}
  754. 		}
  755.  
  756.  
  757. 		private bool _literal = false;
  758. 		[System.Runtime.InteropServices.ComVisible( true )]
  759. 		public int literal
  760. 		{
  761. 			get
  762. 			{
  763. 				return ( _literal ? 1 : 0 );
  764. 			}
  765. 			set
  766. 			{
  767. 				_literal = ( value == 1 );
  768. 			}
  769. 		}
  770.  
  771.  
  772. 		private bool _localizecaptions = false;
  773. 		[System.Runtime.InteropServices.ComVisible( true )]
  774. 		public int localizecaptions
  775. 		{
  776. 			get
  777. 			{
  778. 				return ( _localizecaptions ? 1 : 0 );
  779. 			}
  780. 			set
  781. 			{
  782. 				_localizecaptions = ( value == 1 );
  783. 			}
  784. 		}
  785.  
  786.  
  787. 		private bool topmost = true;
  788. 		[System.Runtime.InteropServices.ComVisible( true )]
  789. 		public int modal
  790. 		{
  791. 			get
  792. 			{
  793. 				return ( topmost ? 1 : 0 );
  794. 			}
  795. 			set
  796. 			{
  797. 				topmost = ( value == 1 );
  798. 			}
  799. 		}
  800.  
  801.  
  802. 		private string _prompt = string.Empty;
  803. 		[System.Runtime.InteropServices.ComVisible( true )]
  804. 		public string prompt
  805. 		{
  806. 			get
  807. 			{
  808. 				return _prompt;
  809. 			}
  810. 			set
  811. 			{
  812. 				if ( _literal )
  813. 				{
  814. 					_prompt = value.Trim( );
  815. 				}
  816. 				else
  817. 				{
  818. 					_prompt = value.Replace( "\\n", "\n" ).Replace( "\\t", "\t" ).Trim( );
  819. 				}
  820. 			}
  821. 		}
  822.  
  823.  
  824. 		private int _selectedindex = -1;
  825. 		[System.Runtime.InteropServices.ComVisible( true )]
  826. 		public int selectedindex
  827. 		{
  828. 			get
  829. 			{
  830. 				return _selectedindex;
  831. 			}
  832. 		}
  833.  
  834.  
  835. 		private string _selecteditem = string.Empty;
  836. 		[System.Runtime.InteropServices.ComVisible( true )]
  837. 		public string selecteditem
  838. 		{
  839. 			get
  840. 			{
  841. 				return _selecteditem;
  842. 			}
  843. 		}
  844.  
  845.  
  846. 		private int _timeout = 0;
  847. 		[System.Runtime.InteropServices.ComVisible( true )]
  848. 		public int timeout
  849. 		{
  850. 			get
  851. 			{
  852. 				return _timeout;
  853. 			}
  854. 			set
  855. 			{
  856. 				if ( value < 0 || value > 3600 )
  857. 				{
  858. 					_timeout = 0;
  859. 				}
  860. 				else
  861. 				{
  862. 					_timeout = value;
  863. 				}
  864. 			}
  865. 		}
  866.  
  867.  
  868. 		private bool _timeoutelapsed = false;
  869. 		[System.Runtime.InteropServices.ComVisible( true )]
  870. 		public int timeoutelapsed
  871. 		{
  872. 			get
  873. 			{
  874. 				return ( _timeoutelapsed ? 1 : 0 );
  875. 			}
  876. 		}
  877.  
  878.  
  879. 		public static string timestamp => Global.Common.TimeStamp( );
  880.  
  881.  
  882. 		private string _title = defaulttitle;
  883. 		[System.Runtime.InteropServices.ComVisible( true )]
  884. 		public string title
  885. 		{
  886. 			get
  887. 			{
  888. 				return _title;
  889. 			}
  890. 			set
  891. 			{
  892. 				if ( string.IsNullOrWhiteSpace( value ) )
  893. 				{
  894. 					_title = defaulttitle;
  895. 				}
  896. 				else
  897. 				{
  898. 					_title = value.Trim( );
  899. 				}
  900. 			}
  901. 		}
  902.  
  903.  
  904. 		private int _top = -1;
  905. 		[System.Runtime.InteropServices.ComVisible( true )]
  906. 		public int top
  907. 		{
  908. 			get
  909. 			{
  910. 				return _top;
  911. 			}
  912. 			set
  913. 			{
  914. 				if ( value < 0 )
  915. 				{
  916. 					_top = 0;
  917. 				}
  918. 				else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - windowheight )
  919. 				{
  920. 					_top = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - windowheight;
  921. 				}
  922. 				else
  923. 				{
  924. 					_top = value;
  925. 				}
  926. 			}
  927. 		}
  928.  
  929.  
  930. 		[System.Runtime.InteropServices.ComVisible( true )]
  931. 		public string Version
  932. 		{
  933. 			get
  934. 			{
  935. 				return Global.Common.ProgramInfo.FileVersion;
  936. 			}
  937. 		}
  938.  
  939.  
  940. 		private int _windowheight = defaultwindowheight;
  941. 		[System.Runtime.InteropServices.ComVisible( true )]
  942. 		public int windowheight
  943. 		{
  944. 			get
  945. 			{
  946. 				return _windowheight;
  947. 			}
  948. 			set
  949. 			{
  950. 				if ( value < defaultwindowheight )
  951. 				{
  952. 					_windowheight = defaultwindowheight;
  953. 				}
  954. 				else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height )
  955. 				{
  956. 					_windowheight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
  957. 				}
  958. 				else
  959. 				{
  960. 					_windowheight = value;
  961. 				}
  962. 			}
  963. 		}
  964.  
  965.  
  966. 		private int _windowwidth = defaultwindowwidth;
  967. 		[System.Runtime.InteropServices.ComVisible( true )]
  968. 		public int windowwidth
  969. 		{
  970. 			get
  971. 			{
  972. 				return _windowwidth;
  973. 			}
  974. 			set
  975. 			{
  976. 				if ( value < defaultwindowwidth )
  977. 				{
  978. 					_windowwidth = defaultwindowwidth;
  979. 				}
  980. 				else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width )
  981. 				{
  982. 					_windowwidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
  983. 				}
  984. 				else
  985. 				{
  986. 					_windowwidth = value;
  987. 				}
  988. 			}
  989. 		}
  990.  
  991. 		#endregion Properties
  992.  
  993.  
  994. 		#region Event Handlers
  995.  
  996. 		private void DropDownBox_Class_ComboBox_SelectedIndexChanged( object sender, System.EventArgs e )
  997. 		{
  998. 			combobox.Update( );
  999. 			_selectedindex = combobox.SelectedIndex;
  1000. 			_selecteditem = combobox.Text;
  1001. 		}
  1002.  
  1003.  
  1004. 		private void DropDownBox_Class_Timer_Elapsed( object sender, System.Timers.ElapsedEventArgs e )
  1005. 		{
  1006. 			_timeoutelapsed = true;
  1007. 			combobox.Update( );
  1008. 			_selectedindex = combobox.SelectedIndex;
  1009. 			_selecteditem = combobox.Text;
  1010. 			dropdownboxform.Close( );
  1011. 		}
  1012.  
  1013. 		#endregion Event Handlers
  1014. 	}
  1015. }

page last uploaded: 2019-01-21, 22:48