Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for dialogboxes_radiobuttonbox.cs

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

  1. using System.Linq;
  2.  
  3.  
  4. namespace RobvanderWoude
  5. {
  6. 	[System.Runtime.InteropServices.Guid( "1F355F39-623F-4559-BBED-C73FA3FB470D" )]
  7. 	public interface RadioButtonBox_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( "752BA062-AF40-47F6-BE1E-D7EB602672F0" ), System.Runtime.InteropServices.InterfaceType( System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch )]
  30. 	public interface RadioButtonBox_Events
  31. 	{
  32. 	}
  33.  
  34.  
  35. 	[System.Runtime.InteropServices.Guid( "8E8C1842-4B89-4F8B-869B-52CA93ED9A0C" ), System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.AutoDual ), System.Runtime.InteropServices.ComSourceInterfaces( typeof( RadioButtonBox_Events ) )]
  36. 	public class RadioButtonBox : RadioButtonBox_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.Timers.Timer timer;
  54. 		static System.Windows.Forms.Form radiobuttonboxform;
  55. 		static System.Windows.Forms.GroupBox groupbox;
  56.  
  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 RadioButtonBox class</h1>\n\n";
  87. 				help += "<p>Present a dialog with a group of radio buttons, and return the text for the selected item</p>\n\n";
  88. 				help += "<h2>COM ProgID: RobvanderWoude.RadioButtonBox</h2>\n\n";
  89. 			}
  90. 			else
  91. 			{
  92. 				help += "Help for RadioButtonBox class\n";
  93. 				help += new string( '\u2500', help.Length ) + "\n\n";
  94. 				help += "Present a dialog with a group of radio buttons, and return the text for the selected item\n\n\n";
  95. 				help += "COM ProgID: RobvanderWoude.RadioButtonBox\n\n\n";
  96. 			}
  97.  
  98.  
  99. 			#region Properties
  100.  
  101. 			if ( html == 1 )
  102. 			{
  103. 				separatorline = string.Empty;
  104. 				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";
  105.  
  106. 				help += "<table>\n";
  107. 				help += "<tr>\n";
  108. 				help += "\t<th colspan=\"5\">Properties (Note that all properties are string or integer, no booleans, objects or arrays)</th>\n";
  109. 				help += "</tr>\n";
  110.  
  111. 				help += "<tr>\n";
  112. 				help += "\t<th>Property Name</th>\n";
  113. 				help += "\t<th>Description</th>\n";
  114. 				help += "\t<th>Mandatory</th>\n";
  115. 				help += "\t<th>Read-Only</th>\n";
  116. 				help += "\t<th>Default (Allowed) Values</th>\n";
  117. 				help += "</tr>\n";
  118. 			}
  119. 			else
  120. 			{
  121. 				col1width = 18;
  122. 				col2width = 50;
  123. 				col3width = 9;
  124. 				col4width = 9;
  125. 				col5width = 42;
  126.  
  127. 				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";
  128. 				linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502 {3,-" + col4width.ToString( ) + "} \u2502 {4,-" + col5width.ToString( ) + "} \u2502\n";
  129.  
  130. 				help += "\u250C" + new string( '\u2500', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2510\n";
  131. 				help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n";
  132. 				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)" );
  133. 				help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n";
  134. 				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";
  135. 				help += string.Format( linetemplate, "Property Name", "Description", "Mandatory", "Read-Only", "Default (Allowed) Values" );
  136. 				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";
  137. 			}
  138.  
  139. 			help += HelpTableRow( linetemplate, "captioncancel", Global.Common.Help.captioncancel, "no", "no", defaultcaptioncancel, html );
  140. 			help += separatorline;
  141.  
  142. 			help += HelpTableRow( linetemplate, "captionok", Global.Common.Help.captionok, "no", "no", defaultcaptionok, html );
  143. 			help += separatorline;
  144.  
  145. 			help += HelpTableRow( linetemplate, "columns", "The number of columns", "no", "no", "", html );
  146. 			help += separatorline;
  147.  
  148. 			help += HelpTableRow( linetemplate, "debuginfo", Global.Common.Help.debuginfo, "N/A", "YES", "", html );
  149. 			help += separatorline;
  150.  
  151. 			help += HelpTableRow( linetemplate, "defaultindex", "The index of the preselected item in the list", "no", "no", "", html );
  152. 			help += separatorline;
  153.  
  154. 			help += HelpTableRow( linetemplate, "errors", Global.Common.Help.errors, "N/A", "YES", "", html );
  155. 			help += separatorline;
  156.  
  157. 			help += HelpTableRow( linetemplate, "fontfamily", Global.Common.Help.fontfamily, "no", "no", defaultfontfamily.Name, html );
  158. 			help += separatorline;
  159.  
  160. 			help += HelpTableRow( linetemplate, "fontsize", Global.Common.Help.fontsize, "no", "no", string.Format( "{0} ({1}..{2})", defaultfontsize, minimumfontsize, maximumfontsize ), html );
  161. 			help += separatorline;
  162.  
  163. 			help += HelpTableRow( linetemplate, "left", Global.Common.Help.left, "no", "no", "Centered (0..screenwidth-windowwidth)", html );
  164. 			help += separatorline;
  165.  
  166. 			if ( html == 1 )
  167. 			{
  168. 				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";
  169. 				help += string.Format( template, "list", "Semicolon-separated list of items", "* YES", "no", "* Set either \"list\" or \"listfile\" property" );
  170. 				template = "<tr>\n\t<td>{0}</td>\n\t<td>{1}</td>\n\t<td>{2}</td>\n</tr>\n";
  171. 				help += string.Format( template, "listfile", "Path to file containing list", "no" );
  172. 			}
  173. 			else
  174. 			{
  175. 				help += HelpTableRow( linetemplate, "list", "Semicolon-separated list of items", "", "no", "", html );
  176. 				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";
  177. 				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 );
  178. 			}
  179. 			help += separatorline;
  180.  
  181. 			help += HelpTableRow( linetemplate, "literal", Global.Common.Help.literal, "no", "no", "0: interpret \"\\t\" as tab and \"\\n\" as newline", html );
  182. 			help += separatorline;
  183.  
  184. 			help += HelpTableRow( linetemplate, "localizecaptions", Global.Common.Help.localizecaptions, "no", "no", "0: English captions", html );
  185. 			help += separatorline;
  186.  
  187. 			help += HelpTableRow( linetemplate, "modal", Global.Common.Help.modal, "no", "no", "1: always on top", html );
  188. 			help += separatorline;
  189.  
  190. 			help += HelpTableRow( linetemplate, "prompt", Global.Common.Help.prompt, "no", "no", "No prompt", html );
  191. 			help += separatorline;
  192.  
  193. 			help += HelpTableRow( linetemplate, "selectedindex", "Index of last selected radio button item", "N/A", "YES", "", html );
  194. 			help += separatorline;
  195.  
  196. 			help += HelpTableRow( linetemplate, "selecteditem", "Text of last selected radio button item", "N/A", "YES", "", html );
  197. 			help += separatorline;
  198.  
  199. 			help += HelpTableRow( linetemplate, "timeout", Global.Common.Help.timeout, "no", "no", "0: no timeout", html );
  200. 			help += separatorline;
  201.  
  202. 			help += HelpTableRow( linetemplate, "timeoutelapsed", Global.Common.Help.timeoutelapsed, "N/A", "YES", "", html );
  203. 			help += separatorline;
  204.  
  205. 			help += HelpTableRow( linetemplate, "title", Global.Common.Help.title, "no", "no", defaulttitle, html );
  206. 			help += separatorline;
  207.  
  208. 			help += HelpTableRow( linetemplate, "top", Global.Common.Help.top, "no", "no", "Centered (0..screenheight-windowheight)", html );
  209. 			help += separatorline;
  210.  
  211. 			help += HelpTableRow( linetemplate, "Version", Global.Common.Help.version, "", html );
  212. 			help += separatorline;
  213.  
  214. 			help += HelpTableRow( linetemplate, "windowheight", "Height of dialog window", "no", "no", string.Format( "{0} ({0}..screenheight)", defaultwindowheight ), html );
  215. 			help += separatorline;
  216.  
  217. 			help += HelpTableRow( linetemplate, "windowwidth", "Width of dialog window", "no", "no", string.Format( "{0} ({0}..screenwidth)", defaultwindowwidth ), html );
  218.  
  219. 			if ( html == 1 )
  220. 			{
  221. 				help += "</table>\n\n\n";
  222. 			}
  223. 			else
  224. 			{
  225. 				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";
  226. 			}
  227.  
  228. 			#endregion Properties
  229.  
  230.  
  231. 			#region Methods
  232.  
  233. 			if ( html == 1 )
  234. 			{
  235. 				separatorline = string.Empty;
  236. 				linetemplate = "<tr>\n\t<td>{0}</td>\n\t<td>{1}</td>\n\t<td>{2}</td>\n</tr>\n";
  237.  
  238. 				help += "<table>\n";
  239. 				help += "<tr>\n";
  240. 				help += "\t<th colspan=\"3\">Methods</th>\n";
  241. 				help += "</tr>\n";
  242.  
  243. 				help += "<tr>\n";
  244. 				help += "\t<th>Method Name</th>\n";
  245. 				help += "\t<th>Description</th>\n";
  246. 				help += "\t<th>Requirements</th>\n";
  247. 				help += "</tr>\n";
  248. 			}
  249. 			else
  250. 			{
  251. 				col1width = 21;
  252. 				col2width = 74;
  253. 				col3width = 39;
  254.  
  255. 				separatorline = "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u253C" + new string( '\u2500', 2 + col3width ) + "\u2524\n";
  256. 				linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502\n";
  257.  
  258. 				help += "\u250C" + new string( '\u2500', 8 + col1width + col2width + col3width ) + "\u2510\n";
  259. 				help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n";
  260. 				help += string.Format( "\u2502 {0,-" + ( 6 + col1width + col2width + col3width ).ToString( ) + "} \u2502\n", "Methods" );
  261. 				help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n";
  262. 				help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u2564" + new string( '\u2550', 2 + col2width ) + "\u2564" + new string( '\u2550', 2 + col3width ) + "\u2561\n";
  263. 				help += string.Format( linetemplate, "Method Name", "Description", "Requirements" );
  264. 				help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u256A" + new string( '\u2550', 2 + col2width ) + "\u256A" + new string( '\u2550', 2 + col3width ) + "\u2561\n";
  265. 			}
  266.  
  267. 			help += HelpTableRow( linetemplate, "CheckUpdate", Global.Common.Help._checkupdate, "", html );
  268. 			help += separatorline;
  269.  
  270. 			help += HelpTableRow( linetemplate, "Credits", Global.Common.Help._credits, "", html );
  271. 			help += separatorline;
  272.  
  273. 			help += HelpTableRow( linetemplate, "Help", Global.Common.Help._help, "", html );
  274. 			help += separatorline;
  275.  
  276. 			help += HelpTableRow( linetemplate, "ListProperties", Global.Common.Help._listproperties, "", html );
  277. 			help += separatorline;
  278.  
  279. 			help += HelpTableRow( linetemplate, "SampleCode", Global.Common.Help._samplecode, "", html );
  280. 			help += separatorline;
  281.  
  282. 			help += HelpTableRow( linetemplate, "Show", "Presents a dialog with a group of radio buttons based on the current property values, and saves the text for the selected item in the \"selecteditem\" property, and its index (in list) in the \"selectedindex\" property.", "", html );
  283.  
  284. 			if ( html == 1 )
  285. 			{
  286. 				help += "</table>\n\n\n";
  287. 			}
  288. 			else
  289. 			{
  290. 				help += "\u2514" + new string( '\u2500', 2 + col1width ) + "\u2534" + new string( '\u2500', 2 + col2width ) + "\u2534" + new string( '\u2500', 2 + col3width ) + "\u2518\n\n\n";
  291. 			}
  292.  
  293. 			#endregion Methods
  294.  
  295.  
  296. 			#region Example
  297.  
  298. 			if ( html == 1 )
  299. 			{
  300. 				help += "<h2>VBScript usage example:</h2>\n\n<pre>";
  301. 			}
  302. 			else
  303. 			{
  304. 				help += "VBScript usage example:\n";
  305. 				help += new string( '\u2500', 23 ) + "\n\n";
  306. 			}
  307.  
  308. 			help += SampleCode( );
  309.  
  310. 			if ( html == 1 )
  311. 			{
  312. 				help += "</pre>";
  313. 			}
  314.  
  315. 			help += "\n\n";
  316.  
  317. 			#endregion Example
  318.  
  319.  
  320. 			help = help.Replace( "\n", System.Environment.NewLine );
  321. 			return help;
  322. 		}
  323.  
  324.  
  325. 		private string HelpTableRow( string template, string col1text, string col2text, string col3text, int html )
  326. 		{
  327. 			if ( html == 1 )
  328. 			{
  329. 				return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text );
  330. 			}
  331. 			else
  332. 			{
  333. 				return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, "", "" );
  334. 			}
  335. 		}
  336.  
  337.  
  338. 		private string HelpTableRow( string template, string col1text, string col2text, string col3text, string col4text, string col5text, int html )
  339. 		{
  340. 			if ( html == 1 )
  341. 			{
  342. 				return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text, col4text, col5text );
  343. 			}
  344. 			else
  345. 			{
  346. 				return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, col4text, col5text );
  347. 			}
  348. 		}
  349.  
  350.  
  351. 		public string ListProperties( )
  352. 		{
  353. 			return Global.Common.Lists.Properties( this );
  354. 		}
  355.  
  356.  
  357. 		public string SampleCode( )
  358. 		{
  359. 			string code = "Set objRadioButtonBox = CreateObject( \"RobvanderWoude.RadioButtonBox\" )\n\n";
  360. 			code += "With objRadioButtonBox\n";
  361. 			code += "\t.list         = \"Arizona;Texas;California;Washington;Montana;Utah\"\n";
  362. 			code += "\t.defaultindex = 4\n";
  363. 			code += "\t.prompt       = \"You have 15 seconds to select a state\"\n";
  364. 			code += "\t.top          = 200\n";
  365. 			code += "\t.left         = 300\n";
  366. 			code += "\t.timeout      = 15\n";
  367. 			code += "\t.Show\n";
  368. 			code += "\tWScript.Echo \"Selected state: \" & .selecteditem\n";
  369. 			code += "End With\n\n";
  370. 			code += "Set objRadioButtonBox = Nothing\n\n";
  371. 			return code;
  372. 		}
  373.  
  374.  
  375. 		[System.STAThread]
  376. 		public void Show( )
  377. 		{
  378. 			#region Initial Sizes
  379.  
  380. 			int buttonheight = 25;
  381. 			int buttonwidth = 80;
  382. 			int horizontalmargin = 10;
  383. 			int promptheight = 20;
  384. 			int promptwidth = 0;
  385. 			double rbsafetyfactor = 1.05;
  386. 			int rbtextheight = 0; // radiobutton text height
  387. 			int rbtextwidth = 0; // radiobutton text width
  388. 			int rbwidth = 15; // radiobutton width without text
  389. 			int verticalmargin = 10;
  390.  
  391. 			#endregion Initial Sizes
  392.  
  393.  
  394. 			#region Timer
  395.  
  396. 			_timeoutelapsed = false;
  397. 			if ( timeout > 0 )
  398. 			{
  399. 				timer = new System.Timers.Timer( );
  400. 				timer.Elapsed += new System.Timers.ElapsedEventHandler( RadioButtonBox_Class_Timer_Elapsed );
  401. 				timer.Interval = _timeout;
  402. 				timer.Start( );
  403. 			}
  404.  
  405. 			#endregion Timer
  406.  
  407.  
  408. 			#region Main Form
  409.  
  410. 			radiobuttonboxform = new System.Windows.Forms.Form( );
  411.  
  412. 			radiobuttonboxform.Font = new System.Drawing.Font( _fontfamily, _fontsize, System.Drawing.FontStyle.Regular );
  413. 			radiobuttonboxform.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  414. 			radiobuttonboxform.MaximizeBox = false;
  415. 			radiobuttonboxform.MinimizeBox = false;
  416. 			if ( left < 0 || top < 0 )
  417. 			{
  418. 				radiobuttonboxform.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  419. 			}
  420. 			else
  421. 			{
  422. 				radiobuttonboxform.Location = new System.Drawing.Point( left, top );
  423. 				radiobuttonboxform.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
  424. 			}
  425. 			radiobuttonboxform.Text = title;
  426.  
  427. 			#endregion Main Form
  428.  
  429.  
  430. 			#region Prompt
  431.  
  432. 			System.Windows.Forms.Label labelPrompt = new System.Windows.Forms.Label( );
  433.  
  434. 			if ( string.IsNullOrWhiteSpace( prompt ) )
  435. 			{
  436. 				promptheight = -1 * verticalmargin;
  437. 			}
  438. 			else if ( _literal )
  439. 			{
  440. 				// leave prompt height unchanged
  441. 			}
  442. 			else
  443. 			{
  444. 				// Calculate required height for single prompt line
  445. 				promptheight = System.Windows.Forms.TextRenderer.MeasureText( "Test", labelPrompt.Font ).Height;
  446.  
  447. 				prompt = prompt.Replace( "\\n", "\n" ).Replace( "\\r", "\r" );
  448. 				labelPrompt.Text = prompt;
  449. 				string[] lines = prompt.Split( "\n".ToCharArray( ) );
  450. 				int promptlines = lines.Length;
  451.  
  452. 				foreach ( string line in lines )
  453. 				{
  454. 					promptwidth = System.Math.Max( promptwidth, System.Windows.Forms.TextRenderer.MeasureText( line, labelPrompt.Font ).Width );
  455. 				}
  456.  
  457. 				// Calculate required height for multiple line prompt
  458. 				promptheight = promptlines * promptheight;
  459. 			}
  460.  
  461. 			#endregion Prompt
  462.  
  463.  
  464. 			#region Radio Buttons
  465.  
  466. 			System.Collections.Generic.List<System.Windows.Forms.RadioButton> radiobuttons = new System.Collections.Generic.List<System.Windows.Forms.RadioButton>( );
  467. 			for ( int i = 0; i < _list.Count; i++ )
  468. 			{
  469. 				System.Windows.Forms.RadioButton radiobutton = new System.Windows.Forms.RadioButton( );
  470. 				radiobutton.Text = _list[i];
  471. 				radiobutton.Checked = ( defaultindex == i );
  472. 				rbtextwidth = System.Math.Max( rbtextwidth, System.Convert.ToInt32( System.Windows.Forms.TextRenderer.MeasureText( _list[i], radiobutton.Font ).Width * rbsafetyfactor ) );
  473. 				rbtextheight = System.Math.Max( rbtextheight, System.Convert.ToInt32( System.Windows.Forms.TextRenderer.MeasureText( _list[i], radiobutton.Font ).Height * rbsafetyfactor ) );
  474. 				radiobuttons.Add( radiobutton );
  475. 			}
  476.  
  477. 			#endregion Radio Buttons
  478.  
  479.  
  480. 			#region Buttons
  481.  
  482. 			System.Windows.Forms.Button okButton = new System.Windows.Forms.Button( );
  483. 			okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
  484. 			okButton.Name = "okButton";
  485. 			okButton.Text = captionok;
  486.  
  487. 			System.Windows.Forms.Button cancelButton = new System.Windows.Forms.Button( );
  488. 			cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  489. 			cancelButton.Name = "cancelButton";
  490. 			cancelButton.Text = captioncancel;
  491.  
  492. 			#endregion Buttons
  493.  
  494.  
  495. 			#region Calculate Window Layout
  496.  
  497. 			if ( rows > 0 ) // rows value is "leading"
  498. 			{
  499. 				rows = System.Math.Min( rows, _list.Count );
  500. 				columns = (int) System.Math.Floor( (decimal) ( _list.Count + rows - 1 ) / rows );
  501. 			}
  502. 			else if ( columns > 0 ) // columns value is "leading"
  503. 			{
  504. 				columns = System.Math.Min( columns, _list.Count );
  505. 				rows = (int) System.Math.Floor( (decimal) ( _list.Count + columns - 1 ) / columns );
  506. 			}
  507. 			else // single column
  508. 			{
  509. 				columns = 1;
  510. 				rows = _list.Count;
  511. 			}
  512.  
  513. 			int rbgroupwidth = 0;
  514. 			int rbgroupheight = 0;
  515. 			int rowheight = 0;
  516. 			int colwidth = 0;
  517.  
  518. 			if ( windowwidth != defaultwindowwidth )
  519. 			{
  520. 				rbgroupwidth = windowwidth - 2 * horizontalmargin;
  521. 				colwidth = (int) System.Math.Floor( (decimal) ( rbgroupwidth - horizontalmargin - columns * ( rbwidth + horizontalmargin ) ) / columns );
  522. 			}
  523. 			else
  524. 			{
  525. 				colwidth = rbtextwidth + rbwidth;
  526. 				rbgroupwidth = System.Math.Max( defaultwindowwidth - 2 * horizontalmargin, columns * ( colwidth + horizontalmargin ) + 2 * horizontalmargin );
  527. 				windowwidth = System.Math.Max( defaultwindowwidth, rbgroupwidth + 2 * horizontalmargin );
  528. 			}
  529.  
  530. 			if ( windowheight != defaultwindowheight )
  531. 			{
  532. 				rbgroupheight = windowheight - promptheight - buttonheight - 4 * verticalmargin;
  533. 				rowheight = (int) System.Math.Floor( (decimal) ( rbgroupheight - 2 * verticalmargin ) / rows );
  534. 			}
  535. 			else
  536. 			{
  537. 				rowheight = rbtextheight + verticalmargin;
  538. 				windowheight = System.Math.Max( defaultwindowheight, 6 * verticalmargin + promptheight + buttonheight + rows * rowheight );
  539. 				rbgroupheight = rows * rowheight + 2 * verticalmargin;
  540. 			}
  541.  
  542. 			#endregion Calculate Window Layout
  543.  
  544.  
  545. 			#region Check Available Group Box Space
  546.  
  547. 			if ( rbgroupheight / rows < rowheight || rbgroupwidth / columns < colwidth )
  548. 			{
  549. 				_errors.Add( timestamp + "Window size too small to display all radio buttons; increase window size, reduce or remove prompt, and/or change number of rows and columns" );
  550. 			}
  551.  
  552. 			#endregion Check Available Group Box Space
  553.  
  554.  
  555. 			#region Build Form
  556.  
  557. 			System.Drawing.Size windowsize = new System.Drawing.Size( windowwidth, windowheight );
  558. 			radiobuttonboxform.ClientSize = windowsize;
  559.  
  560. 			if ( !string.IsNullOrWhiteSpace( prompt ) )
  561. 			{
  562. 				labelPrompt.Size = new System.Drawing.Size( windowwidth - 2 * horizontalmargin, promptheight );
  563. 				labelPrompt.Location = new System.Drawing.Point( horizontalmargin, verticalmargin );
  564. 				radiobuttonboxform.Controls.Add( labelPrompt );
  565. 			}
  566.  
  567. 			groupbox = new System.Windows.Forms.GroupBox( );
  568. 			groupbox.Size = new System.Drawing.Size( rbgroupwidth, rbgroupheight );
  569. 			groupbox.Location = new System.Drawing.Point( horizontalmargin, promptheight + 2 * verticalmargin );
  570.  
  571. 			foreach ( System.Windows.Forms.RadioButton radiobutton in radiobuttons )
  572. 			{
  573. 				groupbox.Controls.Add( radiobutton );
  574. 			}
  575. 			radiobuttonboxform.Controls.Add( groupbox );
  576.  
  577. 			for ( int row = 0; row < rows; row++ )
  578. 			{
  579. 				for ( int column = 0; column < columns; column++ )
  580. 				{
  581. 					int index = row * columns + column;
  582. 					if ( index < radiobuttons.Count )
  583. 					{
  584. 						int x = System.Convert.ToInt32( horizontalmargin + column * ( colwidth + horizontalmargin ) );
  585. 						int y = System.Convert.ToInt32( verticalmargin + row * rowheight );
  586. 						radiobuttons[index].Size = new System.Drawing.Size( colwidth + horizontalmargin, rowheight );
  587. 						radiobuttons[index].Location = new System.Drawing.Point( x, y );
  588. 						radiobuttons[index].CheckedChanged += new System.EventHandler( RadioButtonBox_Class_CheckedChanged );
  589. 					}
  590. 				}
  591. 			}
  592.  
  593. 			okButton.Size = new System.Drawing.Size( buttonwidth, buttonheight );
  594. 			okButton.Location = new System.Drawing.Point( windowwidth / 2 - horizontalmargin - buttonwidth, windowheight - buttonheight - verticalmargin );
  595. 			radiobuttonboxform.Controls.Add( okButton );
  596.  
  597. 			cancelButton.Size = new System.Drawing.Size( buttonwidth, buttonheight );
  598. 			cancelButton.Location = new System.Drawing.Point( windowwidth / 2 + horizontalmargin, windowheight - buttonheight - verticalmargin );
  599. 			radiobuttonboxform.Controls.Add( cancelButton );
  600.  
  601. 			radiobuttonboxform.AcceptButton = okButton;  // OK on Enter
  602. 			radiobuttonboxform.CancelButton = cancelButton; // Cancel on Esc
  603.  
  604. 			#endregion Build Form
  605.  
  606.  
  607. 			#region Show Dialog
  608.  
  609. 			radiobuttonboxform.TopMost = topmost;
  610. 			System.Windows.Forms.DialogResult result = radiobuttonboxform.ShowDialog( );
  611. 			if ( !_timeoutelapsed && result == System.Windows.Forms.DialogResult.Cancel )
  612. 			{
  613. 				_selecteditem = string.Empty;
  614. 				_selectedindex = -1;
  615. 			}
  616.  
  617. 			#endregion Show Dialog
  618.  
  619. 		}
  620.  
  621. 		#endregion Methods
  622.  
  623.  
  624. 		#region Properties
  625.  
  626. 		private string _captioncancel = defaultcaptioncancel;
  627. 		[System.Runtime.InteropServices.ComVisible( true )]
  628. 		public string captioncancel
  629. 		{
  630. 			get
  631. 			{
  632. 				if ( _localizecaptions && _captioncancel == defaultcaptioncancel )
  633. 				{
  634. 					_captioncancel = Global.Common.Localization.Load( "user32.dll", 801, defaultcaptioncancel );
  635. 				}
  636. 				return _captioncancel;
  637. 			}
  638. 			set
  639. 			{
  640. 				if ( string.IsNullOrWhiteSpace( value ) || value.Length > 20 )
  641. 				{
  642. 					_captioncancel = defaultcaptioncancel;
  643. 				}
  644. 				else
  645. 				{
  646. 					_captioncancel = value;
  647. 				}
  648. 				//_debuginfo.Add()
  649. 			}
  650. 		}
  651.  
  652.  
  653. 		private string _captionok = defaultcaptionok;
  654. 		[System.Runtime.InteropServices.ComVisible( true )]
  655. 		public string captionok
  656. 		{
  657. 			get
  658. 			{
  659. 				if ( _localizecaptions && _captionok == defaultcaptionok )
  660. 				{
  661. 					_captionok = Global.Common.Localization.Load( "user32.dll", 800, defaultcaptionok );
  662. 				}
  663. 				return _captionok;
  664. 			}
  665. 			set
  666. 			{
  667. 				if ( string.IsNullOrWhiteSpace( value ) || value.Length > 20 )
  668. 				{
  669. 					_captionok = defaultcaptionok;
  670. 				}
  671. 				else
  672. 				{
  673. 					_captionok = value;
  674. 				}
  675. 			}
  676. 		}
  677.  
  678.  
  679. 		private int _columns = 0;
  680. 		[System.Runtime.InteropServices.ComVisible( true )]
  681. 		public int columns
  682. 		{
  683. 			get
  684. 			{
  685. 				return _columns;
  686. 			}
  687. 			set
  688. 			{
  689. 				_columns = value;
  690. 			}
  691. 		}
  692.  
  693.  
  694. 		private System.Collections.Generic.List<string> _debuginfo = new System.Collections.Generic.List<string>( );
  695. 		[System.Runtime.InteropServices.ComVisible( true )]
  696. 		public string debuginfo
  697. 		{
  698. 			get
  699. 			{
  700. 				return string.Join( "\n", _debuginfo.ToArray( ) );
  701. 			}
  702. 		}
  703.  
  704.  
  705. 		private int _defaultindex = 0;
  706. 		[System.Runtime.InteropServices.ComVisible( true )]
  707. 		public int defaultindex
  708. 		{
  709. 			get
  710. 			{
  711. 				return _defaultindex;
  712. 			}
  713. 			set
  714. 			{
  715. 				_defaultindex = value;
  716. 			}
  717. 		}
  718.  
  719.  
  720. 		private System.Collections.Generic.List<string> _errors = new System.Collections.Generic.List<string>( );
  721. 		[System.Runtime.InteropServices.ComVisible( true )]
  722. 		public string errors
  723. 		{
  724. 			get
  725. 			{
  726. 				return string.Join( "\n", _errors.ToArray( ) );
  727. 			}
  728. 		}
  729.  
  730.  
  731. 		private static bool _fixedpitchonly = false;
  732. 		[System.Runtime.InteropServices.ComVisible( true )]
  733. 		public int fixedpitchonly
  734. 		{
  735. 			get
  736. 			{
  737. 				return ( _fixedpitchonly ? 1 : 0 );
  738. 			}
  739. 			set
  740. 			{
  741. 				_fixedpitchonly = ( value == 1 );
  742. 				if ( _fixedpitchonly)
  743. 				{
  744. 					_fontfamily = System.Drawing.FontFamily.GenericMonospace;
  745. 				}
  746. 			}
  747. 		}
  748.  
  749.  
  750. 		private System.Drawing.FontFamily _fontfamily = defaultfontfamily;
  751. 		[System.Runtime.InteropServices.ComVisible( true )]
  752. 		public string fontfamily
  753. 		{
  754. 			get
  755. 			{
  756. 				return _fontfamily.Name;
  757. 			}
  758. 			set
  759. 			{
  760. 				_fontfamily = Global.Common.Validate.FontFamily( value, defaultfontfamily, _fixedpitchonly );
  761. 			}
  762. 		}
  763.  
  764.  
  765. 		private float _fontsize = defaultfontsize;
  766. 		[System.Runtime.InteropServices.ComVisible( true )]
  767. 		public int fontsize
  768. 		{
  769. 			get
  770. 			{
  771. 				return System.Convert.ToInt32( _fontsize );
  772. 			}
  773. 			set
  774. 			{
  775. 				_fontsize = System.Convert.ToSingle( value ).LimitToRange( minimumfontsize, maximumfontsize );
  776. 			}
  777. 		}
  778.  
  779.  
  780. 		private int _left = -1;
  781. 		[System.Runtime.InteropServices.ComVisible( true )]
  782. 		public int left
  783. 		{
  784. 			get
  785. 			{
  786. 				return _left;
  787. 			}
  788. 			set
  789. 			{
  790. 				if ( value < 0 )
  791. 				{
  792. 					_left = 0;
  793. 				}
  794. 				else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - windowwidth )
  795. 				{
  796. 					_left = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - windowwidth;
  797. 				}
  798. 				else
  799. 				{
  800. 					_left = value;
  801. 				}
  802. 			}
  803. 		}
  804.  
  805.  
  806. 		private System.Collections.Generic.List<string> _list = new System.Collections.Generic.List<string>( );
  807. 		[System.Runtime.InteropServices.ComVisible( true )]
  808. 		public string list
  809. 		{
  810. 			get
  811. 			{
  812. 				return string.Join( ";", _list );
  813. 			}
  814. 			set
  815. 			{
  816. 				_list = value.Split( ";\n".ToCharArray( ), System.StringSplitOptions.RemoveEmptyEntries ).Select( i => i.Trim( ) ).ToList<string>( );
  817. 			}
  818. 		}
  819. 		[System.Runtime.InteropServices.ComVisible( true )]
  820. 		public string listfile
  821. 		{
  822. 			get
  823. 			{
  824. 				return string.Join( ";", _list );
  825. 			}
  826. 			set
  827. 			{
  828. 				if ( System.IO.File.Exists( value ) )
  829. 				{
  830. 					try
  831. 					{
  832. 						System.IO.StreamReader sr = new System.IO.StreamReader( list );
  833. 						string text = sr.ReadToEnd( );
  834. 						sr.Close( );
  835. 						if ( text.IndexOfAny( ";\n\r".ToCharArray( ) ) == -1 )
  836. 						{
  837. 							_errors.Add( timestamp + "Invalid list file content" );
  838. 						}
  839. 						_list = text.Split( ";\n\r".ToCharArray( ), System.StringSplitOptions.RemoveEmptyEntries ).Select( i => i.Trim( ) ).ToList<string>( );
  840. 					}
  841. 					catch ( System.Exception e )
  842. 					{
  843. 						_errors.Add( timestamp + "Unable to open list file: " + e.Message );
  844. 					}
  845.  
  846. 				}
  847. 				else
  848. 				{
  849. 					_errors.Add( timestamp + "List file not found" );
  850. 				}
  851. 			}
  852. 		}
  853.  
  854.  
  855. 		private bool _literal = false;
  856. 		[System.Runtime.InteropServices.ComVisible( true )]
  857. 		public int literal
  858. 		{
  859. 			get
  860. 			{
  861. 				return ( _literal ? 1 : 0 );
  862. 			}
  863. 			set
  864. 			{
  865. 				_literal = ( value == 1 );
  866. 			}
  867. 		}
  868.  
  869.  
  870. 		private bool _localizecaptions = false;
  871. 		[System.Runtime.InteropServices.ComVisible( true )]
  872. 		public int localizecaptions
  873. 		{
  874. 			get
  875. 			{
  876. 				return ( _localizecaptions ? 1 : 0 );
  877. 			}
  878. 			set
  879. 			{
  880. 				_localizecaptions = ( value == 1 );
  881. 			}
  882. 		}
  883.  
  884.  
  885. 		private bool topmost = true;
  886. 		[System.Runtime.InteropServices.ComVisible( true )]
  887. 		public int modal
  888. 		{
  889. 			get
  890. 			{
  891. 				return ( topmost ? 1 : 0 );
  892. 			}
  893. 			set
  894. 			{
  895. 				topmost = ( value == 1 );
  896. 			}
  897. 		}
  898.  
  899.  
  900. 		private string _prompt = string.Empty;
  901. 		[System.Runtime.InteropServices.ComVisible( true )]
  902. 		public string prompt
  903. 		{
  904. 			get
  905. 			{
  906. 				return _prompt;
  907. 			}
  908. 			set
  909. 			{
  910. 				if ( _literal )
  911. 				{
  912. 					_prompt = value.Trim( );
  913. 				}
  914. 				else
  915. 				{
  916. 					_prompt = value.Replace( "\\n", "\n" ).Replace( "\\t", "\t" ).Trim( );
  917. 				}
  918. 			}
  919. 		}
  920.  
  921.  
  922. 		private int _rows = 0;
  923. 		[System.Runtime.InteropServices.ComVisible( true )]
  924. 		public int rows
  925. 		{
  926. 			get
  927. 			{
  928. 				return _rows;
  929. 			}
  930. 			set
  931. 			{
  932. 				_rows = value;
  933. 			}
  934. 		}
  935.  
  936.  
  937. 		private int _selectedindex = -1;
  938. 		[System.Runtime.InteropServices.ComVisible( true )]
  939. 		public int selectedindex
  940. 		{
  941. 			get
  942. 			{
  943. 				return _selectedindex;
  944. 			}
  945. 		}
  946.  
  947.  
  948. 		private string _selecteditem = string.Empty;
  949. 		[System.Runtime.InteropServices.ComVisible( true )]
  950. 		public string selecteditem
  951. 		{
  952. 			get
  953. 			{
  954. 				return _selecteditem;
  955. 			}
  956. 		}
  957.  
  958.  
  959. 		private int _timeout = 0;
  960. 		[System.Runtime.InteropServices.ComVisible( true )]
  961. 		public int timeout
  962. 		{
  963. 			get
  964. 			{
  965. 				return (int) ( _timeout / 1000 );
  966. 			}
  967. 			set
  968. 			{
  969. 				if ( value < 0 || value > 3600 )
  970. 				{
  971. 					_timeout = 0;
  972. 				}
  973. 				else
  974. 				{
  975. 					_timeout = value * 1000;
  976. 				}
  977. 			}
  978. 		}
  979.  
  980.  
  981. 		private bool _timeoutelapsed = false;
  982. 		[System.Runtime.InteropServices.ComVisible( true )]
  983. 		public int timeoutelapsed
  984. 		{
  985. 			get
  986. 			{
  987. 				return ( _timeoutelapsed ? 1 : 0 );
  988. 			}
  989. 		}
  990.  
  991.  
  992. 		private static string timestamp => Global.Common.TimeStamp( );
  993.  
  994.  
  995. 		private string _title = defaulttitle;
  996. 		[System.Runtime.InteropServices.ComVisible( true )]
  997. 		public string title
  998. 		{
  999. 			get
  1000. 			{
  1001. 				return _title;
  1002. 			}
  1003. 			set
  1004. 			{
  1005. 				if ( string.IsNullOrWhiteSpace( value ) )
  1006. 				{
  1007. 					_title = defaulttitle;
  1008. 				}
  1009. 				else
  1010. 				{
  1011. 					_title = value.Trim( );
  1012. 				}
  1013. 			}
  1014. 		}
  1015.  
  1016.  
  1017. 		private int _top = -1;
  1018. 		[System.Runtime.InteropServices.ComVisible( true )]
  1019. 		public int top
  1020. 		{
  1021. 			get
  1022. 			{
  1023. 				return _top;
  1024. 			}
  1025. 			set
  1026. 			{
  1027. 				if ( value < 0 )
  1028. 				{
  1029. 					_top = 0;
  1030. 				}
  1031. 				else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - windowheight )
  1032. 				{
  1033. 					_top = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - windowheight;
  1034. 				}
  1035. 				else
  1036. 				{
  1037. 					_top = value;
  1038. 				}
  1039. 			}
  1040. 		}
  1041.  
  1042.  
  1043. 		[System.Runtime.InteropServices.ComVisible( true )]
  1044. 		public string Version
  1045. 		{
  1046. 			get
  1047. 			{
  1048. 				return Global.Common.ProgramInfo.FileVersion;
  1049. 			}
  1050. 		}
  1051.  
  1052.  
  1053. 		private int _windowheight = defaultwindowheight;
  1054. 		[System.Runtime.InteropServices.ComVisible( true )]
  1055. 		public int windowheight
  1056. 		{
  1057. 			get
  1058. 			{
  1059. 				return _windowheight;
  1060. 			}
  1061. 			set
  1062. 			{
  1063. 				if ( value < defaultwindowheight )
  1064. 				{
  1065. 					_windowheight = defaultwindowheight;
  1066. 				}
  1067. 				else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height )
  1068. 				{
  1069. 					_windowheight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
  1070. 				}
  1071. 				else
  1072. 				{
  1073. 					_windowheight = value;
  1074. 				}
  1075. 			}
  1076. 		}
  1077.  
  1078.  
  1079. 		private int _windowwidth = defaultwindowwidth;
  1080. 		[System.Runtime.InteropServices.ComVisible( true )]
  1081. 		public int windowwidth
  1082. 		{
  1083. 			get
  1084. 			{
  1085. 				return _windowwidth;
  1086. 			}
  1087. 			set
  1088. 			{
  1089. 				if ( value < defaultwindowwidth )
  1090. 				{
  1091. 					_windowwidth = defaultwindowwidth;
  1092. 				}
  1093. 				else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width )
  1094. 				{
  1095. 					_windowwidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
  1096. 				}
  1097. 				else
  1098. 				{
  1099. 					_windowwidth = value;
  1100. 				}
  1101. 			}
  1102. 		}
  1103.  
  1104. 		#endregion Properties
  1105.  
  1106.  
  1107. 		#region Event Handlers
  1108.  
  1109. 		private void RadioButtonBox_Class_CheckedChanged( object sender, System.EventArgs e )
  1110. 		{
  1111. 			foreach ( System.Windows.Forms.RadioButton rb in groupbox.Controls )
  1112. 			{
  1113. 				if ( rb.Checked )
  1114. 				{
  1115. 					_selecteditem = rb.Text;
  1116. 					_selectedindex = _list.IndexOf( selecteditem );
  1117. 					break;
  1118. 				}
  1119. 			}
  1120. 		}
  1121.  
  1122.  
  1123. 		private void RadioButtonBox_Class_Timer_Elapsed( object sender, System.Timers.ElapsedEventArgs e )
  1124. 		{
  1125. 			_timeoutelapsed = true;
  1126. 			foreach ( System.Windows.Forms.RadioButton rb in groupbox.Controls )
  1127. 			{
  1128. 				if ( rb.Checked )
  1129. 				{
  1130. 					_selecteditem = rb.Text;
  1131. 					_selectedindex = _list.IndexOf( selecteditem );
  1132. 					break;
  1133. 				}
  1134. 			}
  1135. 			radiobuttonboxform.Close( );
  1136. 		}
  1137.  
  1138. 		#endregion Event Handlers
  1139. 	}
  1140. }
  1141.  

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