Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 C# C#4.0
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.
 
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 25-Feb-13 1:08am
Edited 26-Feb-13 2:30am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

hi,
 
now try this code,
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
  Permalink  
v3
Comments
Darthillian at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Eventually I used the code from SoMad but adjusted it a little bit Wink | ;) Still loads of thanks man Big Grin | :-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";            
 
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hi,
try this..
 
add namespace
 
using System.IO;
 

string[] Filelistarray = Directory.GetFiles(@"D:\", "*.txt");
 
foreach (string FileName in Filelistarray)
{
    textBox1.Text += FileName;
}
  Permalink  
Comments
Darthillian at 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)

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100