2013-05-11 66 views
0

对于C#来说,我很新,我想要一个包含文件夹中所有文本文件的列表框,并且如果用户在列出的文件上进行配音,文件将显示在文本框中。C#在列表框中列出文本文件,并让用户在文本框中打开它们

我不想使用openFileDialog函数,因为文本文件位于使用username:[email protected]/folder访问的Web服务器上。

有点像一个texteditor限于编辑1个文件夹中的文件:)
如果这是可能的使用openFileDialog,请告诉我如何。

我希望你能理解我想要做什么。

问候,

+0

这很简单,列出ListBox或ListView中文件夹中的所有文本文件并处理MouseDoubleClick来读取内容并将其显示在TextBox中,那么问题是什么? – Bolu 2013-05-11 21:07:31

回答

2

从我了解你之后,你要通过的文件在一个特定的目录遍历,然后让他们进行编辑与一次在列表框中双击打开。

这可以通过使用var Files = Directory.GetFiles("path", ".txt");

Files将文件名的string[]来完成。

ListBox lbx = new ListBox(); 
lbx.Size = new System.Drawing.Size(X,Y); //Set to desired Size. 
lbx.Location = new System.Drawing.Point(X,Y); //Set to desired Location. 
this.Controls.Add(listBox1); //Add to the window control list. 
lbx.DoubleClick += OpenFileandBeginEditingDelegate; 
lbx.BeginUpdate(); 
for(int i = 0; i < numfiles; i++) 
    lbx.Items.Add(Files[i]); 
lbx.EndUpdate(); 

现在你的事件委托应该是这个样子:

OpenFileandBeginEditingDelegate(object sender, EventArgs e) 
{ 
    string file = lbx.SelectedItem.ToString(); 
    FileStream fs = new FileStream(Path + file, FileMode.Open); 

    //Now add this to the textbox 
    byte[] b = new byte[1024]; 
    UTF8Encoding temp = new UTF8Encoding(true); 
    while (fs.Read(b,0,b.Length) > 0) 
    { 
     tbx.Text += temp.GetString(b);//tbx being the textbox you want to use as the editor. 
    } 
} 

我们通过VS编辑窗口添加事件处理程序

然后用这些文件是这样的填充列表框点击相关控件并转到该控件的属性窗格。您将需要切换到事件窗格并滚动,直到找到DoubleClick事件,如果您使用该设计器应该自动插入有效的委托签名并允许您为该事件编写逻辑。

+0

谢谢你,我花了一段时间才把它与我的代码一起工作,但现在它的工作;) – 2013-05-11 22:04:07

+0

我知道这是一个旧帖子,但谢谢。 – 2013-07-09 22:44:37