Question How do I setup comboBox data source property to get a file from my HDD?

NolansPapa

New member
Joined
Sep 27, 2018
Messages
2
Programming Experience
Beginner
I am a newbie....lets just start with that....so forgive my very basic questions on this site.....please.

I am trying to build a GUI for testing 3 different types of modules. I am using a comboBox with a dropdown with the names of the 3 different modules in the Collection property. Thats not a problem, but how do I actually tell it to actually go get that file that the user selected from a location on my HDD? I tried the Data Source Wizard, but I do not see a selection for internal HDD.
 
The Data Source Wizard isn't really what you need. It is for creating types that can be used to represent data in a file or database. It would help to know exactly what the relationship is between these "modules" you have currently in the ComboBox and the files on your hard drive but I'll provide some general information.

Let's say that you know that you have files named First.txt, Second.txt and Third.txt in the same folder as your executable. You might add the items "First", "Second" and "Third" to your ComboBox directly. You would then handle the SelectedIndexChanged or SelectionChangeCommitted event of the control and, in the event handler, do something like this:
var item = myComboBox.Text;
var filePath = Path.Combine(Application.StartupPath, item + ".txt");
var fileContents = File.ReadAllText(filePath);

Considering a different scenario, let's say that the user selects a folder using a FolderBrowserDialog and you want to allow them to select a file from that folder. You can load the file information into the ComboBox like this:
var folder = new DirectoryInfo(myFolderBrowserDialog.SelectedPath);
var files = folder.GetFiles();

myComboBox.DisplayMember = "Name";
myComboBox.ValueMember = "FullName";
myComboBox.DataSource = files;

That will display just the file names in the control but, when the user makes a selection, you can get the full path from the SelectedValue:
var filePath = (string) myComboBox.SelectedValue;
var fileContents = File.ReadAllText(filePath);

If that doesn't give you what you need, please provide more specific details.
 
Thanks for the quick reply...

The 3 modules are similar in many ways, but they have different HW and SW in them, so they need specific test scripts to test each of them. I have put the names of each module in the "Collection" property of the comboBox, but when the user selects the module he wants to test from the dropdown, I want that name to appear in the comboBox, and I need to actually use the correct test script file, communication file, and MAP file to run the test. I also want to display real-time data in a large field below so the user can monitor what is happening, because there is some user interaction that needs to take place during the test at certain times, and the script prompts the user when they need to do things.

Does this make any sense?

Im very new to all this. I can make a pretty GUI, but making the controls do what I want is where I am weak.
 
As I said, you need to handle the appropriate event of the ComboBox. You should read the documentation for the two events to learn the difference and then decide which one suits your situation better. In the event handler, you will use whichever of the Text, SelectedIndex and SelectedValue properties is most appropriate for your situation. You will then use that information to decide which file(s) to load and/or whatever else you need to do. It's pretty basic stuff: get some data, make decision based on its value and perform an action.
 

Latest posts

Back
Top Bottom