I have a Dictionary<string, List<string>> where string is a path to a file. I want to display the name of the File in a combo box and then use the file name as the value to capture it on SelectedValueChanged.
var cmbDatasource = (from moduleReference in moduleReferences
select new { FilePath = moduleReference.Key, Filename = new FileInfo(moduleReference.Key).Name }).ToList();
cmbModules.DataSource = cmbDatasource;
cmbModules.DisplayMember = "Filename";
cmbModules.ValueMember = "FilePath";
I am then doing this in my SelectedValueChanged event
private void cmbModules_SelectedValueChanged(object sender, EventArgs e)
{
var cmb = (ComboBox)sender;
if (cmb != null)
{
var test = cmb.SelectedValue.ToString();
}
}
test now becomes the anonymous type as apose to the value member (Path).
Using SelectedItem property of ComboBox w/Linq Anonymous Type
I’ve looked here but can’t see what I am doing differently.
Example
Dictionary<string, List<string>> moduleReferences = new Dictionary<string, List<string>>();
moduleReferences.Add("C:\Test.txt", new List<string>());
The combobox should display Test.txt however when I capture the event I expect to get the C:\Test.txt to do a look up in the dictionairy. Am I doing anything wrong here?
I believe that when you execute
You are taking only the value that is selected, not the item itself which will be the same type as moduleReference.Key. If you want to get the entire record you need to do