0

Goal:

  • Get all file directories from MySQL and place them into a dictionary.

  • Display them into a combobox just as the filename. e.g. filename

  • Assign the combobox value as the the full directory. e.g. c:\users\user\desktop\filename.jpg

Code:

string filenames = "select filename from  request_label_signoff where progress_user1 is null or progress_user2 is null";

//On load - load specific images from query above
private void Form15_Load(object sender, EventArgs e)
{

    //Dict to store file into
    Dictionary<string, string> files = new Dictionary<string, string>();

    using (var conn = new MySqlConnection(connString))
    {
        conn.Open();
        using (var cmd = new MySqlCommand(filenames, conn))
        {
            using (MySqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {   
                    //add filename without extension and full directory
                    files.Add(Path.GetFileNameWithoutExtension(reader.GetString(0)), reader.GetString(0));
                }
            }
        }

    }

        comboBox1.DataSource = new BindingSource(files, null);
        comboBox1.DisplayMember = "Key";
        comboBox1.ValueMember = "Value";

}




private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    value = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;
    pictureBox1.Image = Image.FromFile(value);

}

Problem:

For some reason the display value for the combobox shows like this:

enter image description here

Text output: [abc 123, C:\Users...]

Whereas it should be abc 123 without the directory next to it.

Question:

Why does he combo-box display value show both items?

LV98
  • 887
  • 5
  • 19
  • It seems that you are populating the combobox in a wrong way. Google for information how to populate combobox or use this SO question as reference: https://stackoverflow.com/questions/7423911/how-to-populate-c-sharp-windows-forms-combobox – Leron_says_get_back_Monica Apr 03 '20 at 10:32
  • [Other way around](https://stackoverflow.com/a/1507008/17034). – Hans Passant Apr 03 '20 at 10:45

2 Answers2

0

You need to change the order of assignment in the combobox.

The reasons are:

Instead of:

comboBox1.DataSource = new BindingSource(files, null);
comboBox1.DisplayMember = "Key";
comboBox1.ValueMember = "Value";

it should be:

comboBox1.DisplayMember = "Key";
comboBox1.ValueMember = "Value";
comboBox1.DataSource = new BindingSource(files, null);
Venkataraman R
  • 8,468
  • 1
  • 21
  • 39
  • hmm... I tried. It is working fine for me. Debug it and see which is getting loaded to files dictionary – Venkataraman R Apr 03 '20 at 11:18
  • If I set `DisplayMember` to value. It just shows the Directory. But the key is showing the both values in these brackets `[]` – LV98 Apr 03 '20 at 11:27
0

Instead of using this: Dictionary<string, string> files = new Dictionary<string, string>();

I have used: var choices = new Dictionary<string, string>();

As guided in the comment by hans-passant

And everything works. Not sure what the difference was.

LV98
  • 887
  • 5
  • 19