/************************************************/
/*★ PictureBox の画像を切り替える 前田 稔 ★*/
/************************************************/
using System;
using System.Drawing;
using System.Windows.Forms;
public class MyForm : Form
{
private Button button1;
private PictureBox pictureBox1;
Image img1 = Image.FromFile(@"C:\Data\Test\Kishi.gif");
Image img2 = Image.FromFile("C:\\Data\\Test\\ayu.gif");
int flag = 1;
public MyForm()
{
// button1
button1 = new Button();
button1.Parent = this;
button1.Location = new Point(90, 221);
button1.Text = "押す";
button1.BackColor = SystemColors.Control;
button1.Click += new System.EventHandler(Button1_Click);
// pictureBox1
pictureBox1 = new System.Windows.Forms.PictureBox();
pictureBox1.Location = new Point(12, 12);
pictureBox1.Size = new Size(258, 198);
// MyForm
Controls.Add(button1);
Controls.Add(pictureBox1);
pictureBox1.Image = img1;
}
private void Button1_Click(object sender, EventArgs e)
{
flag ^= 1;
if (flag==1) pictureBox1.Image = img1;
else pictureBox1.Image = img2;
//else pictureBox1.Image = null;
}
}
class form01
{
public static void Main()
{
MyForm mf = new MyForm();
Application.Run(mf);
}
}
|