大神们,openfolderdialogbrowsedialog怎么用的啊?求教啊!!

23:13 提问
C#Windows窗体程序,我添加了皮肤之后,FolderBrowserDialog.ShowDialog弹不出来?
C#Windows窗体程序,我添加了皮肤之后,FolderBrowserDialog.ShowDialog弹不出来?怎么回事,求大神解答
按赞数排序
在线等,求大神!!!
其他相关推荐中国领先的IT技术网站
51CTO旗下网站
使用FolderBrowserDialog控件
《Visual Basic 2010入门经典(第6版)》第8章显示对话框,本章将深入研究这些对话框,说明如何在Visual Basic 2010应用程序中使用它们,为用户构建具有专业外观的应用程序。本节为大家介绍使用FolderBrowserDialog控件。
作者:吴伟敏/李周芳 译来源:清华大学出版社| 20:01
8.7.2& 使用FolderBrowserDialog控件
在显示Browse For Folder对话框之前,先设置一些基本的属性。下面的代码片段设置了3个主要属性。其中,第1个属性Description允许为用户提供描述或说明。
第2个属性RootFolder指定Browse For Folder对话框的启动文件夹。该属性的值是Environment. SpecialFolder枚举中的一个常量。通常使用MyComputer常量来指定应从My Computer(我的电脑)处开始浏览文件夹,有时也使用My Documents常量来指定从My Documents(我的文档)处开始浏览文件夹。
第3个属性是ShowNewFolderButton。该属性的默认值为True,表示应该显示Make New Folder按钮。如果不想显示该按钮,可将该属性的值设置为False:With&FolderBrowserDialog1 &.Description&=&"Select&a&backup&folder"&.RootFolder&=&Environment.SpecialFolder.MyComputer &.ShowNewFolderButton&=&False&End&With &
设置了必要的属性之后,就执行ShowDialog方法,显示Browse For Folder对话框:FolderBrowserDialog1.ShowDialog()&
FolderBrowserDialog控件将返回值为OK或Cancel的DialogResult。因此,可以在If…End If代码块中使用前面的语句,测试DialogResult是否为OK,其操作与前面几个示例一样。
若要检索用户所选择的文件夹,只需检索SelectedPath属性中的值集并将其赋给一个变量即可。所返回的文件夹有一个完全限定的路径名称。例如,如果在C盘根目录下选择了一个名为Temp的文件夹,那么返回的路径就是C:\Temp:
strFolder = FolderBrowserDialog1.SelectedPath
试一试:&使用FolderBrowserDialog控件
Windows Forms Dialogs.zip
下面的示例将继续Windows Forms Dialogs项目,并且通过FolderBrowserDialog控件显示Browse For Folder对话框。如果该对话框返回的DialogResult为OK,就在窗体的文本框中显示所选的文件夹。
(1) 返回Windows Forms Dialog项目中的窗体设计器。
(2) 通过工具箱在窗体的Print按钮的下方添加另一个Button控件,设置其属性,如下所示:
将Name属性设置为btnBrowse
将Anchor属性设置为Top,Right
将Location属性设置为349,163
将Text属性设置为Browse
(3) 从工具箱给项目添加一个FolderBrowserDialog控件,该控件会添加到窗体下面的工作区中。接受该控件的所有默认属性,因为后面将在代码中设置必要的属性。
(4) 双击Browse按钮,打开该按钮的Click事件过程,添加如下所示的代码:Private&Sub&btnBrowse_Click(ByVal&sender&As&System.Object,&_ &ByVal&e&As&System.EventArgs)&Handles&btnBrowse.Click &'Set&the&FolderBrowser&dialog&properties &With&FolderBrowserDialog1 &.Description&=&"Select&a&backup&folder"&.RootFolder&=&Environment.SpecialFolder.MyComputer &.ShowNewFolderButton&=&False&End&With &'Show&the&FolderBrowser&dialog&and&if&the&user&clicks&the &'OK&button,&display&the&selected&folder &If&FolderBrowserDialog1.ShowDialog&=&Windows.Forms.DialogResult.OK&Then &txtFile.Text&=&FolderBrowserDialog1.SelectedPath &End&If &End&Sub &
(5) 以上是需要添加的所有代码。要测试对该项目所做的修改,请单击工具栏上的Start按钮。
(6) 当显示窗体时,单击Browse按钮,会看到一个如图8-16所示的Browse For Folder对话框。
(7) 下面浏览计算机并选择一个文件夹。单击OK按钮后,所选的文件夹将会显示在窗体的文本框中。注意,返回的文件夹包含一个完全限定的路径名。
在显示Browse For Folder对话框之前,需要为FolderBrowserDialog控件设置一些基本属性,以定制该对话框的外观。首先设置Description属性,为用户提供一些基本的说明信息。然后选择Browse For Folder对话框中的根文件夹。本例使用MyComputer常量,以显示计算机上的所有盘符,如图8-16所示。最后,将ShowNewFolderButton属性设置为False,这样在对话框中就不会显示Make New Folder按钮了:'Set&the&FolderBrowser&dialog&properties &With&FolderBrowserDialog1 &.Description&=&&Select&a&backup&folder&&.RootFolder&=&Environment.SpecialFolder.MyComputer &.ShowNewFolderButton&=&False&End&With &
接着通过If…End If代码块来显示对话框,检查由FolderBrowserDialog控件的ShowDialog方法返回的DialogResult。在If…End If代码块中,使用SelectedPath属性添加在窗体文本框中显示所选文件夹的必要代码:'Show&the&FolderBrowser&dialog&and&if&the&user&clicks&the &'OK&button,&display&the&selected&folder &If&FolderBrowserDialog1.ShowDialog&=&Windows.Forms.DialogResult.OK&Then &txtFile.Text&=&FolderBrowserDialog1.SelectedPath &End&If &
【责任编辑: TEL:(010)】&&&&&&
大家都在看猜你喜欢
热点热点头条头条热点
24H热文一周话题本月最赞
讲师:41573人学习过
讲师:145778人学习过
讲师:13784人学习过
精选博文论坛热帖下载排行
《程序员密码学》涉及密码学的各个研究方向,分组密码、散列函数、公钥密码以及相关的攻击,同时也讲解了密码学算法实现上常用的ASN.编码、...
订阅51CTO邮刊FolderBrowserDialog 类 (System.Windows.Forms)
不是 IT 专业人员?
TechNet 库
FolderBrowserDialog 类
Visual Studio 2008
更新:2007 年 11 月提示用户选择文件夹。无法继承此类。 命名空间:
System.Windows.Forms(在 System.Windows.Forms.dll 中)
public sealed class FolderBrowserDialog : CommonDialog
public final class FolderBrowserDialog extends CommonDialog
public final class FolderBrowserDialog extends CommonDialog
此类提供一种方法,它提示用户浏览、创建并最终选择一个文件夹。如果只允许用户选择文件夹而非文件,则可使用此类。文件夹的浏览通过树控件完成。只能选择文件系统中的文件夹;不能选择虚拟文件夹。通常在创建新 FolderBrowserDialog 后,将
设置为开始浏览的位置。或者,可将
设置为最初选定的
子文件夹的绝对路径。也可以选择设置
属性为用户提供附加说明。最后,调用
方法将对话框显示给用户。如果该对话框关闭并且
显示的对话框为 DialogResult.OK, 则是一个包含选定文件夹路径的字符串。如果用户可通过“新建文件夹”按钮创建新文件夹,则可对控件使用
属性。FolderBrowserDialog 是有模式对话框,因此,在显示时,它会阻止应用程序剩余部分的运行,直到用户选定了文件夹。当对话框有模式显示时,不能进行任何输入(通过键盘或鼠标单击),对对话框上的对象的输入除外。在对调用程序进行输入之前,程序必须隐藏或关闭对话框(通常是响应某一用户操作)。下面的代码示例创建一个应用程序,用户使用该应用程序可以打开
控件中的多格式文本 (.rtf) 文件。
// The following example displays an application that provides the ability to
// open rich text files (rtf) into the RichTextBox. The example demonstrates
// using the FolderBrowserDialog to set the default directory for opening files.
// The OpenFileDialog class is used to open the file.
using System.D
using System.Windows.F
using System.IO;
public class FolderBrowserDialogExampleForm : System.Windows.Forms.Form
private FolderBrowserDialog folderBrowserDialog1;
private OpenFileDialog openFileDialog1;
private RichTextBox richTextBox1;
private MainMenu mainMenu1;
private MenuItem fileMenuItem, openMenuI
private MenuItem folderMenuItem, closeMenuI
private string openFileName, folderN
private bool fileOpened = false;
// The main entry point for the application.
static void Main()
Application.Run(new FolderBrowserDialogExampleForm());
// Constructor.
public FolderBrowserDialogExampleForm()
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.fileMenuItem = new System.Windows.Forms.MenuItem();
this.openMenuItem = new System.Windows.Forms.MenuItem();
this.folderMenuItem = new System.Windows.Forms.MenuItem();
this.closeMenuItem = new System.Windows.Forms.MenuItem();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.mainMenu1.MenuItems.Add(this.fileMenuItem);
this.fileMenuItem.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] {this.openMenuItem,
this.closeMenuItem,
this.folderMenuItem});
this.fileMenuItem.Text = "File";
this.openMenuItem.Text = "Open...";
this.openMenuItem.Click += new System.EventHandler(this.openMenuItem_Click);
this.folderMenuItem.Text = "Select Directory...";
this.folderMenuItem.Click += new System.EventHandler(this.folderMenuItem_Click);
this.closeMenuItem.Text = "Close";
this.closeMenuItem.Click += new System.EventHandler(this.closeMenuItem_Click);
this.closeMenuItem.Enabled = false;
this.openFileDialog1.DefaultExt = "rtf";
this.openFileDialog1.Filter = "rtf files (*.rtf)|*.rtf";
// Set the help text description for the FolderBrowserDialog.
this.folderBrowserDialog1.Description =
"Select the directory that you want to use as the default.";
// Do not allow the user to create new files via the FolderBrowserDialog.
this.folderBrowserDialog1.ShowNewFolderButton = false;
// Default to the My Documents folder.
this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.P
this.richTextBox1.AcceptsTab = true;
this.richTextBox1.Location = new System.Drawing.Point(8, 8);
this.richTextBox1.Size = new System.Drawing.Size(280, 344);
this.richTextBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left |
AnchorStyles.Bottom | AnchorStyles.R
this.ClientSize = new System.Drawing.Size(296, 360);
this.Controls.Add(this.richTextBox1);
this.Menu = this.mainMenu1;
this.Text = "RTF Document Browser";
// Bring up a dialog to open a file.
private void openMenuItem_Click(object sender, System.EventArgs e)
// If a file is not opened, then set the initial directory to the
// FolderBrowserDialog.SelectedPath value.
if (!fileOpened) {
openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedP
openFileDialog1.FileName = null;
// Display the openFile dialog.
DialogResult result = openFileDialog1.ShowDialog();
// OK button was pressed.
if(result == DialogResult.OK)
openFileName = openFileDialog1.FileN
// Output the requested file in richTextBox1.
Stream s = openFileDialog1.OpenFile();
richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText);
s.Close();
fileOpened = true;
catch(Exception exp)
MessageBox.Show("An error occurred while attempting to load the file. The error is:"
+ System.Environment.NewLine + exp.ToString() + System.Environment.NewLine);
fileOpened = false;
Invalidate();
closeMenuItem.Enabled = fileO
// Cancel button was pressed.
else if(result == DialogResult.Cancel)
// Close the current file.
private void closeMenuItem_Click(object sender, System.EventArgs e)
richTextBox1.Text = "";
fileOpened = false;
closeMenuItem.Enabled = false;
// Bring up a dialog to chose a folder path in which to open or save a file.
private void folderMenuItem_Click(object sender, System.EventArgs e)
// Show the FolderBrowserDialog.
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
folderName = folderBrowserDialog1.SelectedP
if(!fileOpened)
// No file is opened, bring up openFileDialog in selected path.
openFileDialog1.InitialDirectory = folderN
openFileDialog1.FileName = null;
openMenuItem.PerformClick();
// The following example displays an application that provides the ability to
// open rich text files (rtf) into the RichTextBox. The example demonstrates
// using the FolderBrowserDialog to set the default directory for opening files.
// The OpenFileDialog class is used to open the file.
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;
import System.IO.*;
public class FolderBrowserDialogExampleForm extends System.Windows.Forms.Form
private FolderBrowserDialog folderBrowserDialog1;
private OpenFileDialog openFileDialog1;
private RichTextBox richTextBox1;
private MainMenu mainMenu1;
private MenuItem fileMenuItem, openMenuI
private MenuItem folderMenuItem, closeMenuI
private String openFileName, folderN
private boolean fileOpened =
// The main entry point for the application.
public static void main(String[] args)
Application.Run(new FolderBrowserDialogExampleForm());
// Constructor.
public FolderBrowserDialogExampleForm()
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.fileMenuItem = new System.Windows.Forms.MenuItem();
this.openMenuItem = new System.Windows.Forms.MenuItem();
this.folderMenuItem = new System.Windows.Forms.MenuItem();
this.closeMenuItem = new System.Windows.Forms.MenuItem();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.folderBrowserDialog1 =
new System.Windows.Forms.FolderBrowserDialog();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.mainMenu1.get_MenuItems().Add(this.fileMenuItem);
this.fileMenuItem.get_MenuItems().AddRange(
new System.Windows.Forms.MenuItem[] { this.openMenuItem,
this.closeMenuItem, this.folderMenuItem });
this.fileMenuItem.set_Text("File");
this.openMenuItem.set_Text("Open...");
this.openMenuItem.add_Click(new System.EventHandler(
this.openMenuItem_Click));
this.folderMenuItem.set_Text("Select Directory...");
this.folderMenuItem.add_Click(new System.EventHandler(
this.folderMenuItem_Click));
this.closeMenuItem.set_Text("Close");
this.closeMenuItem.add_Click(new System.EventHandler(
this.closeMenuItem_Click));
this.closeMenuItem.set_Enabled(false);
this.openFileDialog1.set_DefaultExt("rtf");
this.openFileDialog1.set_Filter("rtf files (*.rtf)|*.rtf");
// Set the help text description for the FolderBrowserDialog.
this.folderBrowserDialog1.set_Description("Select the directory "
+ "that you want to use as the default.");
// Do not allow the user to create new files via the
// FolderBrowserDialog.
this.folderBrowserDialog1.set_ShowNewFolderButton(false);
// Default to the My Documents folder.
this.folderBrowserDialog1.set_RootFolder(
Environment.SpecialFolder.Personal);
this.richTextBox1.set_AcceptsTab(true);
this.richTextBox1.set_Location(new System.Drawing.Point(8, 8));
this.richTextBox1.set_Size(new System.Drawing.Size(280, 344));
this.richTextBox1.set_Anchor(AnchorStyles.Top | AnchorStyles.Left
| AnchorStyles.Bottom | AnchorStyles.Right);
this.set_ClientSize(new System.Drawing.Size(296, 360));
this.get_Controls().Add(this.richTextBox1);
this.set_Menu(this.mainMenu1);
this.set_Text("RTF Document Browser");
} //FolderBrowserDialogExampleForm
// Bring up a dialog to open a file.
private void openMenuItem_Click(Object sender, System.EventArgs e)
// If a file is not opened, then set the initial directory to the
// FolderBrowserDialog.SelectedPath value.
if (!(fileOpened)) {
openFileDialog1.set_InitialDirectory(
folderBrowserDialog1.get_SelectedPath());
openFileDialog1.set_FileName(null);
// Display the openFile dialog.
DialogResult result = openFileDialog1.ShowDialog();
// OK button was pressed.
if (result.Equals(get_DialogResult().OK)) {
openFileName = openFileDialog1.get_FileName();
// Output the requested file in richTextBox1.
Stream s = openFileDialog1.OpenFile();
richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText);
s.Close();
fileOpened =
catch (System.Exception exp) {
MessageBox.Show("An error occurred while attempting to "
+ "load the file. The error is:"
+ System.Environment.get_NewLine()
+ exp.ToString() + System.Environment.get_NewLine());
fileOpened =
Invalidate();
closeMenuItem.set_Enabled(fileOpened);
// Cancel button was pressed.
if (result.Equals(get_DialogResult().Cancel)) {
} //openMenuItem_Click
// Close the current file.
private void closeMenuItem_Click(Object sender, System.EventArgs e)
richTextBox1.set_Text("");
fileOpened =
closeMenuItem.set_Enabled(false);
} //closeMenuItem_Click
// Bring up a dialog to chose a folder path in which to open or save a file.
private void folderMenuItem_Click(Object sender, System.EventArgs e)
// Show the FolderBrowserDialog.
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result.Equals(get_DialogResult().OK)) {
folderName = folderBrowserDialog1.get_SelectedPath();
if (!(fileOpened)) {
// No file is opened, bring up openFileDialog in selected path.
openFileDialog1.set_InitialDirectory(folderName);
openFileDialog1.set_FileName(null);
openMenuItem.PerformClick();
} //folderMenuItem_Click
} //FolderBrowserDialogExampleForm
System.Windows.Forms.FolderBrowserDialog此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见。 受以下版本支持:3.5、3.0、2.0、1.1
此页面有帮助吗?
更多反馈?
1500 个剩余字符
我们非常感谢您的反馈。
页面加载速度够快吗?
您喜欢网页的设计吗?
请告诉我们更多意见→ FolderBrowserDialog 如何实现打开时直接打开它上次打开的目录呢?
&&共有<b style="color:#ff人关注过本帖主题:FolderBrowserDialog 如何实现打开时直接打开它上次打开的目录呢?

我要回帖

更多关于 dialog.rootfolder 的文章

 

随机推荐