Windows Develop Bookmark and Share   
 index > Windows Forms General > Need help with setting up a comboBox
 

Need help with setting up a comboBox

Hello fellow members,

I am working on a C# program using Visual Studio 2005 Professional Edition. I am having some difficulty setting up a combobox. I would like to have the combobox set up to access certain files on my computer hard drive (C:/). I have the file names set up in the combobox, but I am not sure how to bind them to the corresponding files on my hard drive. My research on MSDN has only turned up bits of code examples, but nothing "complete."

I would also like for the file to open within theform, and not in a separate window.

Thank you all,

Sorci

Sorci  Friday, September 28, 2007 7:54 AM

Hi Sorci

first of all create a table to bind value from file

datatable comboDT=new datatable();

comboDT.columns.add("files",typeof(string)); //To add a column to bind the files name

USING (StreamReader sr = new StreamReader("TestFile.txt")) //Reading file
{
String line;
while ((line = sr.ReadLine()) != null)
{
comboDT.rows.add(line) //To add the line to the table
}
}

where the table is filled up with data from file now you can bind the table with combo box

combobox.displaymeber="files"; //its for display purpose so which column u want to display specify the column name

combobox.valuemember="files" //this column not display

combobo.datasource=comboDT //bind the datatable to combo box

Now files names were binded with combo box

Regards

palan

Tharmapalan  Friday, September 28, 2007 8:42 AM

Hi Sorci

first of all create a table to bind value from file

datatable comboDT=new datatable();

comboDT.columns.add("files",typeof(string)); //To add a column to bind the files name

USING (StreamReader sr = new StreamReader("TestFile.txt")) //Reading file
{
String line;
while ((line = sr.ReadLine()) != null)
{
comboDT.rows.add(line) //To add the line to the table
}
}

where the table is filled up with data from file now you can bind the table with combo box

combobox.displaymeber="files"; //its for display purpose so which column u want to display specify the column name

combobox.valuemember="files" //this column not display

combobo.datasource=comboDT //bind the datatable to combo box

Now files names were binded with combo box

Regards

palan

Tharmapalan  Friday, September 28, 2007 8:42 AM

I have no idea what you mean binding the elements in your combo box to actual files but I suggestresearching about System.IO classes, specifically DirectoryInfo and FileInfo...

Code Block

...

using System.IO;

...

string filePath, folderPath;

filePath = <the whole path> // Maybe you should use verbatim string @

folderPath = <the whole path>

....

DirectoryInfo directoryOfFiles = new DirectoryInfo(folderPath);

// To get the files with txt extension in the selected directory

FileInfo[] selectedFiles = directoryOfFiles.GetFiles("*.txt");

// Also this can be used....

FileInfo selectedFile = new FileInfo(filePath);

I hope it helps...at least with the "binding" that you are talking about... Just go through the msdn references for these classes...
Can Bilgin  Friday, September 28, 2007 8:48 AM

Hi Can Bilgin,

I think I got the words mixed up; I wrote the original post at about 1:00am, so I was a bit tired. Basically, I want items I have listed in my comboBox to bring up a files that I have on my hard drive. Additionally, I would like to have the file open within the form, and not in a separate window. I am still pretty new at C# as you may have figured.

Thank you,

Sorci

Sorci  Friday, September 28, 2007 5:28 PM

Hi Palan,

Thank you for the code sample. This should help me in getting on the right path. I am trying to get the items I have listed in my comboBox to bring up files I have on my hard drive, and have the file open within the form.

Thank you,

Sorci

Sorci  Friday, September 28, 2007 5:44 PM

You can use google to search for other answers

Custom Search

More Threads

• Highlighting the node under the mouse in a drag and drop operation
• Reading value from UserAccountControl
• How to add value to a speciffic column in listView?
• foreach statement
• Emergent!!!! List<Type> variable addition problem
• Getting a TableLayoutPanel to Scroll in "real-time", not delayed after scroll thumb de-clicked
• [DataGridView] how to make a DataGridView follow a Query?
• datagrid scroll
• DataGridView and Context Menu
• How to get the pixels per inch when printing?