Click here to Skip to main content
15,867,765 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I would like that when I click on a button called "Dutch" that it loads in all the files in a specified path and show them in a drop down list where you can chose 1 of those files that it then loads into a fixed textbox.
The problem is that when I try this, it doesn't show me the file names in the drop down list at all.
I do use a custom control I got from the website : ButtonDropDown using Custom Control (ButtonDropDownMenu Control)[^]
But I don't know how to implement the automatic loading of those text files.
So what I need is a way to automaticly load the file-names in the drop down menu and when I click on one it loads it into a text box.

This is what I got so far.

C#
private void Dutch_Click(object sender, EventArgs e)
        {
            string[] dirs = Directory.GetFileSystemEntries(@"C:\Users\a961835\Programming","*.txt");
            foreach (string dir in dirs)
            {
                textBox1.Text = dir;
            }
            //string s = File.ReadAllText(@"C:\Users\a961835\Programming");
            //textBox1.Text = s;
        }
Posted
Updated 26-Feb-13 1:30am
v2

hi,

now try this code,
C#
string[] Filelistarray = Directory.GetFiles(@"D:\", "*.txt");
DataTable dt = new DataTable();
dt.Columns.Add("FileName");
foreach (string FileName in Filelistarray)
{
    DataRow dr = dt.NewRow();
    string filenameWithoutPath = Path.GetFileName(FileName);
    dr["FileName"] = filenameWithoutPath;
    dt.Rows.Add(dr);
}

DropDownList1.DataSource = dt;
DropDownList1.DataBind();

regards,
Prakash.T
 
Share this answer
 
v3
Comments
Nomardus 26-Feb-13 7:54am    
I get an error on the comboBox1.DataBind();
This is my version of DropDownList1.DataBind();
Also when I compile it with the comboBox1.Databind(); in comment I get something in return and it says System.Data.DataRowView
Eventually I used the code from SoMad but adjusted it a little bit ;) Still loads of thanks man :D

private void Dutch_Click(object sender, EventArgs e)
        {
            
            string[] Filelistarray = Directory.GetFiles(@"C:\Users\a961835\Programming", "*.txt");
            DataTable dt = new DataTable();
            dt.Columns.Add("FileName");
            foreach (string FileName in Filelistarray)
            {
                DataRow dr = dt.NewRow();
                string filenameWithoutPath = Path.GetFileName(FileName);
                dr["FileName"] = filenameWithoutPath;
                dt.Rows.Add(dr);
            }

            DropDownList.DataSource = dt;            
            DropDownList.DisplayMember = "FileName";            

        }
 
Share this answer
 
hi,
try this..

add namespace

using System.IO;


C#
string[] Filelistarray = Directory.GetFiles(@"D:\", "*.txt");

foreach (string FileName in Filelistarray)
{
    textBox1.Text += FileName;
}
 
Share this answer
 
Comments
Nomardus 25-Feb-13 6:33am    
This isn't bad but doesn't really help since I have tried this already, and sorry for the confusion but it show it in the drop down menu and not the text box, also I should only show the file name and not the entire path.
Still thanks for the effort.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900