如何从列表框选定项目中获取要在Picturbox中显示的图像

时间:2017-04-29 14:55:29

标签: c#

在我实际调用SetPicture()方法之前,一切正常。有关更改所选项目时图像不显示的原因的任何想法。现在我只有一个图像预设,直到我弄清楚我的问题。谢谢你的帮助

namespace CelestialWindowsApp
{
    public partial class Form1 : Form
    {
        public static List CelestialLibrary = new List(); private static CelestialBody celestialBody; private static string name; private static string description; private static string image; public static CelestialBody NewPlanet { get { return celestialBody; } set { celestialBody = value; } }

        public Form1()
        {
            InitializeComponent();
            PopulateLibrary();
        }

        public void PopulateLibrary()//Runs when the Form 1 is loaded. Adds preset item into the ListNox.
        {
            CelestialLibrary = new List<CelestialBody>();
            CelestialBody cb1 = new CelestialBody("Earth", "Our Home", @"C:\Users\Cassidy\documents\visual studio 2015\Projects\CelestialWindowsApp\CelestialWindowsApp\ImageResources\earthimage.jpg");
            CelestialBody cb2 = new CelestialBody("Mars", "4th planet from the sun.");
            CelestialBody cb3 = new CelestialBody("Venus", "2nd planet from the son");

            CelestialLibrary.Add(cb1);
            CelestialLibrary.Add(cb3);
            lbLibrary.DataSource = CelestialLibrary;
            foreach (var p in CelestialLibrary)
            {
                lbLibrary.DisplayMember = "ShowBodies";
            }
        }

        public void AddItem() // Adds the new Item to my library and then adds to and updates the ListBox;
        {
            CelestialBody cb;
            name = txtboxName.Text;
            description = txtboxDescription.Text;
            image = txtboxImagePath.Text;
            cb = new CelestialBody(name, description, image);
            CelestialLibrary.Add(cb);
            MessageBox.Show(name + " has been added to the Library.");
            txtboxName.Text = null;
            txtboxDescription.Text = null;
            lbLibrary.DataSource = null;
            lbLibrary.DataSource = CelestialLibrary;
            lbLibrary.DisplayMember = "ShowBodies";
        }
        public void DisplayItemInfo()//Displays the current selected item information.
        {
            List<CelestialBody> bodyList = (List<CelestialBody>)lbLibrary.DataSource;
            CelestialBody currentItem = bodyList[lbLibrary.SelectedIndex];
            foreach (CelestialBody cb in bodyList)
            {
                if (currentItem.MyId == cb.MyId)
                    celestialBody = new CelestialBody(cb.Name, cb.Description, cb.ImagePath);
                {
                    txtboxItemInfo.Text = celestialBody.Description;
                    SetPicture(celestialBody.ImagePath);
                }
            }
        }

        public void SetPicture(string image)
        {
            if (picboxBodyImage.Image != null)
            {
                picboxBodyImage.Image.Dispose();
            }
            picboxBodyImage.ImageLocation = image;
        }

        private void btnAddNewBody_Click(object sender, EventArgs e)
        {
            AddItem();
        }

        private void lbLibrary_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisplayItemInfo();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您是否检查了变量image中的路径。假设路径正确,请尝试以下

picboxBodyImage.Image = Image.FromFile(image);