先读取这个目录下有哪些文件,将文碧袜件名放在一个数组 或者集合啊都行。悔中激然培激后在循环读取,因为集合中的每一个元素都是一个文件名。。这样不就ok啦。。你觉得呢
using Systemusing System.Collections.Generic
using System.ComponentModel
using System.Data
using System.Drawing
using System.Text
using System.IO
using System.Windows.Forms
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent()
}
/// <summary>
/// 当某个选项卡被取消选中时
/// </summary>
private void tabControl1_Deselected(object sender, TabControlEventArgs e)
{
e.TabPage.Text = e.TabPage.Text.TrimStart('*')
}
/// <summary>
/// 当某个选项卡被选中时
/// </summary>
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
e.TabPage.Text = "*" + e.TabPage.Text
}
/// <summary>
/// 打开文件
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//取得当前的选项卡数目
int tabPageCount = this.tabControl1.TabPages.Count
//取得欲打桐老开的文件路径
string filePath = this.openFileDialog1.FileName
//取得欲打晌腔开的文件名
string fileName = Path.GetFileName(filePath)
//分析是否为txt格式的文件
if (Path.GetExtension(filePath).ToLower() != ".txt")
{
MessageBox.Show("非法文件格式!","打开文件失败",MessageBoxButtons.OK,MessageBoxIcon.Stop)
return
}
//采用"page"加上当前的选项卡数目加1做为选项卡的名称
string currentPageName = "page" + (tabPageCount + 1).ToString()
//采用"richTextBox"加上当前的选项局谨升卡数目加1做为选项卡中包含RichTextBox的名称
string currentTxtName = "richTextBox" + (tabPageCount + 1).ToString()
//添加一个新的选项卡,并指定其选项卡显示文字为当前打开的文件名
this.tabControl1.TabPages.Add(currentPageName,fileName)
//实例化一个新的RichTextBox
RichTextBox ricTxt = new RichTextBox()
//设置其名称,布局等属性
ricTxt.Name = currentTxtName
ricTxt.Dock = DockStyle.Fill
//ricTxt.TextChanged +=
//将新这个新的RichTextBox对象添加到当前新建的选项卡中
this.tabControl1.TabPages[currentPageName].Controls.Add(ricTxt)
//将文件加载到当前的RichTextBox
ricTxt.LoadFile(filePath,RichTextBoxStreamType.PlainText)
}
}
private void changePageText(string currentPageName)
{
this.tabControl1.TabPages[currentPageName].Text = "*" + currentPageName
}
}
}
namespace WindowsApplication2
{
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.tabControl1 = new System.Windows.Forms.TabControl()
this.button1 = new System.Windows.Forms.Button()
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog()
this.SuspendLayout()
//
// tabControl1
//
this.tabControl1.Location = new System.Drawing.Point(0, -3)
this.tabControl1.Name = "tabControl1"
this.tabControl1.SelectedIndex = 0
this.tabControl1.Size = new System.Drawing.Size(540, 510)
this.tabControl1.TabIndex = 0
this.tabControl1.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl1_Selected)
this.tabControl1.Deselected += new System.Windows.Forms.TabControlEventHandler(this.tabControl1_Deselected)
//
// button1
//
this.button1.Location = new System.Drawing.Point(22, 509)
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)
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1"
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F)
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
this.ClientSize = new System.Drawing.Size(539, 540)
this.Controls.Add(this.button1)
this.Controls.Add(this.tabControl1)
this.Name = "Form1"
this.Text = "Form1"
this.ResumeLayout(false)
}
#endregion
private System.Windows.Forms.TabControl tabControl1
private System.Windows.Forms.Button button1
private System.Windows.Forms.OpenFileDialog openFileDialog1
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)