F
Friar Pott
Guest
I’m fairly new to Visual Studio and learning C#.
I am using Visual Studio C# Windows Forms App .NET Framework
I have added 4 PictureBoxes into my main Panel. The PictureBox’s pBox1 and pBox2 will receive images (.png) either from PictureBox mainImage or PictureBox pawnImage depending on the status (bool) Insert1 for pBox1 or (bool) Insert2 for pBox2. I have populated PictureBox mainImage and PictureBox pawnImage with the appropriate resource images I want to use. I am able to populate pBox1 and pBox2 from either mainImage or pawnImage.
I can ‘click’ on pBox1 (or pBox2) to make the image disappear. But if I ‘click’ on pBox1 it shows the bitmap image again. If I keep ‘clicking’ on pBox1 it toggles the image on and then clear. Why would this happen? It would seem once I clear pBox1 it should not toggle (display) back the image when I click it again. Any help would be appreciated.
C# Code so far:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace PictureBoxes
{
public partial class PicBoxes : Form
{
public static bool Insert1 = false;
public static bool Insert2 = false;
public PicBoxes()
{
InitializeComponent();
this.KeyPreview = true;
this.DoubleBuffered = true;
this.Box.Paint += Box_Paint;
}
private void pBox1_Click(object sender, EventArgs e)
{
if (Insert1)
{
Insert1 = false;
}
else Insert1 = true;
Console.WriteLine(" Insert1 {0} ", Insert1);
//Refresh();
pBox1.Invalidate();
}
private void pBox2_Click(object sender, EventArgs e)
{
if (Insert2) Insert2 = false;
else Insert2 = true;
Console.WriteLine(" Insert2 {0} ", Insert2);
//Refresh();
pBox2.Invalidate();
}
private void mainImage_Click(object sender, EventArgs e)
{
mainImage.Invalidate();
}
private void pawnImage_Click(object sender, EventArgs e)
{
pawnImage.Invalidate();
}
private void Box_Paint(object sender, PaintEventArgs e)
{
}
private void pBox1_Paint(object sender, PaintEventArgs e)
{
if (!Insert1)
{
e.Graphics.Clear(Color.SaddleBrown);
}
}
private void pBox2_Paint(object sender, PaintEventArgs e)
{
if (!Insert2)
{
e.Graphics.Clear(Color.Moccasin);
}
}
private void mainImage_Paint(object sender, PaintEventArgs e)
{
if (Insert1) pBox1.Image = mainImage.Image;
if (Insert2) pBox2.Image = mainImage.Image;
}
private void pawnImage_Paint(object sender, PaintEventArgs e)
{
if (Insert1) pBox1.Image = pawnImage.Image;
if (Insert2) pBox2.Image = pawnImage.Image;
}
}
}
Continue reading...
I am using Visual Studio C# Windows Forms App .NET Framework
I have added 4 PictureBoxes into my main Panel. The PictureBox’s pBox1 and pBox2 will receive images (.png) either from PictureBox mainImage or PictureBox pawnImage depending on the status (bool) Insert1 for pBox1 or (bool) Insert2 for pBox2. I have populated PictureBox mainImage and PictureBox pawnImage with the appropriate resource images I want to use. I am able to populate pBox1 and pBox2 from either mainImage or pawnImage.
I can ‘click’ on pBox1 (or pBox2) to make the image disappear. But if I ‘click’ on pBox1 it shows the bitmap image again. If I keep ‘clicking’ on pBox1 it toggles the image on and then clear. Why would this happen? It would seem once I clear pBox1 it should not toggle (display) back the image when I click it again. Any help would be appreciated.
C# Code so far:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace PictureBoxes
{
public partial class PicBoxes : Form
{
public static bool Insert1 = false;
public static bool Insert2 = false;
public PicBoxes()
{
InitializeComponent();
this.KeyPreview = true;
this.DoubleBuffered = true;
this.Box.Paint += Box_Paint;
}
private void pBox1_Click(object sender, EventArgs e)
{
if (Insert1)
{
Insert1 = false;
}
else Insert1 = true;
Console.WriteLine(" Insert1 {0} ", Insert1);
//Refresh();
pBox1.Invalidate();
}
private void pBox2_Click(object sender, EventArgs e)
{
if (Insert2) Insert2 = false;
else Insert2 = true;
Console.WriteLine(" Insert2 {0} ", Insert2);
//Refresh();
pBox2.Invalidate();
}
private void mainImage_Click(object sender, EventArgs e)
{
mainImage.Invalidate();
}
private void pawnImage_Click(object sender, EventArgs e)
{
pawnImage.Invalidate();
}
private void Box_Paint(object sender, PaintEventArgs e)
{
}
private void pBox1_Paint(object sender, PaintEventArgs e)
{
if (!Insert1)
{
e.Graphics.Clear(Color.SaddleBrown);
}
}
private void pBox2_Paint(object sender, PaintEventArgs e)
{
if (!Insert2)
{
e.Graphics.Clear(Color.Moccasin);
}
}
private void mainImage_Paint(object sender, PaintEventArgs e)
{
if (Insert1) pBox1.Image = mainImage.Image;
if (Insert2) pBox2.Image = mainImage.Image;
}
private void pawnImage_Paint(object sender, PaintEventArgs e)
{
if (Insert1) pBox1.Image = pawnImage.Image;
if (Insert2) pBox2.Image = pawnImage.Image;
}
}
}
Continue reading...