Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a custom combobox (www.codeproject.com/Articles/205358/ButtonDropDownMenuControl) control that I have filled with some filenames from a listarray, but now I would like it if I clicked on a filename that the source file loads into the textbox.

This is the code I got so far.
private void buttonDutch_Click(object sender, EventArgs e)
        {
            string[] Filelistarray = Directory.GetFiles(@"C:\Users\a961835\Programming", "*.txt");
            List<string> fileNames = new List<string>();
            foreach (string FileName in Filelistarray)
            {
                string filenameWithoutPath = Path.GetFileName(FileName);
                fileNames.Add(filenameWithoutPath);
            }

            toolStripComboBox1.Items.AddRange(fileNames.ToArray());

            //this.toolStripComboBox1.SelectedIndexChanged += new System.EventHandler(toolStripComboBox1_SelectedIndexChanged);

            
            
        }

        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] Filelistarray = Directory.GetFiles(@"C:\Users\a961835\Programming", "*.txt");
            List<string> files = new List<string>();
            //foreach (string FileName in Filelistarray)

            string selectedItem = (string)toolStripComboBox1.SelectedItem;
            
            
            
            int resultIndex = -1;

            resultIndex = toolStripComboBox1.FindStringExact(selectedItem);

            while (resultIndex!=-1)
            {
                toolStripComboBox1.Items.RemoveAt(resultIndex);
                resultIndex = toolStripComboBox1.FindStringExact(selectedItem, resultIndex);
            }

            textBox1.Text = selectedItem;
        }


The problem is it only shows me the textfilename and not the the contents in that textfile.
Any ideas how to resolve this issue?
Posted

1 solution

Hi,
Quote:
string[] Filelistarray = Directory.GetFiles(@"C:\Users\a961835\Programming", "*.txt");

The "GetFiles" function will return all the files names with full path in the directory. it does not return the file contents. refer this[^]

In order to get the contents, read the contents of the file after selecting from the combobox in the selected index changed event and display in the texbox.
refer How to: Read Text from a File[^]

hope it helps.
 
Share this answer
 
v2
Comments
Nomardus 4-Mar-13 6:48am    
This isn't bad, but the problem is that I have to be able to click on a filename in the combobox and that it searches in the directory I give up and load the content of that file into the textbox, but what I can't find a way to do this automatic.
Atm I need to hardcode the entire path + textfile name and when I click on whatever textfilename in the combobox it only gives me the textfile content which I hardcoded :(
Karthik Harve 4-Mar-13 7:02am    
Instead of
string filenameWithoutPath = Path.GetFileName(FileName);
fileNames.Add(filenameWithoutPath);

why dont you add "FileName" it self. since it will hold complete path ?
Nomardus 4-Mar-13 8:08am    
Because I need just the filenames in the toolstripcombobox and when I click on a filename in the combobox it needs to load in the content of the selected file.

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