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

ここで私がしたいこと

ここに画像の説明を入力してください

論理:

ユーザーが画像ボックス内の画像サムを選択->ユーザーが[削除]キーを押したとき->選択した画像を削除

4

2 に答える 2

0

私はあなたの問題を理解していません。クリアしたい場合はこれを使用してください:

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

編集:画像を設定した後、コントロールの名前を画像パスに設定します。

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

また、KeyDownEventを処理するための新しいEventhandlerを作成します

     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

ここから解決策を見つけましたPictureboxPathと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に

于 2013-01-10T18:45:39.350 に答える