title: Select and display Image files from ListBox, to be display within PictureBox? C#
body: <p>I am trying to make my listBox (<strong>lstFiles</strong>) selectable, so it's able to display the image file within a pictureBox (<strong>pictureBox1</strong>) and change after selecting another file from listBox, Im creating a webcam program that takes pictures which works, but having trouble with with displaying the images.</p>
  
  <p>I have tried many ways but can't get it to work from selecting the filename from the listbox</p>
  
  <p>Any help would be grateful thank you</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Drawing.Imaging;
  using System.Runtime.InteropServices;
  using System.Text;
  using System.Windows.Forms;
  using Pinvoke;
  using System.IO;
  
  namespace TestAvicap32
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
              InitializeDevicesList();
          }
  
          private void InitializeDevicesList()
          {
              splitContainer1.Panel1.Enabled = true;
              splitContainer1.Panel2.Enabled = false;
  
              foreach (CaptureDevice device in CaptureDevice.GetDevices())
              {
                  cboDevices.Items.Add(device);
              }
  
              if (cboDevices.Items.Count &gt; 0)
              {
                  cboDevices.SelectedIndex = 0;
              }
          }
  
          private void btnStart_Click(object sender, EventArgs e)
          {
              int index = cboDevices.SelectedIndex;
              if (index != -1)
              {
                  splitContainer1.Panel1.Enabled = false;
                  splitContainer1.Panel2.Enabled = true;
                  ((CaptureDevice)cboDevices.SelectedItem).Attach(pbImage);
              }
          }
  
          private void btnStop_Click(object sender, EventArgs e)
          {
              splitContainer1.Panel1.Enabled = true;
              splitContainer1.Panel2.Enabled = false;
              ((CaptureDevice)cboDevices.SelectedItem).Detach();
          }
  
          private void btnSnapshot_Click(object sender, EventArgs e)
          {
              try
              {
                  Image image = ((CaptureDevice)cboDevices.SelectedItem).Capture();
                  image.Save(@"c:\webcapture\" + DateTime.Now.ToString("HH.mm.ss-dd-MM-yy") + ".png", ImageFormat.Png);
  
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
              }   
          }
  
          private void btntimer_Click(object sender, EventArgs e)
          {
              timer1.Enabled = true;
              btntimerstop.Visible = true;
              btntimer.Visible = false;
          }
  
          private void timer1_Tick(object sender, EventArgs e)
          {
              Image image = ((CaptureDevice)cboDevices.SelectedItem).Capture();
              image.Save(@"c:\webcapture\" + DateTime.Now.ToString("HH.mm.ss-dd-MM-yy") + ".png", ImageFormat.Png);
          }
  
          private void btntimerstop_Click(object sender, EventArgs e)
          {
              timer1.Enabled = false;
              btntimer.Visible = true;
              btntimerstop.Visible = false;
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              btntimerstop.Visible = false;
  
              foreach (DriveInfo di in DriveInfo.GetDrives())
                  lstDrive.Items.Add(di);
          }
  
          private void lstFolders_SelectedIndexChanged(object sender, EventArgs e)
          {
              lstFiles.Items.Clear();
  
              DirectoryInfo dir = (DirectoryInfo)lstFolders.SelectedItem;
  
              foreach (FileInfo fi in dir.GetFiles())
                  lstFiles.Items.Add(fi);
          }
  
          private void lstDrive_SelectedIndexChanged(object sender, EventArgs e)
  
          {
              lstFolders.Items.Clear();
  
              try
              {
                  DriveInfo drive = (DriveInfo)lstDrive.SelectedItem;
  
                  foreach (DirectoryInfo dirInfo in drive.RootDirectory.GetDirectories())
                      lstFolders.Items.Add(dirInfo);
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
  
          private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
          {
  
          }
  
          private void pictureBox1_Click(object sender, EventArgs e)
          {
  
          }
  
          private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
          {
              //I don't know If I need this?
          }
  
          }
     }
  </code></pre>