-2

我想删除选中鼠标单击的图片框内的图像(其自动生成),所以我可以使用删除键或上下文菜单删除它...

这里的代码:

private void button1_Click(object sender, EventArgs e)
    {
 string theimage = AppDomain.CurrentDomain.BaseDirectory + @"allimages";
 string[] images = Directory.GetFiles(theimage, "*.png");
                int aa;
                for (aa = 1; aa < images.Count(); aa++)
                {
                    PictureBox myPicBox = new PictureBox();
                    myPicBox.Location = new Point(7, 240);
                    myPicBox.Width = 100;
                    myPicBox.Height = 77;
                    myPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                    myPicBox.Margin = new Padding(3, 3, 3, 3);
                    myPicBox.Visible = true;
                    myPicBox.Image = new Bitmap(images[aa]);
                    this.flowLayoutPanel1.Controls.Add(myPicBox);
                    //myPicBox.Click += new EventHandler(curPb_Click);
                    //myPicBox.MouseUp += new MouseEventHandler(myPicBox_MouseUp);
                    myPicBox.MouseDown += new MouseEventHandler(myPicBox_MouseDown);
                    myPicBox.MouseLeave += new EventHandler(mmm_Leave);
                }


        }
        //private PictureBox senderAsPictureBox = null;
        private void mmm_Leave(object sender, EventArgs e)
        {
            PictureBox senderAsPictureBox = sender as PictureBox;
            senderAsPictureBox.BackColor = Color.Empty;
        }
        private void myPicBox_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox senderAsPictureBox = sender as PictureBox;
            MessageBox.Show(senderAsPictureBox.ToString());
            senderAsPictureBox.BackColor = Color.AliceBlue;
        }

这是我想做的

在此处输入图像描述

逻辑:

用户在图片框内选择图像拇指 -> 当用户按下 [delete] 键时 -> 删除所选图像

4

2 回答 2

0

我不明白你的问题。如果你想清除它使用这个:

senderAsPictureBox.Image = null;
senderAsPictureBox.Invalidate();

编辑:设置图像后,将控件的名称设置为图像路径:

                myPicBox.Name = images[aa].ToString();

还要创建一个新的事件处理程序来处理您的 KeyDownEvent

     myPicBox.PreviewKeyDown += new reviewKeyDownEventHandler(myPicBox_PreviewKeyDown);

使用这种方法:

void myPicBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    PictureBox senderAsPictureBox = sender as PictureBox;
    File.Delete(senderAsPictureBox.Name);
}

在您的 MouseDownHandlerMethod 中,将您的控制集中在: senderAsPictureBox.Focus();

于 2013-01-09T08:13:54.830 回答
0

从这里找到解决方案Get Picturebox Path and SubNatural answers

所以,我会把代码留在这里供可能需要的人使用

   private void button1_Click(object sender, EventArgs e)
    {
 string theimage = AppDomain.CurrentDomain.BaseDirectory + @"allimages";
 string[] images = Directory.GetFiles(theimage, "*.png");
                int aa;
                  for (aa = 1; aa < images.Count(); aa++)
            {
                PictureBox myPicBox = new PictureBox();
                myPicBox.Location = new Point(7, 240);
                myPicBox.Width = 100;
                myPicBox.Height = 77;
                myPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                myPicBox.Margin = new Padding(3, 3, 3, 3);
                myPicBox.Visible = true;
                FileStream fs = new FileStream(images[aa], FileMode.Open, FileAccess.Read);
                myPicBox.Image = Image.FromStream(fs);
                myPicBox.Name = images[aa];
                fs.Close();
                this.flowLayoutPanel1.Controls.Add(myPicBox);
                //myPicBox.Click += new EventHandler(curPb_Click);
                //myPicBox.MouseUp += new MouseEventHandler(myPicBox_MouseUp);
                myPicBox.MouseDown += new MouseEventHandler(myPicBox_MouseDown);
                myPicBox.MouseLeave += new EventHandler(mmm_Leave);
                myPicBox.PreviewKeyDown += new PreviewKeyDownEventHandler(myPicBox_PreviewKeyDown);

            }


        }
        private void myPicBox_MouseDown(object sender, MouseEventArgs e)
    {
        PictureBox senderAsPictureBox = sender as PictureBox;
        senderAsPictureBox.Focus(); // binding for clicking
        senderAsPictureBox.BackColor = Color.AliceBlue;
    }
    void myPicBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        PictureBox senderAsPictureBox = sender as PictureBox;
        if (e.KeyCode == Keys.Delete)
            senderAsPictureBox.Image = null;
            senderAsPictureBox.Invalidate();
            senderAsPictureBox.Dispose();
            File.Delete(senderAsPictureBox.Name);
    }

感谢大家帮助我... :) 特别是@SubNatural

于 2013-01-10T18:45:39.350 回答