(view source code of dialogboxes_openfolderbox.cs as plain text)
namespace RobvanderWoude
{
[System.Runtime.InteropServices.Guid( "F0C24C90-8D15-49DA-882C-8AFBEBF3D534" )]
public interface OpenFolderBox_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 ListSpecialFolders( );
[System.Runtime.InteropServices.DispId( 6 )]
string SampleCode( );
[System.Runtime.InteropServices.DispId( 7 )]
void Show( );
}
[System.Runtime.InteropServices.Guid( "7451DB13-75E8-478B-8EAA-4B2BF26EA195" ), System.Runtime.InteropServices.InterfaceType( System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch )]
public interface OpenFolderBox_Events
{
}
[System.Runtime.InteropServices.Guid( "68B64FDE-9021-469D-B9AD-9782E7968B01" ), System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.AutoDual ), System.Runtime.InteropServices.ComSourceInterfaces( typeof( OpenFolderBox_Events ) )]
public class OpenFolderBox : OpenFolderBox_Interface
{
#region Default values
//static System.Environment.SpecialFolder defaultrootfolder = System.Environment.SpecialFolder.MyDocuments; // User's documents only
//static System.Environment.SpecialFolder defaultrootfolder = System.Environment.SpecialFolder.Desktop; // Entire computer and network
static System.Environment.SpecialFolder defaultrootfolder = System.Environment.SpecialFolder.MyComputer; // Entire computer
static readonly string defaultstartfolder = System.Environment.GetFolderPath( System.Environment.SpecialFolder.MyDocuments );
#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 OpenFolderBox class</h1>\n\n";
help += "<p>Present a Browse Folder dialog, and return the selected folder path</p>\n\n";
help += "<h2>COM ProgID: RobvanderWoude.OpenFolderBox</h2>\n\n";
}
else
{
help += "Help for OpenFolderBox class\n";
help += new string( '\u2500', help.Length ) + "\n\n";
help += "Present a Browse Folder dialog, and return the selected folder path\n\n\n";
help += "COM ProgID: RobvanderWoude.OpenFolderBox\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, "allowmakedir", "Show \"Make New Folder\" button (0=false; 1=true)", "no", "no", "0: don't show", html );
help += separatorline;
help += HelpTableRow( linetemplate, "debuginfo", Global.Common.Help.debuginfo, "N/A", "YES", "", html );
help += separatorline;
help += HelpTableRow( linetemplate, "description", "Text above directory tree in dialog", "no", "no", "", html );
help += separatorline;
help += HelpTableRow( linetemplate, "errors", Global.Common.Help.errors, "N/A", "YES", "", html );
help += separatorline;
help += HelpTableRow( linetemplate, "rootfolderfolder", "Windows Special Folder that is used as the directory tree root", "no", "no", defaultrootfolder.ToString( ) + " (use the \"ListSpecialFolders\" method to get a list of valid names)", html );
help += separatorline;
help += HelpTableRow( linetemplate, "selectedfolder", "The path of the selected folder", "N/A", "YES", "", html );
help += separatorline;
help += HelpTableRow( linetemplate, "startfolder", "The initial folder for the dialog", "no", "no", defaultstartfolder, 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, "ListSpecialFolders", "Returns a semicolon-separated list of Windows Special Folders", "", 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 Browse Folder dialog based on the current property values, and if \"OK\"is clicked, saves the selected folder path in the \"selectedfolder\" property (if \"Cancel\" is clicked, the startfolder is assumed selected).", "", 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 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 ListSpecialFolders( )
{
return string.Join( ";", Global.Common.Lists.SpecialFolders( ) );
}
public string SampleCode( )
{
string code = "Set objOpenFolderBox = CreateObject( \"RobvanderWoude.OpenFolderBox\" )\n\n";
code += "With objOpenFolderBox\n";
code += "\t.rootfolder = \"MyDocuments\"\n";
code += "\t.Show\n";
code += "\tWScript.Echo \"Selected folder: \" & .selectedfolder\n";
code += "\tWScript.Echo\n";
code += "\tWScript.Echo .ListProperties( )\n";
code += "End With\n\n";
code += "Set objOpenFolderBox = Nothing";
return code;
}
[System.STAThread]
public void Show( )
{
try
{
using ( System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog( ) )
{
dialog.RootFolder = _rootfolder;
if ( !string.IsNullOrWhiteSpace( startfolder ) )
{
dialog.SelectedPath = startfolder;
}
dialog.Description = description;
dialog.ShowNewFolderButton = _allowmakedir;
if ( dialog.ShowDialog( ) == System.Windows.Forms.DialogResult.OK )
{
startfolder = dialog.SelectedPath;
}
}
}
catch ( System.Exception e )
{
_errors.Add( Global.Common.TimeStamp( ) + "Error while trying to show dialog: " + e.Message );
}
}
#endregion Methods
#region Properties
private bool _allowmakedir = false;
[System.Runtime.InteropServices.ComVisible( true )]
public int allowmakedir
{
get
{
return ( _allowmakedir ? 1 : 0 );
}
set
{
_allowmakedir = ( 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 _description = string.Empty;
[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 System.Environment.SpecialFolder _rootfolder = defaultrootfolder;
[System.Runtime.InteropServices.ComVisible( true )]
public string rootfolder
{
get
{
return _rootfolder.ToString( );
}
set
{
if ( Global.Common.Lists.SpecialFolders( ).Contains( value ) )
{
try
{
_rootfolder = (System.Environment.SpecialFolder) System.Enum.Parse( typeof( System.Environment.SpecialFolder ), value, true );
}
catch( System.Exception e )
{
_debuginfo.Add( timestamp + "Error while trying to make sense of specified root directory: " + e.Message );
_debuginfo.Add( timestamp + "Using " + defaultrootfolder.ToString( ) + " instead" );
_rootfolder = defaultrootfolder;
}
}
else
{
_debuginfo.Add( timestamp + "Invalid root directory, using " + defaultrootfolder.ToString( ) + " instead" );
_rootfolder = defaultrootfolder;
}
}
}
private string _selectedfolder = string.Empty;
[System.Runtime.InteropServices.ComVisible( true )]
public string selectedfolder
{
get
{
return _selectedfolder;
}
}
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 " + defaultrootfolder.ToString( ) + " instead" );
_startfolder = string.Empty;
}
}
}
else
{
_debuginfo.Add( timestamp + "Invalid start directory, using " + defaultrootfolder.ToString( ) + " instead" );
_startfolder = string.Empty;
}
_selectedfolder = _startfolder;
}
}
public static string timestamp => Global.Common.TimeStamp( );
[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.0141 seconds