 |
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1).
** Please don't post code questions here **
For issues specific to a particular language in .NET, please see the other forum categories. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

June 24th, 2003, 03:09 PM
|
Registered User
|
|
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Open directory dialog box
Hi,
I have a question. I'm looking for a dialog box where the user can browse for a directory. All I can find is a dialog box where you can open a file.
Is there one to open a directory? If not, how can I solve this so the user can select a directory?
Thanks.
|

June 24th, 2003, 03:41 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You have a few options. One of these is to use API calls to have the Browse For Folder dialog presented. The book "Solutions Toolkit - 30 Practical Components for .NET" (ISBN: 1-86100-739-6) shows you how to do this.
Alternatively, you can do it the "old way" Create a form that has a DirListBox and a DriveListBox. On the SelectedIndexChanged event of the DriveListBox, update the DirListBox.
The third option is to upgrade to Visual Studio .NET 2003 / .NET Framework 1.1. It has a FolderBrowserDialog which is exactly what you need.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|

June 25th, 2003, 05:32 PM
|
Registered User
|
|
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for your response Imar.
Now, I used a DriveListBox and a DirListBox control because i have .NET Framework 1.0. . They're not really what I want, but i was making do with them when I noticed that they are defined in the code as:
private Microsoft.VisualBasic.Compatibility.VB6.DirListBox
dirListBox1;
private
Microsoft.VisualBasic.Compatibility.VB6.DriveListB ox
driveListBox1;
When i run the below code, error is
"The type or namespace name 'Compatability' does not exist in the class or namespace 'Microsoft.VisualBasic'(are you missing an assembly reference?)"
How do i add assembly reference?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DirectoryPickerDialog1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button1;
private Microsoft.VisualBasic.Compatibility.VB6.DirListBox dirListBox1;
private Microsoft.VisualBasic.Compatibility.VB6.DriveListB ox driveListBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
/// <summary>
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// </summary>
private void InitializeComponent()
{
System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button1 = new System.Windows.Forms.Button();
this.dirListBox1 = new Microsoft.VisualBasic.Compatibility.VB6.DirListBox ();
this.driveListBox1 = new Microsoft.VisualBasic.Compatibility.VB6.DriveListB ox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// openFileDialog1
//
this.openFileDialog1.Filter = "aaa|*|aaaa|*.*";
//
// button1
//
this.button1.Location = new System.Drawing.Point(256, 120);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 24);
this.button1.TabIndex = 0;
this.button1.Text = "Select";
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMous eDown);
//
// dirListBox1
//
this.dirListBox1.IntegralHeight = false;
this.dirListBox1.Location = new System.Drawing.Point(16, 56);
this.dirListBox1.Name = "dirListBox1";
this.dirListBox1.Size = new System.Drawing.Size(200, 144);
this.dirListBox1.TabIndex = 1;
//
// driveListBox1
//
this.driveListBox1.Location = new System.Drawing.Point(16, 16);
this.driveListBox1.Name = "driveListBox1";
this.driveListBox1.Size = new System.Drawing.Size(200, 20);
this.driveListBox1.TabIndex = 2;
this.driveListBox1.SelectedIndexChanged += new System.EventHandler(this.OnDirSelectChanged);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(256, 176);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(392, 19);
this.textBox1.TabIndex = 3;
this.textBox1.Text = ((string)(configurationAppSettings.GetValue("textB ox1.Text", typeof(string))));
//
// label1
//
this.label1.Location = new System.Drawing.Point(256, 152);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 16);
this.label1.TabIndex = 4;
this.label1.Text = "Selected Directory";
//
// DirectoryPickerDialog
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(664, 222);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.textBox1,
this.driveListBox1,
this.dirListBox1,
this.button1});
this.Name = "DirectoryPickerDialog";
this.Text = "DirectoryPickerDialog";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.textBox1.Text = this.dirListBox1.Path;
}
private void OnDirSelectChanged(object sender, System.EventArgs e)
{
try
{
this.dirListBox1.Path = this.driveListBox1.Drive;
}
catch(System.Exception exc)
{
MessageBox.Show(exc.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
Thanks.
|

June 25th, 2003, 08:56 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
You'll need to add references to the Microsoft.VisualBasic.Compatibility and Microsoft.VisualBasic namespaces.
To do this, expand your project, right-click References and choose Add Reference. Then on the .NET tab, select Microsoft Visual Basic .NET runtime and Microsoft Visual Basic .NET Compatibility runtime to reference both these namespaces.
Funny thing is that my version of Visual Studio (2002 and 2003), adds the references automatically when I add an instance of the Dir/Drive listbox. Did you remove them, or is there another problem?
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|

June 26th, 2003, 10:00 PM
|
Registered User
|
|
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for your response Imar.
The code is working perfectly now. Actually I did not add Microsoft.VisualBasic.Compatibility and Microsoft.VisualBasic namespaces in the code, that is why i got such errors.
|

July 2nd, 2003, 06:53 AM
|
Authorized User
|
|
Join Date: May 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Instead of doing this length process you can use the Common Dialog Class for open Dialog Box or save dialog box.
Quote:
quote:Originally posted by mkpriya
Thanks for your response Imar.
Now, I used a DriveListBox and a DirListBox control because i have .NET Framework 1.0. . They're not really what I want, but i was making do with them when I noticed that they are defined in the code as:
private Microsoft.VisualBasic.Compatibility.VB6.DirListBox
dirListBox1;
private
Microsoft.VisualBasic.Compatibility.VB6.DriveListB ox
driveListBox1;
When i run the below code, error is
"The type or namespace name 'Compatability' does not exist in the class or namespace 'Microsoft.VisualBasic'(are you missing an assembly reference?)"
How do i add assembly reference?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DirectoryPickerDialog1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button1;
private Microsoft.VisualBasic.Compatibility.VB6.DirListBox dirListBox1;
private Microsoft.VisualBasic.Compatibility.VB6.DriveListB ox driveListBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
/// <summary>
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// </summary>
private void InitializeComponent()
{
System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button1 = new System.Windows.Forms.Button();
this.dirListBox1 = new Microsoft.VisualBasic.Compatibility.VB6.DirListBox ();
this.driveListBox1 = new Microsoft.VisualBasic.Compatibility.VB6.DriveListB ox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// openFileDialog1
//
this.openFileDialog1.Filter = "aaa|*|aaaa|*.*";
//
// button1
//
this.button1.Location = new System.Drawing.Point(256, 120);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 24);
this.button1.TabIndex = 0;
this.button1.Text = "Select";
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMous eDown);
//
// dirListBox1
//
this.dirListBox1.IntegralHeight = false;
this.dirListBox1.Location = new System.Drawing.Point(16, 56);
this.dirListBox1.Name = "dirListBox1";
this.dirListBox1.Size = new System.Drawing.Size(200, 144);
this.dirListBox1.TabIndex = 1;
//
// driveListBox1
//
this.driveListBox1.Location = new System.Drawing.Point(16, 16);
this.driveListBox1.Name = "driveListBox1";
this.driveListBox1.Size = new System.Drawing.Size(200, 20);
this.driveListBox1.TabIndex = 2;
this.driveListBox1.SelectedIndexChanged += new System.EventHandler(this.OnDirSelectChanged);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(256, 176);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(392, 19);
this.textBox1.TabIndex = 3;
this.textBox1.Text = ((string)(configurationAppSettings.GetValue("textB ox1.Text", typeof(string))));
//
// label1
//
this.label1.Location = new System.Drawing.Point(256, 152);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 16);
this.label1.TabIndex = 4;
this.label1.Text = "Selected Directory";
//
// DirectoryPickerDialog
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(664, 222);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.textBox1,
this.driveListBox1,
this.dirListBox1,
this.button1});
this.Name = "DirectoryPickerDialog";
this.Text = "DirectoryPickerDialog";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.textBox1.Text = this.dirListBox1.Path;
}
private void OnDirSelectChanged(object sender, System.EventArgs e)
{
try
{
this.dirListBox1.Path = this.driveListBox1.Drive;
}
catch(System.Exception exc)
{
MessageBox.Show(exc.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
Thanks.
|
Deepesh Jain
VB,VBA & .NET Specialist
Wiley Support Team
|

July 2nd, 2003, 07:24 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hmmmm, is that true? If I am not mistaken, you then end up with a dialog box that allows you to select a file, and not a folder. I think the original poster meant a solution that allows a user to browser for just a folder in a read-only tree.
Or am I overlooking something??
Imar
Quote:
quote:Originally posted by Deepesh_Jain
Instead of doing this length process you can use the Common Dialog Class for open Dialog Box or save dialog box.
|
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|
 |