(view source code of dialogboxes_openfilebox.cs as plain text)
namespace RobvanderWoude
{
[System.Runtime.InteropServices.Guid( "766B494F-6F7D-41A6-A72B-0597D07D685E" )]
public interface OpenFileBox_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( "383FD006-91A0-40CF-A42D-82FAC95BA4E5" ), System.Runtime.InteropServices.InterfaceType( System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch )]
public interface OpenFileBox_Events
{
}
[System.Runtime.InteropServices.Guid( "2DA001A3-8C65-4AAF-8EB6-B29C11C31EC7" ), System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.AutoDual ), System.Runtime.InteropServices.ComSourceInterfaces( typeof( OpenFileBox_Events ) )]
public class OpenFileBox : OpenFileBox_Interface
{
#region Default Values
static string defaultdescription = ( _multiselect ? "Select one or more files" : "Select a file" );
const string defaultfilter = "All files (*.*)|*.*";
static readonly string defaultstartfolder = System.IO.Directory.GetCurrentDirectory( );
static readonly string defaulttitle = string.Format( "{0}, Version {1}", Global.Common.ProgramInfo.FileName, Global.Common.ProgramInfo.FileVersion );
#endregion Default Values
#region Methods
public void CheckUpdate( )
{
Global.Common.ProgramInfo.CheckUpdate( );
}
public string Credits( )
{
return Global.Common.Credits( );
}
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 += "<h1>Help for OpenFileBox class</h1>\n\n";
help += "<p>Present an Open File dialog, and return the selected file path</p>\n\n";
help += "<h2>COM ProgID: RobvanderWoude.OpenFileBox</h2>\n\n";
}
else
{
help += "Help for OpenFileBox class\n";
help += new string( '\u2500', help.Length ) + "\n\n";
help += "Present an Open File dialog, and return the selected file path\n\n\n";
help += "COM ProgID: RobvanderWoude.OpenFileBox\n\n\n";
}
#region Properties
if ( html == 1 )
{
separatorline = string.Empty;
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";
help += "<table>\n";
help += "<tr>\n";
help += "\t<th colspan=\"5\">Properties (Note that all properties are string or integer, no booleans, objects or arrays)</th>\n";
help += "</tr>\n";
help += "<tr>\n";
help += "\t<th>Property Name</th>\n";
help += "\t<th>Description</th>\n";
help += "\t<th>Mandatory</th>\n";
help += "\t<th>Read-Only</th>\n";
help += "\t<th>Default (Allowed) Values</th>\n";
help += "</tr>\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, "addallfiles", "Add \"All files (*.*)\" to filter (0=false; 1=true)", "no", "no", "1", html );
help += separatorline;
help += HelpTableRow( linetemplate, "addextension", "Add extension if user forgot it (0=false; 1=true)", "no", "no", "1", html );
help += separatorline;
help += HelpTableRow( linetemplate, "debuginfo", Global.Common.Help.debuginfo, "N/A", "YES", "", html );
help += separatorline;
help += HelpTableRow( linetemplate, "defaultextension", "Default extension to be added", "no", "no", "", html );
help += separatorline;
help += HelpTableRow( linetemplate, "errors", Global.Common.Help.errors, "N/A", "YES", "", html );
help += separatorline;
help += HelpTableRow( linetemplate, "filter", "File filter", "no", "no", defaultfilter, html );
help += separatorline;
help += HelpTableRow( linetemplate, "multidotted", "Allow multiple extensions (0=false; 1=true)", "no", "no", "1", html );
help += separatorline;
help += HelpTableRow( linetemplate, "multiselect", "Allow selecting multiple files (0=false; 1=true)", "no", "no", "0", html );
help += separatorline;
help += HelpTableRow( linetemplate, "readonlychecked", "Checkbox read-only is checked (0=false; 1=true)", "no", "no", "0", html );
help += separatorline;
help += HelpTableRow( linetemplate, "selectedfile", "The path of the selected file(s)", "N/A", "YES", "", html );
help += separatorline;
help += HelpTableRow( linetemplate, "showreadonly", "Show checkbox to open read-only (0=false; 1=true)", "no", "no", "0", html );
help += separatorline;
help += HelpTableRow( linetemplate, "startfolder", "The initial folder for the dialog", "no", "no", "", html );
help += separatorline;
help += HelpTableRow( linetemplate, "title", Global.Common.Help.title, "no", "no", defaulttitle, html );
if ( html == 1 )
{
help += "</table>\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 = "<tr>\n\t<td>{0}</td>\n\t<td>{1}</td>\n\t<td>{2}</td>\n</tr>\n";
help += "<table>\n";
help += "<tr>\n";
help += "\t<th colspan=\"3\">Methods</th>\n";
help += "</tr>\n";
help += "<tr>\n";
help += "\t<th>Method Name</th>\n";
help += "\t<th>Description</th>\n";
help += "\t<th>Requirements</th>\n";
help += "</tr>\n";
}
else
{
col1width = 18;
col2width = 74;
col3width = 42;
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 an Open File dialog based on the current property values, and if \"OK\"is clicked, saves the selected file path in the \"selectedfile\" property (if \"Cancel\" is clicked, \"selectedfile\" will be empty).", "", html );
if ( html == 1 )
{
help += "</table>\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 += "<table>\n<tr>\n\t<th>";
}
help += "Notes:";
if ( html == 1 )
{
help += "</th>\n\t<td>";
}
else
{
help += "\n\t";
}
help += "Though the name may suggest otherwise, this dialog does NOT actually open the selected file, it merely returns the selected file's path.";
if ( html == 1 )
{
help += "<br />\n";
}
else
{
help += "\n\t";
}
help += "A similar warning goes for the read-only checkbox: it doesn't actually DO anything, but you can use it to allow the user a choice for the";
if ( html == 1 )
{
help += " ";
}
else
{
help += "\n\t";
}
help += "actions based on this dialog's result.";
if ( html == 1 )
{
help += "</td>\n</tr>\n</table>";
}
help += "\n\n\n";
#endregion Notes
#region Example
if ( html == 1 )
{
help += "<h2>VBScript usage example:</h2>\n\n<pre>";
}
else
{
help += "VBScript usage example:\n";
help += new string( '\u2500', 23 ) + "\n\n";
}
help += SampleCode( );
if ( html == 1 )
{
help += "</pre>";
}
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 objOpenFileBox = CreateObject( \"RobvanderWoude.OpenFileBox\" )\n\n";
code += "With objOpenFileBox\n";
code += "\t.startfolder = \"D:\\\"\n";
code += "\t.Show\n";
code += "\tWScript.Echo \"Selected file: \" & .selectedfile\n";
code += "\tWScript.Echo\n";
code += "\tWScript.Echo .ListProperties( )\n";
code += "End With\n\n";
code += "Set objOpenFileBox = Nothing";
return code;
}
[System.STAThread]
public void Show( )
{
#region Apply Settings
string ext = System.Text.RegularExpressions.Regex.Match( filter, @"(\.[^.]+)$", System.Text.RegularExpressions.RegexOptions.IgnoreCase ).Groups[0].ToString( );
if ( string.IsNullOrWhiteSpace( ext ) )
{
_errors.Add( Global.Common.TimeStamp( ) + "Invalid filetype specification" );
}
// If only "*.ext" is specified, use "ext files (*.ext)|*.ext" instead
if ( System.Text.RegularExpressions.Regex.IsMatch( filter, @"^\*\.(\*|\w+)$" ) )
{
if ( ext == ".*" )
{
filter = string.Format( "All files (*.{0})|*.{0}", ext );
}
else
{
filter = string.Format( "{0} files (*.{0})|*.{0}", ext );
}
}
// Use "All files" filter if not specified
if ( string.IsNullOrWhiteSpace( filter ) )
{
filter = "All files (*.*)|*.*";
}
#endregion Apply Settings
try
{
using ( System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog( ) )
{
dialog.AutoUpgradeEnabled = true;
dialog.AddExtension = _addextension;
dialog.CheckFileExists = true;
dialog.CheckPathExists = true;
dialog.DefaultExt = _defaultextension;
dialog.Filter = filter;
dialog.FilterIndex = 1;
dialog.InitialDirectory = startfolder;
dialog.Multiselect = _multiselect;
dialog.ReadOnlyChecked = _readonlychecked;
dialog.RestoreDirectory = true;
dialog.ShowReadOnly = _showreadonly;
dialog.SupportMultiDottedExtensions = _multidotted;
dialog.Title = title;
dialog.ValidateNames = true;
if ( dialog.ShowDialog( ) == System.Windows.Forms.DialogResult.OK )
{
_selectedfile = dialog.FileName;
}
else
{
_selectedfile = string.Empty;
}
}
}
catch ( System.Exception e )
{
_errors.Add( Global.Common.TimeStamp( ) + "Error while trying to show Open File dialog: " + e.Message );
}
}
#endregion Methods
#region Properties
private bool _addallfiles = true;
[System.Runtime.InteropServices.ComVisible( true )]
public int addallfiles
{
get
{
return ( _addallfiles ? 1 : 0 );
}
set
{
_addallfiles = ( value == 1 );
}
}
private bool _addextension = false;
[System.Runtime.InteropServices.ComVisible( true )]
public int addextension
{
get
{
return ( _addextension ? 1 : 0 );
}
set
{
_addextension = ( value == 1 );
}
}
private System.Collections.Generic.List<string> _debuginfo = new System.Collections.Generic.List<string>( );
[System.Runtime.InteropServices.ComVisible( true )]
public string debuginfo
{
get
{
return string.Join( "\n", _debuginfo.ToArray( ) );
}
}
private string _defaultextension = string.Empty;
[System.Runtime.InteropServices.ComVisible( true )]
public string defaultextension
{
get
{
return _defaultextension;
}
set
{
if ( value.StartsWith( "." ) )
{
_defaultextension = value.Trim( );
}
else
{
_debuginfo.Add( timestamp + "Invalid default extension, using \".*\" instead" );
_defaultextension = ".*";
}
}
}
private string _description = defaultdescription;
[System.Runtime.InteropServices.ComVisible( true )]
public string description
{
get
{
return _description;
}
set
{
_description = value.Trim( );
}
}
private System.Collections.Generic.List<string> _errors = new System.Collections.Generic.List<string>( );
[System.Runtime.InteropServices.ComVisible( true )]
public string errors
{
get
{
return string.Join( "\n", _errors.ToArray( ) );
}
}
private string _filter = defaultfilter;
[System.Runtime.InteropServices.ComVisible( true )]
public string filter
{
get
{
return _filter;
}
set
{
// If only "*.ext" is specified, use "ext files (*.ext)|*.ext" instead
if ( System.Text.RegularExpressions.Regex.IsMatch( value, @"^\*\.(\*|\w+)$" ) )
{
string ext = value.Substring( 2 ).ToLower( );
if ( ext == ".*" )
{
_filter = string.Format( "All files (*.{0})|*.{0}", ext );
}
else
{
_filter = string.Format( "{0} files (*.{0})|*.{0}", ext );
}
}
else
{
_filter = value;
}
if ( _addallfiles )
{
// Append "All files" filter if not specified
if ( !System.Text.RegularExpressions.Regex.IsMatch( filter, @"All files\s+\(\*\.\*\)\|\*\.\*", System.Text.RegularExpressions.RegexOptions.IgnoreCase ) )
{
if ( string.IsNullOrWhiteSpace( _filter ) )
{
_filter = "All files (*.*)|*.*";
}
else
{
_filter += "|All files (*.*)|*.*";
}
}
}
}
}
private bool _multidotted = true;
[System.Runtime.InteropServices.ComVisible( true )]
public int multidotted
{
get
{
return ( _multidotted ? 1 : 0 );
}
set
{
_multidotted = ( value == 1 );
}
}
private static bool _multiselect = false;
[System.Runtime.InteropServices.ComVisible( true )]
public int multiselect
{
get
{
return ( _multiselect ? 1 : 0 );
}
set
{
_multiselect = ( value == 1 );
}
}
private bool _readonlychecked = false;
[System.Runtime.InteropServices.ComVisible( true )]
public int readonlychecked
{
get
{
return ( _readonlychecked ? 1 : 0 );
}
set
{
_readonlychecked = ( value == 1 );
}
}
private string _selectedfile = string.Empty;
[System.Runtime.InteropServices.ComVisible( true )]
public string selectedfile
{
get
{
return _selectedfile;
}
}
private bool _showreadonly = false;
[System.Runtime.InteropServices.ComVisible( true )]
public int showreadonly
{
get
{
return ( _showreadonly ? 1 : 0 );
}
set
{
_showreadonly = ( value == 1 );
}
}
private string _startfolder = defaultstartfolder;
[System.Runtime.InteropServices.ComVisible( true )]
public string startfolder
{
get
{
return _startfolder;
}
set
{
if ( System.IO.Directory.Exists( value ) )
{
try
{
_startfolder = System.IO.Path.GetFullPath( value );
}
catch ( System.ArgumentException )
{
try
{
// Assuming the error is caused by a trailing backslash in doublequotes
_startfolder = System.IO.Path.GetFullPath( value.Substring( 0, startfolder.IndexOf( '"' ) ) + "." );
}
catch ( System.Exception )
{
_debuginfo.Add( timestamp + "Invalid start directory, using current directory instead" );
_startfolder = ".";
}
}
}
else
{
_debuginfo.Add( timestamp + "Invalid start directory, using current directory instead" );
_startfolder = ".";
}
_selectedfile = _startfolder;
}
}
public static string timestamp => Global.Common.TimeStamp( );
private string _title = defaulttitle;
[System.Runtime.InteropServices.ComVisible( true )]
public string title
{
get
{
return _title;
}
set
{
_title = value.Trim( );
}
}
[System.Runtime.InteropServices.ComVisible( true )]
public string Version
{
get
{
return Global.Common.ProgramInfo.FileVersion;
}
}
#endregion Properties
}
}
page last modified: 2024-04-16; loaded in 0.0131 seconds