当前位置: 代码迷 >> C# >> c#求好手相助
  详细解决方案

c#求好手相助

热度:561   发布时间:2016-05-05 05:35:33.0
c#求高手相助
[b]列表框实现加载图片名,如何实现点击图片名,让图片在pictureBox中显示


listBox加载图片名代码:
       private void button5_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "*.gif|*.gif";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string path = this.openFileDialog1.FileName;
                FileInfo f = new FileInfo(path);
                AddFile(f.FullName);
                string strTmp = Convert.ToString(num);
                for (int i = 1; i <= 5 - strTmp.Length; i++)
                    strTmp += ' ';
                strTmp += "--" + f.Name;
                this.listBox1.Items.Add(strTmp);
             //   pictureBox1.Image = listBox1.DataSource;
            }
        }

点击实现在pictureBox中显示图片代码:
 private void listBox1_Click(object sender, EventArgs e)
        {
          
                  pictureBox1.Image=Image.FromFile(this.path +"\\"+listBox1.SelectedItem.ToString());
        }


出现错误,求助!!!!
------解决思路----------------------

  public string filepath;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "*.gif
------解决思路----------------------
*.jpg";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string  path = this.openFileDialog1.FileName;
                 filepath=Path.GetDirectoryName(path); 
                FileInfo f = new FileInfo(path);
               //// AddFile(f.FullName);
               // string strTmp = Convert.ToString(num);
               // for (int i = 1; i <= 5 - strTmp.Length; i++)
               //     strTmp += ' ';
               // strTmp += "--" + f.Name;
                this.listBox1.Items.Add(f.Name);
                //   pictureBox1.Image = listBox1.DataSource;
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(filepath + "\\" + listBox1.SelectedItem.ToString());
        }



  相关解决方案