プロが教える店舗&オフィスのセキュリティ対策術

Visual C# 2008を使用しています。

ボタンを押すとpictureboxに画像が表示されるプログラムを作成しました。
今度は、その表示された画像をクリックし、別のボタンを押すことでその画像が削除できるようにしたいと思っているのですが、うまくプログラムできません。

誰か教えていただけないでしょうか?
ソースコードは以下のようになっています。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace algorithm
{
public partial class Form1 : Form
{
int space = 10;

public Form1()
{
InitializeComponent();
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void kihonsyori_Click(object sender, EventArgs e)
{
if (this.pictureBox1.Image == null)
{
Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\基本処理箱.png");
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height);
space += 50;
img.Dispose();
g.Dispose();
}
else
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
}

private void groupBox1_Enter(object sender, EventArgs e)
{

}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void hanpuku_Click(object sender, EventArgs e)
{
if (this.pictureBox1.Image == null)
{
Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\反復箱.png");
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height);
space += 80;
img.Dispose();
g.Dispose();
}
else
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
}


private void sentaku_Click(object sender, EventArgs e)
{
if (this.pictureBox1.Image == null)
{
Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\選択箱.png");
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height);
space += 80;
img.Dispose();
g.Dispose();
}
else
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
}

private void sakujyo_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
space = 0;
}
}
}

A 回答 (2件)

 こんにちは。


 別口で実験台のC#のプロジェクトを作成しても良いのならば、以下で試せませんか。
 pictureBox1, button1(読み込み), button2(削除)が必要です。参考程度で。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//スタートアップ
private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.Image = new Bitmap("test.bmp");
this.pictureBox1.Invalidate();

this.button2.Enabled = false;
}

//読み込みボタン
private void button1_Click(object sender, EventArgs e)
{
if (this.pictureBox1.Image != null)
this.pictureBox1.Image.Dispose();

this.pictureBox1.Image = new Bitmap("test.bmp");
this.pictureBox1.Invalidate();
}

//削除ボタン
private void button2_Click(object sender, EventArgs e)
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
this.pictureBox1.Invalidate();

this.button2.Enabled = false;
}

//ピクチャーボックスをクリックする
private void pictureBox1_Click(object sender, EventArgs e)
{
if (this.pictureBox1.Image == null)
return;

this.button2.Enabled ^= true;
}
}
}
    • good
    • 0
この回答へのお礼

回答ありがとうございます。

書いていただいたプログラムを参考にして、プログラムを組みなおしてみました。
しかし、picturebox自体をクリックしてもボタンが押せるようになってしまいます。picturebox内で画像のない場所をクリックしてもボタンが押せてしまうという状況です。

pictureboxに複数の画像を表示させ、一つ一つの画像をクリックできるようにしたいと思っています。
うまく参考にできず申し訳ないのですが、教えていただけないでしょうか?

お礼日時:2009/12/14 17:27

 こんばんは。

御礼頂きました。
 サムネイルの様な処理をしたいのでしょうか。
 不可能ではないのですが、プログラム内でビットマップの配列と、それぞれの位置を保持しておき、ピクチャーボックスのクリックイベントで、マウスの辺り判定を計測するなど、モグラ叩きの様なプログラムをする必要があります。
 どちらにしろ、ピクチャーボックスは複数の絵を出すのに向いていないのは確かです。
 リストビューでは駄目なのでしょうか。 
 http://oshiete1.goo.ne.jp/qa5400139.html

 上記URLの様にビットマップを並べて表示出来ますし、一貫した操作方法で追加、削除が行えます。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!