using System.Linq; namespace RobvanderWoude { [System.Runtime.InteropServices.Guid( "BC45108A-B13D-4D27-87C5-4E9A759107C2" )] public interface DateTimeBox_Interface { [System.Runtime.InteropServices.DispId( 1 )] void CheckUpdate( ); [System.Runtime.InteropServices.DispId( 2 )] string Credits( ); [System.Runtime.InteropServices.DispId( 3 )] string Help( int html = 0 ); [System.Runtime.InteropServices.DispId( 4 )] string ListProperties( ); [System.Runtime.InteropServices.DispId( 5 )] string SampleCode( ); [System.Runtime.InteropServices.DispId( 6 )] void Show( ); } [System.Runtime.InteropServices.Guid( "1D9EEC0E-41BE-4190-8ADF-F310D081F099" ), System.Runtime.InteropServices.InterfaceType( System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch )] public interface DateTimeBox_Events { } [System.Runtime.InteropServices.Guid( "13BFE67A-D92B-4AA9-A9DE-D7A0C109F12E" ), System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.AutoDual ), System.Runtime.InteropServices.ComSourceInterfaces( typeof( DateTimeBox_Events ) )] public class DateTimeBox : DateTimeBox_Interface { #region Default Values const string defaultcaptioncancel = "Cancel"; const string defaultcaptionok = "OK"; const string defaultculture = "fr-CA"; // yyyy-MM-dd HH:mm:ss static readonly string defaulttitle = string.Format( "{0}, Version {1}", Global.Common.ProgramInfo.FileName, Global.Common.ProgramInfo.FileVersion ); const int defaultwindowheight = 135; const int defaultwindowwidth = 220; const float minimumfontsize = 6; const float maximumfontsize = 48; static readonly System.Drawing.FontFamily defaultfontfamily = System.Drawing.FontFamily.GenericSansSerif; static readonly float defaultfontsize = 10; #endregion Default Values static System.Windows.Forms.Form datetimeboxform = null; static System.Windows.Forms.DateTimePicker datepicker = null; static System.Windows.Forms.DateTimePicker timepicker = null; static bool cancelled = false; #region Methods public void CheckUpdate( ) { Global.Common.ProgramInfo.CheckUpdate( ); } public string Credits( ) { return Global.Common.Credits( ); } static string GetAMDesignator( string culture ) { return System.Globalization.CultureInfo.GetCultureInfo( culture, defaultculture ).DateTimeFormat.AMDesignator; } public static int GetIso8601WeekOfYear( System.DateTime date ) { return Global.Common.GetIso8601WeekOfYear( date ); } static string GetPMDesignator( string culture ) { return System.Globalization.CultureInfo.GetCultureInfo( culture, defaultculture ).DateTimeFormat.PMDesignator; } public string Help( int html = 0 ) { int col1width = 0; int col2width = 0; int col3width = 0; int col4width = 0; int col5width = 0; string help = string.Empty; string linetemplate = string.Empty; string separatorline = string.Empty; if ( html == 1 ) { help += "

Help for DateTimeBox class

\n\n"; help += "

Present a dialog with date and/or time picker, and return the selected date and/or time values

\n\n"; help += "

COM ProgID: RobvanderWoude.DateTimeBox

\n\n"; } else { help += "Help for DateTimeBox class\n"; help += new string( '\u2500', help.Length ) + "\n\n"; help += "Present a dialog with date and/or time picker, and return the selected date and/or time values\n\n\n"; help += "COM ProgID: RobvanderWoude.DateTimeBox\n\n\n"; } #region Properties if ( html == 1 ) { separatorline = string.Empty; linetemplate = "\n\t{0}\n\t{1}\n\t{2}\n\t{3}\n\t{4}\n\n"; help += "\n"; help += "\n"; help += "\t\n"; help += "\n"; help += "\n"; help += "\t\n"; help += "\t\n"; help += "\t\n"; help += "\t\n"; help += "\t\n"; help += "\n"; } else { col1width = 16; col2width = 50; col3width = 9; col4width = 9; col5width = 44; 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"; linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502 {3,-" + col4width.ToString( ) + "} \u2502 {4,-" + col5width.ToString( ) + "} \u2502\n"; help += "\u250C" + new string( '\u2500', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2510\n"; help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n"; 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)" ); help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n"; 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"; help += string.Format( linetemplate, "Property Name", "Description", "Mandatory", "Read-Only", "Default (Allowed) Values" ); 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"; } help += HelpTableRow( linetemplate, "captioncancel", Global.Common.Help.captioncancel, "no", "no", defaultcaptioncancel, html ); help += separatorline; help += HelpTableRow( linetemplate, "captionok", Global.Common.Help.captionok, "no", "no", defaultcaptionok, html ); help += separatorline; help += HelpTableRow( linetemplate, "culturegui", "The \"culture\" used to format date/time in the GUI", "no", "no", defaultculture, html ); help += separatorline; help += HelpTableRow( linetemplate, "culturein", "The \"culture\" used to format initial date/time", "no", "no", defaultculture, html ); help += separatorline; help += HelpTableRow( linetemplate, "cultureout", "The \"culture\" used to format output date/time", "no", "no", defaultculture, html ); help += separatorline; help += HelpTableRow( linetemplate, "dateformatgui", "Date format for GUI, overrides culture", "no", "no", "short date format for culturegui", html ); help += separatorline; help += HelpTableRow( linetemplate, "dateformatin", "Date format for initial date, overrides culture", "no", "no", "short date format for culturein", html ); help += separatorline; help += HelpTableRow( linetemplate, "dateformatout", "Date format for output, overrides culture", "no", "no", "short date format for cultureout", html ); help += separatorline; help += HelpTableRow( linetemplate, "dateonly", "Date picker only, no time picker (0=false, 1=true)", "no", "no", "0: show time picker", html ); help += separatorline; help += HelpTableRow( linetemplate, "dateseparatorgui", "Date separator used in the GUI", "no", "no", "determined by culturegui", html ); help += separatorline; help += HelpTableRow( linetemplate, "dateseparatorgui", "Date separator used in initial date", "no", "no", "determined by culturein", html ); help += separatorline; help += HelpTableRow( linetemplate, "dateseparatorgui", "Date separator used in output", "no", "no", "determined by cultureout", html ); help += separatorline; help += HelpTableRow( linetemplate, "debuginfo", Global.Common.Help.debuginfo, "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "earliestdate", "Earliest date allowed", "no", "no", string.Format( "{0} ({1} .. {1})", System.DateTime.MinValue, System.DateTime.MaxValue ), html ); help += separatorline; help += HelpTableRow( linetemplate, "errors", Global.Common.Help.errors, "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "fontfamily", Global.Common.Help.fontfamily, "no", "no", defaultfontfamily.Name, html ); help += separatorline; help += HelpTableRow( linetemplate, "fontsize", Global.Common.Help.fontsize, "no", "no", string.Format( "{0} ({1}..{2})", defaultfontsize, minimumfontsize, maximumfontsize ), html ); help += separatorline; help += HelpTableRow( linetemplate, "initialdatetime", "Initial date/time for date/time pickers", "no", "no", "current date/time", html ); help += separatorline; help += HelpTableRow( linetemplate, "latestdate", "Latest date allowed", "no", "no", string.Format( "{1} ({0} .. {1})", System.DateTime.MinValue, System.DateTime.MaxValue ), html ); help += separatorline; help += HelpTableRow( linetemplate, "left", Global.Common.Help.left, "no", "no", "Centered (0..screenwidth - windowwidth)", html ); help += separatorline; help += HelpTableRow( linetemplate, "literal", Global.Common.Help.literal, "no", "no", "0: interpret \"\\t\" as tab and \"\\n\" as newline", html ); help += separatorline; help += HelpTableRow( linetemplate, "localizecaptions", Global.Common.Help.localizecaptions, "no", "no", "0: English captions", html ); help += separatorline; help += HelpTableRow( linetemplate, "maxdaysback", "Maximum number of days back from initial date", "no", "no", "-1 (you can set either earliestdate or maxdaysback, but not both)", html ); help += separatorline; help += HelpTableRow( linetemplate, "maxdaysforward", "Maximum number of days forward from initial date", "no", "no", "-1 (you can set either latestdate or maxdaysback, but not both)", html ); help += separatorline; help += HelpTableRow( linetemplate, "modal", Global.Common.Help.modal, "no", "no", "1: always on top", html ); help += separatorline; help += HelpTableRow( linetemplate, "prompt", Global.Common.Help.prompt, "no", "no", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "selecteddate", "Date selected in date picker", "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "selectedtime", "Time selected in time picker", "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "selectedweek", "Week of year for selected date", "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "selectedweekday", "Day of week for selected date", "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeformatgui", "Time format for GUI, overrides culture", "no", "no", "long time format for culturegui", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeformatin", "Time format for initial time, overrides culture", "no", "no", "long time format for culturein", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeformatout", "Time format for output, overrides culture", "no", "no", "long time format for cultureout", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeonly", "Time picker only, no date picker (0=false, 1=true)", "no", "no", "0: show date picker", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeout", Global.Common.Help.timeout, "no", "no", "0: no timeout", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeoutelapsed", Global.Common.Help.timeoutelapsed, "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeformatgui", "Time format for GUI, overrides culture", "no", "no", "long time format for culturegui", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeformatin", "Time format for initial date, overrides culture", "no", "no", "long time format for culturein", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeformatout", "Time format for output, overrides culture", "no", "no", "long time format for cultureout", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeseparatorgui", "Time separator used in the GUI", "no", "no", "determined by culturegui", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeseparatorgui", "Time separator used in initial date", "no", "no", "determined by culturein", html ); help += separatorline; help += HelpTableRow( linetemplate, "timeseparatorgui", "Time separator used in output", "no", "no", "determined by cultureout", html ); help += separatorline; help += HelpTableRow( linetemplate, "title", Global.Common.Help.title, "no", "no", defaulttitle, html ); help += separatorline; help += HelpTableRow( linetemplate, "top", Global.Common.Help.top, "no", "no", "Centered (0..screenheight - windowheight)", html ); help += separatorline; help += HelpTableRow( linetemplate, "Version", Global.Common.Help.version, "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "windowheight", Global.Common.Help.windowheight, "no", "no", string.Format( "{0} ({0}..{1})", defaultwindowheight, "screenheight" ), html ); help += separatorline; help += HelpTableRow( linetemplate, "windowwidth", Global.Common.Help.windowwidth, "no", "no", string.Format( "{0} ({0}..{1})", defaultwindowwidth, "screenwidth" ), html ); if ( html == 1 ) { help += "
Properties (Note that all properties are string or integer, no booleans, objects or arrays)
Property NameDescriptionMandatoryRead-OnlyDefault (Allowed) Values
\n\n\n"; } else { 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"; } #endregion Properties #region Methods if ( html == 1 ) { separatorline = string.Empty; linetemplate = "\n\t{0}\n\t{1}\n\t{2}\n\n"; help += "\n"; help += "\n"; help += "\t\n"; help += "\n"; help += "\n"; help += "\t\n"; help += "\t\n"; help += "\t\n"; help += "\n"; } else { col1width = 16; col2width = 74; col3width = 44; separatorline = "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u253C" + new string( '\u2500', 2 + col3width ) + "\u2524\n"; linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502\n"; help += "\u250C" + new string( '\u2500', 8 + col1width + col2width + col3width ) + "\u2510\n"; help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n"; help += string.Format( "\u2502 {0,-" + ( 6 + col1width + col2width + col3width ).ToString( ) + "} \u2502\n", "Methods" ); help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n"; help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u2564" + new string( '\u2550', 2 + col2width ) + "\u2564" + new string( '\u2550', 2 + col3width ) + "\u2561\n"; help += string.Format( linetemplate, "Method Name", "Description", "Requirements" ); help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u256A" + new string( '\u2550', 2 + col2width ) + "\u256A" + new string( '\u2550', 2 + col3width ) + "\u2561\n"; } help += HelpTableRow( linetemplate, "CheckUpdate", Global.Common.Help._checkupdate, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "Credits", Global.Common.Help._credits, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "Help", Global.Common.Help._help, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "ListProperties", Global.Common.Help._listproperties, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "SampleCode", Global.Common.Help._samplecode, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "Show", "Presents a date/time picker dialog based on the current property values, and if \"OK\" is clicked, saves the last selected date and time in the \"selecteddate\" and \"selectedtime\" properties (if \"Cancel\" is clicked, \"selecteddate\" and \"selectedtime\" are set to empty strings).", "", html ); if ( html == 1 ) { help += "
Methods
Method NameDescriptionRequirements
\n\n\n"; } else { help += "\u2514" + new string( '\u2500', 2 + col1width ) + "\u2534" + new string( '\u2500', 2 + col2width ) + "\u2534" + new string( '\u2500', 2 + col3width ) + "\u2518\n\n\n"; } #endregion Methods #region Notes if ( html == 1 ) { help += "\n\n\t\n\t\n\n
"; } help += "Notes:"; if ( html == 1 ) { help += ""; } else { help += "\n\t"; } help += "If the current culture of the local computer does not use AM/PM designators, no AM/PM"; if ( html == 1 ) { help += " "; } else { help += "\n\t"; } help += "designators can be shown in the date and Time Pickers, regardless of selected GUI culture."; if ( html == 1 ) { help += "
"; } help += "\n\n\n"; #endregion Notes #region Example if ( html == 1 ) { help += "

VBScript usage example:

\n\n
";
			}
			else
			{
				help += "VBScript usage example:\n";
				help += new string( '\u2500', 23 ) + "\n\n";
			}

			help += SampleCode( );

			if ( html == 1 )
			{
				help += "
"; } help += "\n\n"; #endregion Example help = help.Replace( "\n", System.Environment.NewLine ); return help; } private string HelpTableRow( string template, string col1text, string col2text, string col3text, int html ) { if ( html == 1 ) { return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text ); } else { return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, "", "" ); } } private string HelpTableRow( string template, string col1text, string col2text, string col3text, string col4text, string col5text, int html ) { if ( html == 1 ) { return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text, col4text, col5text ); } else { return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, col4text, col5text ); } } public string ListProperties( ) { return Global.Common.Lists.Properties( this ); } public string SampleCode( ) { string code = "Set objDateTimeBox = CreateObject( \"RobvanderWoude.DateTimeBox\" )\n\n"; code += "With objDateTimeBox\n"; code += "\t.top = 200\n"; code += "\t.left = 300\n"; code += "\t.timeout = 15\n"; code += "\t.title = \"Pick a Date and Time\"\n"; code += "\t.Show\n"; code += "\tWScript.Echo \"Selected date and time: \" & .selecteddate & \" \" & .selectedtime\n"; code += "\tWScript.Echo\n"; code += "\tWScript.Echo .ListProperties( )\n"; code += "End With\n\n"; code += "Set objDateTimeBox = Nothing"; return code; } [System.STAThread] public void Show( ) { _selecteddate = _initialdatetime.Date; _selectedtime = _initialdatetime.TimeOfDay; #region Timer try { _timeoutelapsed = false; if ( _timeout > 0 ) { System.Timers.Timer timer = new System.Timers.Timer( ); timer.Elapsed += new System.Timers.ElapsedEventHandler( DateTimeBox_Timer_Elapsed ); timer.Interval = timeout * 1000; timer.Start( ); _debuginfo.Add( timestamp + "Timer started with interval " + timeout.ToString( ) ); } } catch ( System.Exception e ) { _errors.Add( Global.Common.TimeStamp( ) + "Timer Error: " + e.Message ); } #endregion Timer #region Form Controls int rows = 2; datetimeboxform = new System.Windows.Forms.Form( ); if ( windowheight == defaultwindowheight ) { if ( _dateonly || _timeonly ) { rows -= 1; } if ( !string.IsNullOrWhiteSpace( prompt ) ) { rows += 1; } } if ( rows != 2 ) { windowheight += ( rows - 2 ) * 40; } _debuginfo.Add( timestamp + "Form rows: " + rows ); _debuginfo.Add( timestamp + "Window height: " + windowheight ); System.Drawing.Size size = new System.Drawing.Size( windowwidth, windowheight ); datetimeboxform.ClientSize = size; datetimeboxform.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; datetimeboxform.MaximizeBox = false; datetimeboxform.MinimizeBox = false; datetimeboxform.Font = new System.Drawing.Font( fontfamily, fontsize ); if ( left < 0 || top < 0 ) { datetimeboxform.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; } else { datetimeboxform.StartPosition = System.Windows.Forms.FormStartPosition.Manual; datetimeboxform.Location = new System.Drawing.Point( left, top ); } datetimeboxform.Text = title; datetimeboxform.TopLevel = topmost; System.Drawing.Point firstrow = new System.Drawing.Point( 15, 15 ); System.Drawing.Point secondrow = new System.Drawing.Point( 15, 55 ); System.Drawing.Point thirdrow = new System.Drawing.Point( 15, 95 ); if ( !string.IsNullOrWhiteSpace( prompt ) ) { System.Windows.Forms.Label labelprompt = new System.Windows.Forms.Label( ); labelprompt.Location = firstrow; if ( !_literal ) { labelprompt.Text = prompt.Replace( "\\n", "\n" ).Replace( "\\r", "\r" ); } //datetimeboxform.ClientSize = size; labelprompt.Size = new System.Drawing.Size( size.Width - 30, 40 ); datetimeboxform.Controls.Add( labelprompt ); } _debuginfo.Add( timestamp + $"startposition = {datetimeboxform.StartPosition.ToString( )}; top = {top}; left = {left}; location = {datetimeboxform.Location.X},{datetimeboxform.Location.Y}" ); if ( !_timeonly ) { datepicker = new System.Windows.Forms.DateTimePicker( ); datepicker.CustomFormat = System.Globalization.CultureInfo.GetCultureInfo( _culturegui, defaultculture ).DateTimeFormat.LongDatePattern; datepicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom; if ( string.IsNullOrWhiteSpace( prompt ) ) { datepicker.Location = firstrow; } else { datepicker.Location = secondrow; } datepicker.MaxDate = _latestdate; datepicker.MinDate = _earliestdate; datepicker.Size = new System.Drawing.Size( datetimeboxform.Width - 30, 25 ); datepicker.Value = _initialdatetime; datepicker.TextChanged += new System.EventHandler( DateTimeBox_DatePicker_TextChanged ); datetimeboxform.Controls.Add( datepicker ); } if ( !_dateonly ) { timepicker = new System.Windows.Forms.DateTimePicker( ); switch ( rows ) { case 1: timepicker.Location = firstrow; break; case 2: timepicker.Location = secondrow; break; case 3: timepicker.Location = thirdrow; break; } timepicker.ShowUpDown = true; timepicker.Size = new System.Drawing.Size( datetimeboxform.Width - 30, 25 ); timepicker.Value = _initialdatetime; _debuginfo.Add( timestamp + "GUI culture: " + _culturegui ); _debuginfo.Add( timestamp + "GUI time format: " + timeformatgui ); _debuginfo.Add( timestamp + "GUI AM designator: " + GetAMDesignator( culturegui ) ); _debuginfo.Add( timestamp + "GUI PM designator: " + GetPMDesignator( culturegui ) ); _debuginfo.Add( timestamp + "Local culture: " + System.Globalization.CultureInfo.CurrentCulture.Name ); _debuginfo.Add( timestamp + "Local time format: " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern ); _debuginfo.Add( timestamp + "Local AM designator: " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.AMDesignator ); _debuginfo.Add( timestamp + "Local PM designator: " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.PMDesignator ); if ( string.IsNullOrWhiteSpace( System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.AMDesignator ) && !string.IsNullOrWhiteSpace( GetAMDesignator( culturegui ) ) ) { _errors.Add( Global.Common.TimeStamp( ) + "The local system is unable to display AM/PM designators in TimePicker GUI" ); _debuginfo.Add( timestamp + "Though AM/PM designators in output will be displayed correctly, the local system is unable to display AM/PM designators in TimePicker GUI" ); } timepicker.CustomFormat = System.Globalization.CultureInfo.GetCultureInfo( _culturegui, defaultculture ).DateTimeFormat.LongTimePattern; timepicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom; timepicker.TextChanged += DateTimeBox_TimePicker_TextChanged; datetimeboxform.Controls.Add( timepicker ); } System.Windows.Forms.Button okButton = new System.Windows.Forms.Button( ); okButton.DialogResult = System.Windows.Forms.DialogResult.OK; okButton.Name = "okButton"; okButton.Size = new System.Drawing.Size( 80, 25 ); okButton.Text = captionok; okButton.Location = new System.Drawing.Point( datetimeboxform.Width / 2 - 10 - 80, datetimeboxform.Height - 68 ); datetimeboxform.Controls.Add( okButton ); System.Windows.Forms.Button cancelButton = new System.Windows.Forms.Button( ); cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; cancelButton.Name = "cancelButton"; cancelButton.Size = new System.Drawing.Size( 80, 25 ); cancelButton.Text = captioncancel; cancelButton.Location = new System.Drawing.Point( datetimeboxform.Width / 2 + 10, datetimeboxform.Height - 68 ); datetimeboxform.Controls.Add( cancelButton ); datetimeboxform.AcceptButton = okButton; // OK on Enter datetimeboxform.CancelButton = cancelButton; // Cancel on Esc datetimeboxform.Activate( ); #endregion Form Controls System.Windows.Forms.DialogResult result = datetimeboxform.ShowDialog( ); if ( !_timeoutelapsed ) { _debuginfo.Add( timestamp + "DateTimeBox dialog result: " + result.ToString( ) ); } if ( !_timeoutelapsed && result == System.Windows.Forms.DialogResult.Cancel ) { cancelled = true; _selecteddate = System.DateTime.MinValue.Date; _selectedtime = System.DateTime.MinValue.TimeOfDay; } else { if ( !_timeonly ) { try { //_selecteddate = datepicker.Value.Date; _debuginfo.Add( timestamp + "selecteddate: " + selecteddate ); string date = _selecteddate.ToString( System.Globalization.CultureInfo.GetCultureInfo( cultureout, defaultculture ).DateTimeFormat.ShortDatePattern ); _debuginfo.Add( timestamp + "selecteddate: " + date ); } catch ( System.Exception e ) { _errors.Add( Global.Common.TimeStamp( ) + "selecteddate: " + e.Message ); } } if ( !_dateonly ) { try { //_selectedtime = timepicker.Value.TimeOfDay; _debuginfo.Add( timestamp + "selectedtime: " + selectedtime ); string time = ( _selecteddate.Date + _selectedtime ).ToString( System.Globalization.CultureInfo.GetCultureInfo( cultureout, defaultculture ).DateTimeFormat.ShortTimePattern ); _debuginfo.Add( timestamp + "selectedtime: " + time ); } catch ( System.Exception e ) { _errors.Add( Global.Common.TimeStamp( ) + "selectedtime: " + e.Message ); } } } } #endregion Methods #region Properties private string _cancelcaption = defaultcaptioncancel; [System.Runtime.InteropServices.ComVisible( true )] public string captioncancel { get { if ( _localizecaptions && _cancelcaption == defaultcaptioncancel ) { _cancelcaption = Global.Common.Localization.Load( "user32.dll", 801, defaultcaptioncancel ); } return _cancelcaption; } set { if ( string.IsNullOrWhiteSpace( value ) || value.Length > 20 ) { _cancelcaption = defaultcaptioncancel; } else { _cancelcaption = value; } //_debuginfo.Add() } } private string _captionok = defaultcaptionok; [System.Runtime.InteropServices.ComVisible( true )] public string captionok { get { if ( _localizecaptions && _captionok == defaultcaptionok ) { _captionok = Global.Common.Localization.Load( "user32.dll", 800, defaultcaptionok ); } return _captionok; } set { if ( string.IsNullOrWhiteSpace( value ) || value.Length > 20 ) { _captionok = defaultcaptionok; } else { _captionok = value; } } } private static string _culturegui = defaultculture; [System.Runtime.InteropServices.ComVisible( true )] public string culturegui { get { return _culturegui; } set { if ( Global.Common.Lists.Cultures( ).Contains( value, System.StringComparer.InvariantCultureIgnoreCase ) ) { _culturegui = value; } else { _culturegui = defaultculture; } dateformatgui = System.Globalization.CultureInfo.GetCultureInfo( _culturegui, defaultculture ).DateTimeFormat.ShortDatePattern; dateseparatorgui = System.Globalization.CultureInfo.GetCultureInfo( _culturegui, defaultculture ).DateTimeFormat.DateSeparator; timeformatgui = System.Globalization.CultureInfo.GetCultureInfo( _culturegui, defaultculture ).DateTimeFormat.LongTimePattern; timeseparatorgui = System.Globalization.CultureInfo.GetCultureInfo( _culturegui, defaultculture ).DateTimeFormat.TimeSeparator; } } private static string _culturein = defaultculture; [System.Runtime.InteropServices.ComVisible( true )] public string culturein { get { return _culturein; } set { if ( Global.Common.Lists.Cultures( ).Contains( value, System.StringComparer.InvariantCultureIgnoreCase ) ) { _culturein = value; } else { _culturein = defaultculture; } dateformatin = System.Globalization.CultureInfo.GetCultureInfo( _culturein, defaultculture ).DateTimeFormat.ShortDatePattern; dateseparatorin = System.Globalization.CultureInfo.GetCultureInfo( _culturein, defaultculture ).DateTimeFormat.DateSeparator; timeformatin = System.Globalization.CultureInfo.GetCultureInfo( _culturein, defaultculture ).DateTimeFormat.LongTimePattern; timeseparatorin = System.Globalization.CultureInfo.GetCultureInfo( _culturein, defaultculture ).DateTimeFormat.TimeSeparator; } } private static string _cultureout = defaultculture; [System.Runtime.InteropServices.ComVisible( true )] public string cultureout { get { return _cultureout; } set { if ( Global.Common.Lists.Cultures( ).Contains( value, System.StringComparer.InvariantCultureIgnoreCase ) ) { _cultureout = value; } else { _cultureout = defaultculture; } dateformatout = System.Globalization.CultureInfo.GetCultureInfo( _cultureout, defaultculture ).DateTimeFormat.ShortDatePattern; dateseparatorout = System.Globalization.CultureInfo.GetCultureInfo( _cultureout, defaultculture ).DateTimeFormat.DateSeparator; timeformatout = System.Globalization.CultureInfo.GetCultureInfo( _cultureout, defaultculture ).DateTimeFormat.LongTimePattern; timeseparatorout = System.Globalization.CultureInfo.GetCultureInfo( _cultureout, defaultculture ).DateTimeFormat.TimeSeparator; } } private string _dateformatgui = System.Globalization.CultureInfo.GetCultureInfo( _culturegui, defaultculture ).DateTimeFormat.ShortDatePattern; [System.Runtime.InteropServices.ComVisible( true )] public string dateformatgui { get { return _dateformatgui; } set { if ( value.IndexOfAny( "dMy".ToCharArray( ) ) == -1 ) { _errors.Add( Global.Common.TimeStamp( ) + "Custom date format (GUI) should contain at least one of the characters \"d\", \"M\" or \"y\"" ); } else { _dateformatgui = value; } } } private string _dateformatin = System.Globalization.CultureInfo.GetCultureInfo( _culturein, defaultculture ).DateTimeFormat.ShortDatePattern; [System.Runtime.InteropServices.ComVisible( true )] public string dateformatin { get { return _dateformatin; } set { if ( value.IndexOfAny( "dMy".ToCharArray( ) ) == -1 ) { _errors.Add( Global.Common.TimeStamp( ) + "Custom date format (in) should contain at least one of the characters \"d\", \"M\" or \"y\"" ); } else { _dateformatin = value; } } } private string _dateformatout = System.Globalization.CultureInfo.GetCultureInfo( _cultureout, defaultculture ).DateTimeFormat.ShortDatePattern; [System.Runtime.InteropServices.ComVisible( true )] public string dateformatout { get { return _dateformatout; } set { if ( value.IndexOfAny( "dMy".ToCharArray( ) ) == -1 ) { _errors.Add( Global.Common.TimeStamp( ) + "Custom date format (out) should contain at least one of the characters \"d\", \"M\" or \"y\"" ); } else { _dateformatout = value; } } } private bool _dateonly = false; [System.Runtime.InteropServices.ComVisible( true )] public int dateonly { get { return ( _dateonly ? 1 : 0 ); } set { _dateonly = ( value == 1 ); } } private string _dateseparatorgui = System.Globalization.CultureInfo.GetCultureInfo( _culturegui, defaultculture ).DateTimeFormat.DateSeparator; [System.Runtime.InteropServices.ComVisible( true )] public string dateseparatorgui { get { return _dateseparatorgui; } set { _dateseparatorgui = value; } } [System.Runtime.InteropServices.ComVisible( true )] public string dateseparatorin { get; set; } = System.Globalization.CultureInfo.GetCultureInfo( _culturein, defaultculture ).DateTimeFormat.DateSeparator; [System.Runtime.InteropServices.ComVisible( true )] public string dateseparatorout { get; set; } = System.Globalization.CultureInfo.GetCultureInfo( _cultureout, defaultculture ).DateTimeFormat.DateSeparator; private System.Collections.Generic.List _debuginfo = new System.Collections.Generic.List( ); [System.Runtime.InteropServices.ComVisible( true )] public string debuginfo { get { return string.Join( "\n", _debuginfo.ToArray( ) ); } } private System.DateTime _earliestdate = System.DateTime.MinValue; [System.Runtime.InteropServices.ComVisible( true )] public string earliestdate { get { return _earliestdate.ToString( "yyyy-MM-dd HH:mm:ss" ); } set { if ( _maxdaysback != -1 ) { _errors.Add( Global.Common.TimeStamp( ) + "You may specify either an earliest date or a maximum number of days back from the initial date, but not both" ); _earliestdate = System.DateTime.MinValue; } else if ( System.DateTime.TryParse( value, System.Globalization.CultureInfo.GetCultureInfo( culturein, null ), System.Globalization.DateTimeStyles.None, out System.DateTime testdate ) ) { if ( testdate.Ticks > _initialdatetime.Ticks ) { _errors.Add( Global.Common.TimeStamp( ) + "Earliest date must be before initial date/time" ); } else if ( testdate.Ticks > _latestdate.Ticks ) { _errors.Add( Global.Common.TimeStamp( ) + "Earliest date must be before latest date" ); } else { _earliestdate = testdate; } } else { _errors.Add( Global.Common.TimeStamp( ) + "Earlist date not recognized as a valid date" ); } } } private System.Collections.Generic.List _errors = new System.Collections.Generic.List( ); [System.Runtime.InteropServices.ComVisible( true )] public string errors { get { return string.Join( "\n", _errors.ToArray( ) ); } } private System.Drawing.FontFamily _fontfamily = defaultfontfamily; [System.Runtime.InteropServices.ComVisible( true )] public string fontfamily { get { return _fontfamily.Name; } set { _fontfamily = Global.Common.Validate.FontFamily( value, defaultfontfamily ); } } private float _fontsize = defaultfontsize; [System.Runtime.InteropServices.ComVisible( true )] public int fontsize { get { return System.Convert.ToInt32( _fontsize ); } set { _fontsize = System.Convert.ToSingle( value ).LimitToRange( minimumfontsize, maximumfontsize ); } } private static System.DateTime _initialdatetime = System.DateTime.Now; [System.Runtime.InteropServices.ComVisible( true )] public string initialdatetime { get { return _initialdatetime.ToString( "yyyy-MM-dd HH:mm:ss" ); } set { if ( System.DateTime.TryParse( value, System.Globalization.CultureInfo.GetCultureInfo( culturein, defaultculture ), System.Globalization.DateTimeStyles.None, out System.DateTime testdatetime ) ) { if ( testdatetime.Ticks > _latestdate.Ticks ) { _errors.Add( Global.Common.TimeStamp( ) + "Initial date/time must be before latest date" ); _initialdatetime = System.DateTime.Now; } else if ( testdatetime.Ticks > _earliestdate.Ticks ) { _errors.Add( Global.Common.TimeStamp( ) + "Initial date/time must be after earliest date" ); _initialdatetime = System.DateTime.Now; } else { _initialdatetime = testdatetime; } } else { _errors.Add( Global.Common.TimeStamp( ) + "Initial date/time not recognized as a valid date/time" ); _initialdatetime = System.DateTime.Now; } } } private System.DateTime _latestdate = System.DateTime.MaxValue; [System.Runtime.InteropServices.ComVisible( true )] public string latestdate { get { return _latestdate.ToString( "yyyy-MM-dd HH:mm:ss" ); } set { if ( _maxdaysforward != -1 ) { _errors.Add( Global.Common.TimeStamp( ) + "You may specify either a latest date or a maximum number of days forward from the initial date, but not both" ); _latestdate = System.DateTime.MaxValue; } else if ( System.DateTime.TryParse( value, System.Globalization.CultureInfo.GetCultureInfo( culturein, null ), System.Globalization.DateTimeStyles.None, out System.DateTime testdate ) ) { if ( testdate.Ticks < _initialdatetime.Ticks ) { _errors.Add( Global.Common.TimeStamp( ) + "Latest date must be after initial date/time" ); } else if ( testdate.Ticks < _earliestdate.Ticks ) { _errors.Add( Global.Common.TimeStamp( ) + "Latest date must be after earliest date" ); } else { _latestdate = testdate; } } else { _errors.Add( Global.Common.TimeStamp( ) + "Latest date not recognized as a valid date" ); } } } private int _left = -1; [System.Runtime.InteropServices.ComVisible( true )] public int left { get { return _left; } set { if ( value < 0 ) { _left = 0; } else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - windowwidth ) { _left = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - windowwidth; } else { _left = value; } } } private bool _literal = false; [System.Runtime.InteropServices.ComVisible( true )] public int literal { get { return ( _literal ? 1 : 0 ); } set { _literal = ( value == 1 ); } } private bool _localizecaptions = false; [System.Runtime.InteropServices.ComVisible( true )] public int localizecaptions { get { return ( _localizecaptions ? 1 : 0 ); } set { _localizecaptions = ( value == 1 ); } } private int _maxdaysback = -1; [System.Runtime.InteropServices.ComVisible( true )] public int maxdaysback { get { return _maxdaysback; } set { if ( _latestdate != System.DateTime.MinValue ) { _errors.Add( Global.Common.TimeStamp( ) + "You may specify either an earliest date or a maximum number of days back from the initial date, but not both" ); _maxdaysback = -1; } else if ( value < -1 ) { _errors.Add( Global.Common.TimeStamp( ) + "Invalid maximum offset in days back" ); _maxdaysback = -1; } else if ( _initialdatetime.AddDays( -1 * value ).Ticks > System.DateTime.MaxValue.Ticks ) { _errors.Add( Global.Common.TimeStamp( ) + "Maximum number of days back from initial date/time exceeds minimum date allowed" ); _maxdaysforward = -1; } else { _maxdaysback = value; } } } private int _maxdaysforward = -1; [System.Runtime.InteropServices.ComVisible( true )] public int maxdaysforward { get { return _maxdaysforward; } set { if ( _latestdate != System.DateTime.MaxValue ) { _errors.Add( Global.Common.TimeStamp( ) + "You may specify either a latest date or a maximum number of days forward from the initial date, but not both" ); _maxdaysforward = -1; } else if ( value < -1 ) { _errors.Add( Global.Common.TimeStamp( ) + "Invalid maximum offset in days forward" ); _maxdaysforward = -1; } else if ( _initialdatetime.AddDays( value ).Ticks > System.DateTime.MaxValue.Ticks ) { _errors.Add( Global.Common.TimeStamp( ) + "Maximum number of days forward from initial date/time exceeds maximum date allowed" ); _maxdaysforward = -1; } else { _maxdaysforward = value; } } } private bool topmost = true; [System.Runtime.InteropServices.ComVisible( true )] public int modal { get { return ( topmost ? 1 : 0 ); } set { topmost = ( value == 1 ); } } private string _prompt = string.Empty; [System.Runtime.InteropServices.ComVisible( true )] public string prompt { get { return _prompt; } set { if ( _literal ) { _prompt = value.Trim( ); } else { _prompt = value.Replace( "\\n", "\n" ).Replace( "\\t", "\t" ).Trim( ); } } } private System.DateTime _selecteddate = _initialdatetime; [System.Runtime.InteropServices.ComVisible( true )] public string selecteddate { get { if ( cancelled ) { return string.Empty; } else { return _selecteddate.ToString( dateformatout, System.Globalization.CultureInfo.GetCultureInfo( _cultureout, defaultculture ) ); } } } private System.TimeSpan _selectedtime = _initialdatetime.TimeOfDay; [System.Runtime.InteropServices.ComVisible( true )] public string selectedtime { get { if ( cancelled ) { return string.Empty; } else { return ( System.DateTime.Now.Date + _selectedtime ).ToString( timeformatout, System.Globalization.CultureInfo.GetCultureInfo( _cultureout, defaultculture ) ); } } } private int _selectedweek = GetIso8601WeekOfYear( _initialdatetime.Date ); [System.Runtime.InteropServices.ComVisible( true )] public int selectedweek { get { if ( cancelled ) { return -1; } else { return _selectedweek; } } } private string _selectedweekday = _initialdatetime.DayOfWeek.ToString( ); [System.Runtime.InteropServices.ComVisible( true )] public string selectedweekday { get { if ( cancelled ) { return string.Empty; } else { return _selectedweekday; } } } private string _timeformatgui = System.Globalization.CultureInfo.GetCultureInfo( _culturegui, defaultculture ).DateTimeFormat.LongTimePattern; [System.Runtime.InteropServices.ComVisible( true )] public string timeformatgui { get { return _timeformatgui; } set { if ( value.IndexOfAny( "hHmst".ToCharArray( ) ) == -1 ) { _errors.Add( Global.Common.TimeStamp( ) + "Custom time format (GUI) should contain at least one of the characters \"h\", \"H\", \"m\", \"s\" or \"t\"" ); } else { _timeformatgui = value; } } } private string _timeformatin = System.Globalization.CultureInfo.GetCultureInfo( _culturein, defaultculture ).DateTimeFormat.LongTimePattern; [System.Runtime.InteropServices.ComVisible( true )] public string timeformatin { get { return _timeformatin; } set { if ( value.IndexOfAny( "hHmst".ToCharArray( ) ) == -1 ) { _errors.Add( Global.Common.TimeStamp( ) + "Custom time format (in) should contain at least one of the characters \"h\", \"H\", \"m\", \"s\" or \"t\"" ); } else { _timeformatin = value; } } } private string _timeformatout = System.Globalization.CultureInfo.GetCultureInfo( _cultureout, defaultculture ).DateTimeFormat.LongTimePattern; [System.Runtime.InteropServices.ComVisible( true )] public string timeformatout { get { return _timeformatout; } set { if ( value.IndexOfAny( "hHmst".ToCharArray( ) ) == -1 ) { _errors.Add( Global.Common.TimeStamp( ) + "Custom time format (out) should contain at least one of the characters \"h\", \"H\", \"m\", \"s\" or \"t\"" ); } else { _timeformatout = value; } } } private bool _timeonly = false; [System.Runtime.InteropServices.ComVisible( true )] public int timeonly { get { return ( _timeonly ? 1 : 0 ); } set { _timeonly = ( value == 1 ); } } private int _timeout = 0; [System.Runtime.InteropServices.ComVisible( true )] public int timeout { get { return _timeout; } set { if ( value < 0 || value > 3600 ) { _timeout = 0; } else { _timeout = value; } } } private bool _timeoutelapsed = false; [System.Runtime.InteropServices.ComVisible( true )] public int timeoutelapsed => ( _timeoutelapsed ? 1 : 0 ); private string _timeseparatorgui = System.Globalization.CultureInfo.GetCultureInfo( _culturegui, defaultculture ).DateTimeFormat.TimeSeparator; [System.Runtime.InteropServices.ComVisible( true )] public string timeseparatorgui { get { return _timeseparatorgui; } set { _timeseparatorgui = value; } } private string _timeseparatorin = System.Globalization.CultureInfo.GetCultureInfo( _culturein, defaultculture ).DateTimeFormat.TimeSeparator; [System.Runtime.InteropServices.ComVisible( true )] public string timeseparatorin { get { return _timeseparatorin; } set { _timeseparatorin = value; } } private string _timeseparatorout = System.Globalization.CultureInfo.GetCultureInfo( _cultureout, defaultculture ).DateTimeFormat.TimeSeparator; [System.Runtime.InteropServices.ComVisible( true )] public string timeseparatorout { get { return _timeseparatorout; } set { _timeseparatorout = value; } } public static string timestamp => Global.Common.TimeStamp( ); private string _title = defaulttitle; [System.Runtime.InteropServices.ComVisible( true )] public string title { get { return _title; } set { if ( string.IsNullOrWhiteSpace( value ) ) { _title = string.Format( "{0}, Version {1}", Global.Common.ProgramInfo.FileName, Global.Common.ProgramInfo.FileVersion ); } else { _title = value.Trim( ); } } } private int _top = -1; [System.Runtime.InteropServices.ComVisible( true )] public int top { get { return _top; } set { if ( value < 0 ) { _top = 0; } else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - windowheight ) { _top = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - windowheight; } else { _top = value; } } } [System.Runtime.InteropServices.ComVisible( true )] public string Version { get { return Global.Common.ProgramInfo.FileVersion; } } private int _windowheight = defaultwindowheight; [System.Runtime.InteropServices.ComVisible( true )] public int windowheight { get { return _windowheight; } set { if ( value < defaultwindowheight ) { _windowheight = defaultwindowheight; } else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height ) { _windowheight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; } else { _windowheight = value; } } } private int _windowwidth = defaultwindowwidth; [System.Runtime.InteropServices.ComVisible( true )] public int windowwidth { get { return _windowwidth; } set { if ( value < defaultwindowwidth ) { _windowwidth = defaultwindowwidth; } else if ( value > System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width ) { _windowwidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; } else { _windowwidth = value; } } } #endregion Properties #region Event handlers private void DateTimeBox_DatePicker_TextChanged( object sender, System.EventArgs e ) { _selecteddate = datepicker.Value; } private void DateTimeBox_TimePicker_TextChanged( object sender, System.EventArgs e ) { _selectedtime = timepicker.Value.TimeOfDay; } private void DateTimeBox_Timer_Elapsed( object sender, System.Timers.ElapsedEventArgs e ) { _timeoutelapsed = true; if ( !_timeonly ) { _selecteddate = datepicker.Value; } if ( !_dateonly ) { _selectedtime = timepicker.Value.TimeOfDay; } _debuginfo.Add( timestamp + "DateTimeBox dialog result: Timeout elapsed" ); datetimeboxform.Close( ); } #endregion Event Handlers } }