在 Windows Forms 中使用 saveFileDialog、openFileDialog、folderBrowserDialog 的简单示例
2011-11-23 22:02 音乐让我说 阅读(955) 评论(0) 编辑 收藏 举报1. 界面如下:
2. Partial 的代码如下:
partial class FileSelected { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.panel1 = new System.Windows.Forms.Panel(); this.button2 = new System.Windows.Forms.Button(); this.label6 = new System.Windows.Forms.Label(); this.txtPath3 = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.btnSubmit = new System.Windows.Forms.Button(); this.label4 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.txtPath2 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.btnBrowser = new System.Windows.Forms.Button(); this.txtPath = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); this.btnClose = new System.Windows.Forms.Button(); this.panel1.SuspendLayout(); this.SuspendLayout(); // // panel1 // this.panel1.Controls.Add(this.btnClose); this.panel1.Controls.Add(this.button2); this.panel1.Controls.Add(this.label6); this.panel1.Controls.Add(this.txtPath3); this.panel1.Controls.Add(this.label5); this.panel1.Controls.Add(this.btnSubmit); this.panel1.Controls.Add(this.label4); this.panel1.Controls.Add(this.button1); this.panel1.Controls.Add(this.txtPath2); this.panel1.Controls.Add(this.label3); this.panel1.Controls.Add(this.label2); this.panel1.Controls.Add(this.btnBrowser); this.panel1.Controls.Add(this.txtPath); this.panel1.Controls.Add(this.label1); this.panel1.Location = new System.Drawing.Point(2, 1); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(781, 421); this.panel1.TabIndex = 0; // // button2 // this.button2.Location = new System.Drawing.Point(679, 136); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 12; this.button2.Text = "浏览..."; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // label6 // this.label6.AutoSize = true; this.label6.ForeColor = System.Drawing.Color.Red; this.label6.Location = new System.Drawing.Point(175, 186); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(197, 12); this.label6.TabIndex = 11; this.label6.Text = "提示:如果目录找不到,则会报错。"; // // txtPath3 // this.txtPath3.Location = new System.Drawing.Point(177, 138); this.txtPath3.Name = "txtPath3"; this.txtPath3.Size = new System.Drawing.Size(489, 21); this.txtPath3.TabIndex = 10; // // label5 // this.label5.AutoSize = true; this.label5.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label5.Location = new System.Drawing.Point(10, 145); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(147, 14); this.label5.TabIndex = 9; this.label5.Text = "请选择目录所在路径:"; // // btnSubmit // this.btnSubmit.Location = new System.Drawing.Point(224, 316); this.btnSubmit.Name = "btnSubmit"; this.btnSubmit.Size = new System.Drawing.Size(75, 23); this.btnSubmit.TabIndex = 8; this.btnSubmit.Text = "确定"; this.btnSubmit.UseVisualStyleBackColor = true; // // label4 // this.label4.AutoSize = true; this.label4.ForeColor = System.Drawing.Color.Red; this.label4.Location = new System.Drawing.Point(175, 111); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(197, 12); this.label4.TabIndex = 7; this.label4.Text = "提示:如果文件找不到,则会报错。"; // // button1 // this.button1.Location = new System.Drawing.Point(679, 69); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 6; this.button1.Text = "浏览..."; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // txtPath2 // this.txtPath2.Location = new System.Drawing.Point(177, 71); this.txtPath2.Name = "txtPath2"; this.txtPath2.Size = new System.Drawing.Size(489, 21); this.txtPath2.TabIndex = 5; // // label3 // this.label3.AutoSize = true; this.label3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label3.Location = new System.Drawing.Point(10, 78); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(147, 14); this.label3.TabIndex = 4; this.label3.Text = "请选择文件所在路径:"; // // label2 // this.label2.AutoSize = true; this.label2.ForeColor = System.Drawing.Color.Red; this.label2.Location = new System.Drawing.Point(175, 44); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(389, 12); this.label2.TabIndex = 3; this.label2.Text = "提示:如果文件存在,则会覆盖文件。如果不存在,则会自动创建文件。"; // // btnBrowser // this.btnBrowser.Location = new System.Drawing.Point(679, 11); this.btnBrowser.Name = "btnBrowser"; this.btnBrowser.Size = new System.Drawing.Size(75, 23); this.btnBrowser.TabIndex = 2; this.btnBrowser.Text = "浏览..."; this.btnBrowser.UseVisualStyleBackColor = true; this.btnBrowser.Click += new System.EventHandler(this.btnBrowser_Click); // // txtPath // this.txtPath.Location = new System.Drawing.Point(177, 11); this.txtPath.Name = "txtPath"; this.txtPath.Size = new System.Drawing.Size(489, 21); this.txtPath.TabIndex = 1; // // label1 // this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label1.Location = new System.Drawing.Point(10, 16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(161, 14); this.label1.TabIndex = 0; this.label1.Text = "请选择文件保存的路径:"; // // saveFileDialog1 // this.saveFileDialog1.DefaultExt = "txt"; this.saveFileDialog1.Filter = "文本文件|*.txt|所有文件|*.*"; this.saveFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.saveFileDialog1_FileOk); // // openFileDialog1 // this.openFileDialog1.DefaultExt = "txt"; this.openFileDialog1.Filter = "文本文件|*.txt|所有文件|*.*"; this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk); // // folderBrowserDialog1 // this.folderBrowserDialog1.Description = "HelloMyWorld"; // // btnClose // this.btnClose.Location = new System.Drawing.Point(378, 316); this.btnClose.Name = "btnClose"; this.btnClose.Size = new System.Drawing.Size(75, 23); this.btnClose.TabIndex = 13; this.btnClose.Text = "关闭"; this.btnClose.UseVisualStyleBackColor = true; this.btnClose.Click += new System.EventHandler(this.btnClose_Click); // // FileSelected // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(784, 422); this.Controls.Add(this.panel1); this.Name = "FileSelected"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "文件选择"; this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Button btnBrowser; private System.Windows.Forms.TextBox txtPath; private System.Windows.Forms.Label label1; private System.Windows.Forms.SaveFileDialog saveFileDialog1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox txtPath2; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button btnSubmit; private System.Windows.Forms.Label label4; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1; private System.Windows.Forms.Label label5; private System.Windows.Forms.TextBox txtPath3; private System.Windows.Forms.Label label6; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button btnClose; }
3. 后台代码如下:
public partial class FileSelected : Form { public FileSelected() { InitializeComponent(); } #region 第一个浏览 private void btnBrowser_Click(object sender, EventArgs e) { var result = this.saveFileDialog1.ShowDialog(); } private void saveFileDialog1_FileOk(object sender, CancelEventArgs e) { this.txtPath.Text = this.saveFileDialog1.FileName; } #endregion #region 第二个浏览 private void button1_Click(object sender, EventArgs e) { this.openFileDialog1.ShowDialog(); } private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { this.txtPath2.Text = this.openFileDialog1.FileName; } #endregion #region 第三个浏览 private void button2_Click(object sender, EventArgs e) { var result = this.folderBrowserDialog1.ShowDialog(); if(result == System.Windows.Forms.DialogResult.OK) { this.txtPath3.Text = this.folderBrowserDialog1.SelectedPath; } } #endregion private void btnClose_Click(object sender, EventArgs e) { this.Close(); } }
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。