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;
}
}
}
No.1ベストアンサー
- 回答日時:
こんにちは。
別口で実験台の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;
}
}
}
回答ありがとうございます。
書いていただいたプログラムを参考にして、プログラムを組みなおしてみました。
しかし、picturebox自体をクリックしてもボタンが押せるようになってしまいます。picturebox内で画像のない場所をクリックしてもボタンが押せてしまうという状況です。
pictureboxに複数の画像を表示させ、一つ一つの画像をクリックできるようにしたいと思っています。
うまく参考にできず申し訳ないのですが、教えていただけないでしょうか?
No.2
- 回答日時:
こんばんは。
御礼頂きました。サムネイルの様な処理をしたいのでしょうか。
不可能ではないのですが、プログラム内でビットマップの配列と、それぞれの位置を保持しておき、ピクチャーボックスのクリックイベントで、マウスの辺り判定を計測するなど、モグラ叩きの様なプログラムをする必要があります。
どちらにしろ、ピクチャーボックスは複数の絵を出すのに向いていないのは確かです。
リストビューでは駄目なのでしょうか。
http://oshiete1.goo.ne.jp/qa5400139.html
上記URLの様にビットマップを並べて表示出来ますし、一貫した操作方法で追加、削除が行えます。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- C言語・C++・C# Windows Formアプリからコンソールを呼び出して文字を出力させたい 8 2023/05/09 10:53
- C言語・C++・C# C# DatagridviewにExcelシートを反映するとエラーが出る 2 2023/05/06 17:12
- JavaScript clear機能を失わずにファイルアップロード機能を作成したい 3 2023/06/10 16:12
- Visual Basic(VBA) Visual Basic : ImageListの画像がそろったときにメッセージを表示 1 2023/07/20 13:53
- HTML・CSS CSSが効かずどのように指定すれば良いか分からないのでアドバイスお願い致します 2 2023/06/07 12:25
- JavaScript アップロードファイルの種類によって処理を分岐させたいのですが書き方が分からずアドバイスお願いします 4 2023/06/17 19:12
- C言語・C++・C# 大量のデータを読み込んで表示する速度を改善したい 8 2023/05/07 13:29
- その他(プログラミング・Web制作) pythonで、tkinterとpillowの組み合わせ 2 2022/08/16 17:42
- JavaScript 画像の表示位置 3 2022/12/23 08:25
- C言語・C++・C# C#テキストボックスの文字を配列にいれてその後表示する 4 2022/07/17 04:47
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
【gcc・cygwin】multiple defin...
-
C++でイベントドリブンなコード...
-
TimerProc コールバック関数を...
-
C# KeyDownイベントでショート...
-
戻り値を返す関数の前に(void)...
-
bccでエラー:メンバー関数は呼...
-
Notepad++の関数リスト表示の変...
-
LNK2019:未解決の外部シンボル ...
-
(void)0 はどんな意味ですか
-
既定のコンストラクタがありま...
-
関数ポインタについて
-
C++でオーバーロードに関するバ...
-
pictureboxの画像を削除するには?
-
C/C++でのScene管理について
-
Arduinoを使ってMIDI機器のコン...
-
void*型の配列について
-
多重定義が起きている?--lnk20...
-
ウインドウの移動禁止
-
Arduinoでの圧電スピーカーとタ...
-
ifstreamについて
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
既定のコンストラクタがありま...
-
【gcc・cygwin】multiple defin...
-
戻り値を返す関数の前に(void)...
-
C++にてtemplateで受け取った任...
-
多重定義が起きている?--lnk20...
-
gcc: incompatible pointer type
-
静的でないメンバ関数の呼び出...
-
int main()、void main()、void...
-
Notepad++の関数リスト表示の変...
-
C# KeyDownイベントでショート...
-
C# Controls.Addで動的に配置し...
-
ArduinoでMouse関数を使用して...
-
(void)0 はどんな意味ですか
-
DLLの関数呼び出しで引数がある...
-
void*型の配列について
-
ウインドウの移動禁止
-
const_castのつかいどころを教...
-
VC++でGetKeyboardStateがうま...
-
C言語 プロトタイプ宣言
-
stddef.hって何?
おすすめ情報