Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

replacing image using selectedIndexChanged

listbox picturebox

  • Please log in to reply
3 replies to this topic

#1 emillion

emillion

    CC Lurker

  • New Member
  • Pip
  • 7 posts
  • Location:south florida
  • Programming Language:C#
  • Learning:C#

Posted 16 October 2012 - 09:12 AM

I have a piece of code I put together using some examples from msdn and other various sources.

My goal here is to select an item in the listbox and have the corrosponding image display below. Initially the selected image displays with no problem. My problem is when I change the selected item in the list the image doens't change as I expected. Can someone analyze the code I've posted and explain why this isn't working or shoot me some examples?

I was doing some testing and found that if I showed a messagebox after the image is displayed then use the controls.Remove(picture) -- it works but this is not the way I'm attempting to have the program flow.

Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
	 public Form1()
	 {
		 InitializeComponent();
	 }
		 private void Form1_Load(object sender, EventArgs e)
	 {
		 // Check to see if folder is empty
		 string[] array1 = Directory.GetFiles(@"C:\documents and settings\all users\documents\igrt\", "*.jpg");
		 if (array1.Length == 0)
		 {
			 MessageBox.Show("There are no images to process. The Program will now terminate");
			 this.Close();
		 }
		 DirectoryInfo dinfo = new DirectoryInfo(@"C:\documents and settings\all users\documents\igrt");
		 FileInfo[] Files = dinfo.GetFiles("*.jpg");
		 foreach (FileInfo file in Files)
		 {
			 listBox1.Items.Add(file.Name);
		 }

	 }
		 private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
	 {
		 PictureBox picture = new PictureBox
		 {
			 Name = "pictureBox",
			 Size = new Size(350, 350),
			 Location = new Point(200, 200)	
		 };
		 picture.ImageLocation = (@"c:\documents and settings\all users\documents\igrt\" + (listBox1.SelectedItem.ToString()));
		 Controls.Add(picture);
		 MessageBox.Show(picture.ImageLocation);

	 }	
}
}

  • 0

#2 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 16 October 2012 - 09:54 AM

I think it's best if you create a default image ... then on changed ... you change its image location rather than ... creating a new object and add to the controls, but then when the index changed again ... you must remove the current image and add it.

So, on your UI design ... add a default image, then on changed just change the location of the image.

hope this helps
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#3 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 16 October 2012 - 11:04 AM

Why not have a default image control and only change the property. Also, you might want to verify if SelectedItem is correct. In webforms, it's SelectedValue.
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#4 emillion

emillion

    CC Lurker

  • New Member
  • Pip
  • 7 posts
  • Location:south florida
  • Programming Language:C#
  • Learning:C#

Posted 16 October 2012 - 11:07 AM

Thanks I restructured the code and it appears to be working like a champ. Great advice. I will be back for more.
    public partial class Form1 : Form
    {
	    public Form1()
	    {
		    InitializeComponent();
	    }
		  private void Form1_Load(object sender, EventArgs e)
	    {
		    // Check to see if IGRT folder is empty
		    string[] array1 = Directory.GetFiles(@"C:\documents and settings\all users\documents\igrt\", "*.jpg");
		    if (array1.Length == 0)
		    {
			    MessageBox.Show("There are no images to process. The Program will now terminate");
			    this.Close();
		    }
		    DirectoryInfo dinfo = new DirectoryInfo(@"C:\documents and settings\all users\documents\igrt");
		    FileInfo[] Files = dinfo.GetFiles("*.jpg");
		    foreach (FileInfo file in Files)
		    {
			    listBox1.Items.Add(file.Name);
		    }
	    }
		    private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
	    {
		    pictureBox1.ImageLocation = (@"c:\documents and settings\all users\documents\igrt\" + (listBox1.SelectedItem.ToString()));
	    }	  
    }
}

  • 0





Also tagged with one or more of these keywords: listbox, picturebox

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download