2

I have been working on this in my spare time, to no avail. I really could use a hand getting this to work.

I have a winform in C#. I am currently working with a listbox and a picturebox to display embedded image resources. I want to populate the listbox with just the file name, as the full path is longer than the width of the listbox can accommodate on my form.

Here is some code I was working with:

string[] x = System.IO.Directory.GetFiles(@"C:\Users\bassp\Dropbox\VS Projects\WindowsFormsApplication1\WindowsFormsApplication1\Resources\", "*.jpg");
        pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        foreach (string f in x)
        {
            string entry1 = Path.GetFullPath(f);
            string entry = Path.GetFileNameWithoutExtension(f);
            listBox1.DisplayMember = entry;
            listBox1.ValueMember = entry1;
            listBox1.Items.Add(entry);

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        pictureBox1.ImageLocation = listBox1.SelectedItem.ToString();
    }

If I populate the listbox with the full path,(entry1), things work pretty smooth other than not being able to see the image name you will be selecting due to the length of the full path.

When I try to populate the listbox with (entry), just the file names appear in the listbox, which is desireable, however, the image will no longer open on selection from the listbox.

How can I get this working correctly? Help is greatly appreciated.

Patrick

2 Answers 2

1

You're on the right track by setting the DisplayMember and ValueMember properties, but you'd need to make a few corrections, and it might not be necessary here.

Store the original directory path in a separate variable, then just combine that with the file name in your SelectedIndexChanged event to get the original file path.

string basePath =
    @"C:\Users\bassp\Dropbox\VS Projects\WindowsFormsApplication1\WindowsFormsApplication1\Resources\";

string[] x = Directory.GetFiles(basePath, "*.jpg");

foreach (string f in x)
{
    listBox1.Items.Add(Path.GetFileNameWithoutExtension(f));
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    pictureBox1.ImageLocation =
        Path.Combine(basePath, listBox1.SelectedItem.ToString()) + ".jpg";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. your solution worked well for me.
1

Grant answer is perfectly ok for a simple task like this, but i'll explain another way that may be useful in other situations.

You can define a class to store your filenames and paths, something like:

class images
{
    public string filename { get; set; }
    public string fullpath { get; set; }
}

This way, your code could be like:

string[] x = System.IO.Directory.GetFiles(@"C:\Users\bassp\Dropbox\VS Projects\WindowsFormsApplication1\WindowsFormsApplication1\Resources\", "*.jpg");
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
List<images> imagelist=new List<images>();
foreach (string f in x)
{
    images img= new images();
    img.fullpath = Path.GetFullPath(f);
    img.filename = Path.GetFileNameWithoutExtension(f);
    imagelist.Add(img);
}
listBox1.DisplayMember = "filename";
listBox1.ValueMember = "fullpath";
listBox1.DataSource = imagelist;

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    pictureBox1.ImageLocation = ((images)listBox1.SelectedItem).fullpath;
}

I have not tested it,maybe it has any typo, but i hope you get the idea.

1 Comment

Yes,you are right @GrantWinney. I'm used to do it this way, but SelectedValue is maybe better :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.