using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.InteropServices;
//Folgender Verweis wird benötigt:
// - Microsoft.VisualBasic.Compatibility
//Folgende Toolbox-Elemente werden benötigt:
// - DriveListBox
// - FileListBox
// - DirListBox
///
/// Summary description for Form1.
///
public class FolderDialog : System.Windows.Forms.Form
{
internal Label lblDriveListBox;
internal Label lblFileListBox;
private Microsoft.VisualBasic.Compatibility.VB6.DriveListBox driveListBox;
private Microsoft.VisualBasic.Compatibility.VB6.FileListBox fileListBox;
private Microsoft.VisualBasic.Compatibility.VB6.DirListBox dirListBox;
internal Button btnSelect;
internal Button btnCancel;
private IContainer components;
internal Label lblFilename;
private TextBox txtFileName;
private ComboBox cmbTyp;
internal Label lblFiletype;
private Timer timer1;
private string tempDrive;
public FolderDialog()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
tempDrive = this.driveListBox.Drive;
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FolderDialog));
this.driveListBox = new Microsoft.VisualBasic.Compatibility.VB6.DriveListBox();
this.fileListBox = new Microsoft.VisualBasic.Compatibility.VB6.FileListBox();
this.dirListBox = new Microsoft.VisualBasic.Compatibility.VB6.DirListBox();
this.lblDriveListBox = new System.Windows.Forms.Label();
this.lblFileListBox = new System.Windows.Forms.Label();
this.btnSelect = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.lblFilename = new System.Windows.Forms.Label();
this.txtFileName = new System.Windows.Forms.TextBox();
this.cmbTyp = new System.Windows.Forms.ComboBox();
this.lblFiletype = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// driveListBox
//
this.driveListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.driveListBox.FormattingEnabled = true;
this.driveListBox.Location = new System.Drawing.Point(16, 24);
this.driveListBox.Name = "driveListBox";
this.driveListBox.Size = new System.Drawing.Size(248, 21);
this.driveListBox.TabIndex = 7;
this.driveListBox.SelectedIndexChanged += new System.EventHandler(this.driveListBox_SelectedIndexChanged);
//
// fileListBox
//
this.fileListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.fileListBox.FormattingEnabled = true;
this.fileListBox.HorizontalScrollbar = true;
this.fileListBox.Location = new System.Drawing.Point(270, 50);
this.fileListBox.Name = "fileListBox";
this.fileListBox.Pattern = "*.*";
this.fileListBox.Size = new System.Drawing.Size(193, 186);
this.fileListBox.TabIndex = 10;
this.fileListBox.SelectedIndexChanged += new System.EventHandler(this.fileListBox_SelectedIndexChanged);
//
// dirListBox
//
this.dirListBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dirListBox.FormattingEnabled = true;
this.dirListBox.HorizontalScrollbar = true;
this.dirListBox.IntegralHeight = false;
this.dirListBox.Location = new System.Drawing.Point(16, 51);
this.dirListBox.Name = "dirListBox";
this.dirListBox.Size = new System.Drawing.Size(248, 185);
this.dirListBox.TabIndex = 8;
this.dirListBox.Change += new System.EventHandler(this.dirListBox_Change);
this.dirListBox.SelectedIndexChanged += new System.EventHandler(this.dirListBox_SelectedIndexChanged);
//
// lblDriveListBox
//
this.lblDriveListBox.Location = new System.Drawing.Point(16, 8);
this.lblDriveListBox.Name = "lblDriveListBox";
this.lblDriveListBox.Size = new System.Drawing.Size(100, 16);
this.lblDriveListBox.TabIndex = 6;
this.lblDriveListBox.Text = "Drive:";
//
// lblFileListBox
//
this.lblFileListBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.lblFileListBox.Location = new System.Drawing.Point(270, 31);
this.lblFileListBox.Name = "lblFileListBox";
this.lblFileListBox.Size = new System.Drawing.Size(128, 16);
this.lblFileListBox.TabIndex = 9;
this.lblFileListBox.Text = "Files:";
//
// btnSelect
//
this.btnSelect.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnSelect.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnSelect.Enabled = false;
this.btnSelect.Location = new System.Drawing.Point(388, 240);
this.btnSelect.Name = "btnSelect";
this.btnSelect.Size = new System.Drawing.Size(75, 23);
this.btnSelect.TabIndex = 4;
this.btnSelect.Text = "OK";
this.btnSelect.UseVisualStyleBackColor = true;
this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
//
// btnCancel
//
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(388, 269);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 5;
this.btnCancel.Text = "Cancel";
//
// lblFilename
//
this.lblFilename.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.lblFilename.AutoSize = true;
this.lblFilename.Location = new System.Drawing.Point(13, 245);
this.lblFilename.Name = "lblFilename";
this.lblFilename.Size = new System.Drawing.Size(52, 13);
this.lblFilename.TabIndex = 0;
this.lblFilename.Text = "Filename:";
//
// txtFileName
//
this.txtFileName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtFileName.Location = new System.Drawing.Point(95, 242);
this.txtFileName.Name = "txtFileName";
this.txtFileName.Size = new System.Drawing.Size(287, 20);
this.txtFileName.TabIndex = 1;
this.txtFileName.TextChanged += new System.EventHandler(this.txtFileName_TextChanged);
//
// cmbTyp
//
this.cmbTyp.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.cmbTyp.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbTyp.FormattingEnabled = true;
this.cmbTyp.Location = new System.Drawing.Point(95, 268);
this.cmbTyp.Name = "cmbTyp";
this.cmbTyp.Size = new System.Drawing.Size(287, 21);
this.cmbTyp.TabIndex = 3;
this.cmbTyp.SelectedIndexChanged += new System.EventHandler(this.cmbTyp_SelectedIndexChanged);
//
// lblFiletype
//
this.lblFiletype.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.lblFiletype.AutoSize = true;
this.lblFiletype.Location = new System.Drawing.Point(13, 271);
this.lblFiletype.Name = "lblFiletype";
this.lblFiletype.Size = new System.Drawing.Size(46, 13);
this.lblFiletype.TabIndex = 2;
this.lblFiletype.Text = "Filetype:";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// FolderDialog
//
this.AcceptButton = this.btnSelect;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(475, 303);
this.Controls.Add(this.lblFiletype);
this.Controls.Add(this.cmbTyp);
this.Controls.Add(this.txtFileName);
this.Controls.Add(this.lblFilename);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnSelect);
this.Controls.Add(this.lblFileListBox);
this.Controls.Add(this.lblDriveListBox);
this.Controls.Add(this.dirListBox);
this.Controls.Add(this.fileListBox);
this.Controls.Add(this.driveListBox);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(483, 337);
this.Name = "FolderDialog";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "FolderDialog";
this.Load += new System.EventHandler(this.FolderDialog_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private int intDefaultFilterIndex = 0;
private string strDefaultExt;
private bool blnOverwritePrompt = false;
private string strFilter;
private string[] strPatternArray;
private string strDenyDrives = "";
///
/// Returns the selected path (without FileName)
///
public string FilePath
{
get { return dirListBox.Path; }
}
///
/// Returns full selected File (Filename and Path)
///
public string FullFileNamePath
{
get
{
return this.FilePath + "\\" + this.FileName;
}
}
///
/// Set Dialog Title
///
public string Title
{
set
{
this.Text = value;
}
}
///
/// Set initial Directory
///
public string InitialDirectory
{
set
{
driveListBox.Drive = value.Substring(0, 2);
dirListBox.Path = value;
}
}
///
/// Returns the selected or typed FileName (with extension)
///
public string FileName
{
get
{
return Path.HasExtension(txtFileName.Text) ? txtFileName.Text : txtFileName.Text + strDefaultExt;
}
set
{
txtFileName.Text = value;
}
}
///
/// Default (OnLoad) FilterIdex selected in Combobox (-1 show all files)
///
public int DefaultFilterIndex
{
get { return intDefaultFilterIndex; }
set { intDefaultFilterIndex = value; }
}
///
/// Default Extension, If user don't type a extension
///
public string DefaultExt
{
get { return strDefaultExt; }
set { strDefaultExt = value; }
}
public bool OverwritePrompt
{
get
{
return blnOverwritePrompt;
}
set
{
blnOverwritePrompt = value;
}
}
///
/// Drives which should deny access, example "CE" => Deny Drive C and E
///
public string DenyDrives
{
get
{
return strDenyDrives;
}
set
{
strDenyDrives = value;
}
}
///
/// Filter for FileListBox
/// Example: Filter = Image Files(*.BMP;*.JPG)|*.BMP;*.JPG|All Files(*.*)|*.*
///
public string Filter
{
get
{
return strFilter;
}
set
{
strFilter = value;
string[] strArray;
strArray = strFilter.Split('|');
cmbTyp.Items.Clear();
fileListBox.Pattern = "";
strPatternArray = new string[strArray.Length / 2];
int j=0;
for (int i = 0; i < strArray.Length; i++)
{
cmbTyp.Items.Add(strArray[i]);
i++;
strPatternArray[j] = strArray[i];
j++;
}
}
}
private void driveListBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
try
{
this.dirListBox.Path = this.driveListBox.Drive;
tempDrive= this.driveListBox.Drive;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
driveListBox.Drive = tempDrive;
}
}
private void dirListBox_Change(object sender, System.EventArgs e)
{
try
{
this.fileListBox.Path = this.dirListBox.Path;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void dirListBox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
this.fileListBox.Path = this.dirListBox.Path;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void FolderDialog_Load(object sender, System.EventArgs e)
{
cmbTyp.SelectedIndex = intDefaultFilterIndex;
RemoveDrives(driveListBox, strDenyDrives, ShowRemoveDriveTypeConstants.dtRemove);
}
private void cmbTyp_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbTyp.SelectedIndex >= 0)
{
fileListBox.Pattern = strPatternArray[cmbTyp.SelectedIndex];
}
}
private void btnSelect_Click(object sender, EventArgs e)
{
if (blnOverwritePrompt)
{
//Check File if exist
if (File.Exists(this.FullFileNamePath))
{
if (MessageBox.Show(String.Format("File {0} already exist.\nDo you want to replace it?"),
this.FullFileNamePath), this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
this.DialogResult = DialogResult.OK;
else
this.DialogResult = DialogResult.None;
}
}
//Check valid Filename
char[] chrInvalidChars = Path.GetInvalidFileNameChars();
if (txtFileName.Text.IndexOfAny(chrInvalidChars)>=0)
{
MessageBox.Show("You have entered an invalid char in the filename. Please correct it!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.DialogResult = DialogResult.None;
}
}
private void fileListBox_SelectedIndexChanged(object sender, EventArgs e)
{
txtFileName.Text = fileListBox.SelectedItem.ToString();
}
private enum ShowRemoveDriveTypeConstants
{
dtShow,
dtRemove
}
///
/// Remove Drive from DriveListBox, so you cannot access to this drives
///
/// DriveListBox to modify
/// Drive letters example: "cd",
/// If dtRemove then the Drives would be removed from the list
/// If dtShow then only the typed Drives showed in Listbox
private void RemoveDrives(Microsoft.VisualBasic.Compatibility.VB6.DriveListBox drivelist, string drives,
ShowRemoveDriveTypeConstants ShowRemove)
{
int i;
string nDrives;
nDrives = drives.ToLower();
switch (ShowRemove)
{
case ShowRemoveDriveTypeConstants.dtShow:
for (i=drivelist.Items.Count-1;i>=0; i--)
{
if (nDrives.IndexOf(drivelist.Items[i].ToString().Substring(0,1)) < 0)
{
drivelist.Items.RemoveAt(i);
}
}
break;
case ShowRemoveDriveTypeConstants.dtRemove:
for (i=drivelist.Items.Count-1;i>=0; i--)
{
if (nDrives.IndexOf(drivelist.Items[i].ToString().Substring(0,1)) >= 0)
{
drivelist.Items.RemoveAt(i);
}
}
break;
default:
break;
}
}
private void txtFileName_TextChanged(object sender, EventArgs e)
{
btnSelect.Enabled = txtFileName.Text.Length > 0;
}
private void timer1_Tick(object sender, EventArgs e)
{
//Update the DriveListBox
driveListBox.Refresh();
}
}