Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace LiestBox_1 { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); //AllowSelection 指示是否启用了列表项的选择 //BorderStyle ListBox 绘制边框类型 //ColumnWidth 列的宽度 //DataSource 数据源 //HorizintalScrollbar 是否显示水平滚动条 //Items 项 //SelectedIndex 索引 //SelectedIndices 当前项从 0开始的索引项 //SelectedItems 获取包含 ListBox 中当前选定项 //SelectedItems 当前选定项的集合 //SelectedValue 成员属性 值 //Sorted 是否按字母顺序排序 //ClearSelected 取消所有选择 //FindString 查找ListBox中 以指定字符串开头的第一个项 //FindStringExact 第一个精确匹配指定字符串的项 //GetItemText 返回指定项 的文本形式 //SetSelected 选择或清除对 ListBox 中指定项的选定 //Sort 对ListBox 中的项 排序 //SelectedIndexChanged 在SelectedIndex 属性更改后发生 //SelectedValueChanged 在SelectedValue 属性更改时发生 } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace LiestBox_1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //HorizontalScrollbar 使其能水平方向滚动条 listBox1.HorizontalScrollbar = true; //ScrollAlwaysVisible =true 水质方向 滚动条 listBox1.ScrollAlwaysVisible = true; //SelectionMode 设置为 SelectedMode 枚举成员 MultiExtended 实现选择多项 listBox1.SelectionMode = SelectionMode.MultiExtended; } private void button1_Click(object sender, EventArgs e) { FolderBrowserDialog folderbrowser = new FolderBrowserDialog(); //实例化对话框 if(folderbrowser.ShowDialog()==DialogResult.OK) { textBox1.Text = folderbrowser.SelectedPath;//获取选择的文件夹路径 DirectoryInfo dinfo = new DirectoryInfo(textBox1.Text); FileSystemInfo[] finfo = dinfo.GetFileSystemInfos();//获取指定文件夹下的所有子文件夹及文件 listBox1.Items.AddRange(finfo); label3.Text="("+listBox1.Items.Count+"项)"; //显示文件夹中的项数 } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { label4.Text = "您选择的是: "; for(int i=0;i<listBox1.SelectedItems.Count;i++) //循环遍历选择的多项 { label4.Text += listBox1.SelectedItems[i] + ","; } } } }
namespace LiestBox_1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 31);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(71, 12);
this.label1.TabIndex = 0;
this.label1.Text = "选择文件夹:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(277, 26);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "选择";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(13, 63);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(107, 12);
this.label2.TabIndex = 3;
this.label2.Text = "文件夹中文件列表:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(126, 63);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(0, 12);
this.label3.TabIndex = 4;
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(51, 91);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(327, 136);
this.listBox1.TabIndex = 5;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(13, 239);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(0, 12);
this.label4.TabIndex = 6;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(91, 27);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(180, 21);
this.textBox1.TabIndex = 7;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(401, 270);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label4);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "文件查看选择";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox1;
}
}