2013-01-08 48 views
-2

我想删除里面PictureBox的图像(其自动生成),其选择的鼠标点击,这样我就可以删除键或文本菜单可能删除它...如何删除自动生成的图片框内的图像?

下面的代码:

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; 
     } 

这里是我想做

enter image description here

LOGIC:

用户选择内部PictureBox的Image拇指 - >当使用者按下[Delete]键 - >删除所选图像

+0

请注明您在调试此线路时遇到或遇到什么文件路径的错误或问题theimage = AppDomain.CurrentDomain.BaseDirectory + @“allimages”; – MethodMan

+0

@DJKRAZE nope,它根本没有错误 – radiaku

回答

0

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

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

编辑: 在您设置的图片,设置监控到的ImagePath名称:

   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();

+0

myPicBox.Image = new Bitmap(images [aa]); ...我想选择这个,并删除它与删除击键 – radiaku

+0

@ radiaku你想模拟一个'鼠标按钮点击'为什么你想这样做..?如果您要删除myPicBox.Image,则不需要鼠标单击。您的逻辑没有任何意义。请说明你真的想要做什么 – MethodMan

+0

@DJKRAZE 我想先选择图片,然后抓住按键来删除当前选择的图片 – radiaku

0
从这里 Get Picturebox Path和SubNatural

找到解决答案

因此,我将离开这里的代码,谁可能需要它

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