picture box invalidate method in C# - c#

I want to draw something in a picture box and remove them.
in this case i need to draw a fill circle and after a time remove it and draw a circle without fill it.
i use below code but when i want to remove a shape i need to draw picturebox another time with new shape and it causes a sensitive change in picture box.now i know i should use invalidate() method. but i do not know where and how should i use that.
void pbmapDo()
{
Graphics graphicPBMap = pbMap.CreateGraphics();
// usually Values : gridNeedUpdate = true; rulersNeedUpdate = true; rulerNeedUpdate = true; backGroundNeedUpdate = true; nodesNeedUpdate = true;
if (backGroundNeedUpdate)
{
Bitmap srce = new Bitmap(BackGround);
Bitmap dest = new Bitmap(pbMap.Width, pbMap.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics gr = Graphics.FromImage(dest);
gr.DrawImage(srce, new Rectangle(Point.Empty, dest.Size));
graphicPBMap.DrawImage(dest, 0, 0);
}
backGroundNeedUpdate = false;
if (isGridShow && gridNeedUpdate)
{
for (int i = 0; i < pbMap.Width / 60 + 1; i++)
{
graphicPBMap.DrawLine(gridPen, pbMap.Width - i * 60, 0, pbMap.Width - i * 60, pbMap.Height);
}
for (int i = 0; i < pbMap.Height / 60 + 1; i++)
{
graphicPBMap.DrawLine(gridPen, pbMap.Width, i * 60, 0, i * 60);
}
}
gridNeedUpdate = false;
if (isShowRulers && rulersNeedUpdate )
{
graphicPBMap.DrawLine(new Pen(Brushes.Black, 46), pbMap.Width - 1, pbMap.Height - 1, 0 - 1, pbMap.Height - 1);
graphicPBMap.DrawLine(new Pen(Brushes.Black, 49), 0, 0, 0, pbMap.Height);
for (int i = 0; i < pbMap.Width / 60 + 1; i++)
{
graphicPBMap.DrawString(XPixelToLong((double)pbMap.Width - i * 60).ToString(), new Font("Tahoma", 7), Brushes.White, pbMap.Width - i * 60, pbMap.Height - 15);
graphicPBMap.DrawLine(gridPen, pbMap.Width - i * 60, pbMap.Height - 24, pbMap.Width - i * 60, pbMap.Height);
}
for (int i = 0; i < pbMap.Height / 60 + 1; i++)
{
graphicPBMap.DrawLine(gridPen, 0, i * 60, 25, i * 60);
graphicPBMap.DrawString(YPixelToLat(i * 60).ToString(), new Font("Tahoma", 7), Brushes.White, 0, i * 60);
}
}
rulersNeedUpdate = false;
if (rulerNeedUpdate)
{
if (x0ruler != -1 && y0ruler != -1)
{
if (x1ruler != -1 && y1ruler != -1)
{
Rectangle rectAngle = new Rectangle((int)(x1ruler - 1), (int)(y1ruler - 1), 2, 2);
graphicPBMap.DrawEllipse(rulerPen, rectAngle);
graphicPBMap.DrawLine(rulerPen, x0ruler, y0ruler, x1ruler, y1ruler);
rectAngle = new Rectangle((int)(x0ruler - 1), (int)(y0ruler - 1), 2, 2);
graphicPBMap.DrawEllipse(rulerPen, rectAngle);
}
else
{
Rectangle rectAngle = new Rectangle((int)(x0ruler - 1), (int)(y0ruler - 1), 2, 2);
graphicPBMap.DrawEllipse(rulerPen, rectAngle);
}
}
}
rulerNeedUpdate = false;
if (nodesNeedUpdate)
{
nodesNeedUpdate = false;
Node node;
for (int i = 0; i < nodes.Count; i++)
{
node = (Node)(nodes[i]);
if (node.IsOn)
{
drawOnCircle(graphicPBMap,node.Longitude, node.Latitude, node.RInMeter, node.IsSelected);
}
else
{
drawOffCircle(graphicPBMap, node.Longitude, node.Latitude, node.RInMeter, node.IsSelected);
}
}
}
}
EDIT 1 :
i changed code once.

You can use
invalidate() or pictureBox.image = null; where you need to change the image of your picturebox.
Anyway, it is not required to change the background always. Instead you can make your bitmap and place it as pictureBox.Image = bitmap.
If you need exact sample source code let me know.

sorry for the late.
try this
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap bckMap = null;
private void Form1_Load(object sender, EventArgs e)
{
bckMap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
using (Graphics g = Graphics.FromImage(bckMap))
{
g.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, bckMap.Width, bckMap.Height));
g.Dispose();
}
pictureBox1.BackgroundImage = bckMap;
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
Bitmap ellips = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
using (Graphics g = Graphics.FromImage(ellips))
{
g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(0, 0, ellips.Width, ellips.Height));
g.Dispose();
}
this.pictureBox1.Image = ellips;
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
Bitmap ellips = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
using (Graphics g = Graphics.FromImage(ellips))
{
g.FillRectangle(new SolidBrush(Color.Green), new Rectangle(5, 5, ellips.Width-10, ellips.Height-10));
g.Dispose();
}
this.pictureBox1.Image = ellips;
}

I finally find a solution.
I need to use paint event of picture box.
private void pbMap_Paint(object sender, PaintEventArgs e)
{
//do every think you want with picture box like draw circle or change background
}
in this event i put every think i want to do with pbMap picture box.
and when i changed some i call below function to apply my changes,
public void updateMap()
{
if (pbMap.InvokeRequired) // just if you call in thread
{
pbMap.Invoke(new Action(() => pbMap.Invalidate()));
}
else
{
pbMap.Invalidate();
}
}

Related

C# ToolStripProfessionalRender, event OnRenderItemText applied only on the first item

I'm drawing a custom toolstrip using ToolStripProfessionalRender and editing the OnRenderItemText event as follows:
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
e.Item.ForeColor = Clr.White;
e.Item.TextAlign = ContentAlignment.MiddleLeft;
e.Item.Alignment = ToolStripItemAlignment.Left;
base.OnRenderItemText(e);
if (e.Item.GetType() == typeof(ToolStripDropDownButton))
{
ToolStripDropDownButton tsmi = (ToolStripDropDownButton)e.Item;
if (tsmi.HasDropDownItems && tsmi.OwnerItem == null)
{
Rectangle bounds = tsmi.Bounds;
bounds.X = bounds.Right - 25;
bounds.Width = 25;
bounds.Y = 10;
// Draw the corner
Graphics G = e.Graphics;
SolidBrush brushw = new SolidBrush(Color.FromArgb(70,70,70));
Point[] points =
{
new Point(bounds.Right - 3, bounds.Height - 11), // point top right
new Point(bounds.Right - 3, bounds.Bottom - 14), // point bottom right
new Point(bounds.Right - 10, bounds.Bottom - 14) // point bottom left
};
G.FillPolygon(brushw, points);
}
}
}
and basically the output i'm trying to obtain is the following:
So drawing a little triangle on the bottom right corner when i got a ToolStripDropDownButton. The problem is that the little triangle is drawn only first item.
To end up the question i draw this toolstrip dynamically using a function that adds a dropdownbutton at each call.
ToolStripDropDownButton m_Item = new ToolStripDropDownButton(text, image);
m_Item.ImageAlign = ContentAlignment.MiddleCenter;
m_Item.ImageScaling = ToolStripItemImageScaling.None;
m_Item.Name = name;
m_Item.ForeColor = Color.White;
m_Item.BackColor = Color.FromArgb(95, 95, 95);
m_Item.Padding = new Padding(5);
m_Item.ShowDropDownArrow = false;
m_Item.Paint += new PaintEventHandler(this.PaintButtonBorder);
if (tabPage != null)
m_Item.Click += (sender, e) => AddClickTab(sender, e, tabPage);
((ToolStripDropDownMenu)m_Item.DropDown).ShowImageMargin = false;
((ToolStripDropDownMenu)m_Item.DropDown).ShowCheckMargin = false;
((ToolStripDropDownMenu)m_Item.DropDown).Cursor = Cursors.Hand;
toolStrip1.Items.Add(m_Item);
if (SubItems != null)
{
for(int i = 0; i < SubItems.Length; i++)
{
object[] subitem = (object[])SubItems[i];
FnAddToolStripMenuItem(
subitem[0].ToString(),
subitem[1].ToString(),
(Bitmap)subitem[2],
m_Item,
(TabPage)subitem[3]
);
}
}
Am i missing some "new" maybe?
Override the OnRenderItemText method only to draw the text part as it says, and/or to set the default properties used when rendering the text. To change the look and the shape of the arrows of the dropdown items, override the OnRenderArrow method.
Example
using System.Drawing;
using System.Drawing.Drawing2D;
protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
{
// Optional: to be the default color of the arrows.
e.ArrowColor = Color.FromArgb(70, 70, 70);
if (e.Item is ToolStripDropDownButton item && item.OwnerItem == null)
{
var g = e.Graphics;
var r = new Rectangle(item.Bounds.Width - 10, item.Bounds.Height - 10, 8, 8);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.Half;
using (var br = new SolidBrush(e.ArrowColor))
g.FillPolygon(br, new[]
{
new Point(r.Left, r.Bottom),
new Point(r.Right, r.Top),
new Point(r.Right, r.Bottom)
});
g.SmoothingMode = SmoothingMode.None;
g.PixelOffsetMode = PixelOffsetMode.Default;
}
else
base.OnRenderArrow(e);
}
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
e.Item.ForeColor = Color.White;
e.Item.TextAlign = ContentAlignment.MiddleLeft;
e.Item.Alignment = ToolStripItemAlignment.Left;
base.OnRenderItemText(e);
}
Make sure to enable the ShowDropDownArrow property of the dropdown buttons. So comment this m_Item.ShowDropDownArrow = false;.
If you are also interested to change the color according to the current state of the dropdown button (Selected, Pressed), then you can do for example:
using (var br = new SolidBrush(item.Selected
? Color.FromArgb(150, 150, 150) : item.Pressed
? Color.FromArgb(100, 100, 100) :
Color.FromArgb(70, 70, 70)))
//...

drawing flickers

have made something to draw on the screen(as overlay for everyting else) but when I move something over the drawing it starts flickering, I have already set DoubleBuffered on true
is their a way to fix this with a dll,function,rendering?
private void button2_Click(object sender, EventArgs e)
{
DoubleBuffered = true;
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Point midx = new Point(960, 960);
Point midy = new Point(540, 540);
Point LinksBoven = new Point(10, 10);
Point pt = Cursor.Position;
string letter = "drawing on";
float emSize = 12;
while (true)
{
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(0, 200, 0));
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
if (cbCircel.Checked == true)
{
g.DrawEllipse(Pens.Black, midx.X - 10, midy.Y - 10, 20, 20);
}
if (cbKruis.Checked == true)
{
g.DrawLine(myPen, 940, 540, 980, 540);
g.DrawLine(myPen, 960, 560, 960, 520);
}
pt.X = 10;
pt.Y = 10;
g.DrawString(letter, new Font(FontFamily.GenericSansSerif, emSize, FontStyle.Regular), new SolidBrush(Color.Green), pt);
}
}
}

Zoom pictures in PictureBox

I have a picture and a line in a PictureBox control. I know how to zoom the picture itself using this code :
private void pictureBox1_Click(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (Globals.x == 1)
{
Globals.y = 1;
Globals.a = e.X;
Globals.b = e.Y;
}
}
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Create a local version of the graphics object for the PictureBox.
Graphics g = e.Graphics;
if (Globals.y == 0)
{
e.Graphics.DrawImage(bitmap, 0, 0, 470, 353);
e.Graphics.DrawLine(System.Drawing.Pens.Red, 210, 66, 112, 122);
e.Graphics.DrawLine(System.Drawing.Pens.Red, 112, 122, 15, 205);
}
Bitmap bmpZoom = new Bitmap(bitmap.Width, bitmap.Height);
int new4W = bitmap.Width / 3;
int new4H = bitmap.Height / 3;
int new2W = bitmap.Width *2/ 3;
int new2H = bitmap.Height *2/ 3;
Rectangle srcRect = new Rectangle(Globals.a - new4W,Globals.b - new4H, new2W, new2H);
Rectangle dstRect = new Rectangle(60, 10, bmpZoom.Width, bmpZoom.Height);
}
My code can zoom the part of the picture that i have clicked on, but when I try to draw the line again, it's in the same location anymore (and that is normal).
I want a solution that can zoom both the picture and the line. If anyone could help me, I would be glad.

Named argument specifications must appear after all fixed arguments have been specified

I'm working in image processing in C# and I have two major error:
Error: Named argument specifications must appear after all fixed arguments have been specified
Error: System.Drawing.Size' is a 'type' but is used like a 'variable'
This is my code:
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;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.CvEnum;
using Emgu.CV.GPU;
using Emgu.CV.UI;
namespace SNAKE_C_Sharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void imageBox1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Filter = "(*.*)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Image image = Image.FromFile(dialog.FileName);
pictureBox1.Image = image;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
struct parameter
{
public double alpha { get; set; }
public double beta { get; set; }
public double gamma { get; set; }
};
unsafe private void button3_Click(object sender, EventArgs e)
{
{
int length = 1000;
MCvPoint2D64f* contour;
MCvPoint2D64f center = new MCvPoint2D64f();
var snake_param = new List<parameter>();
snake_param.Add(new parameter { alpha = 0.1, beta = 0.1, gamma = 0.1, });
//Image src_img = pictureBox1.Image;
IntPtr dst_img = new IntPtr();
//IntPtr src_img = Emgu.CV.CvInvoke.cvLoadImage("pictureBox1.Image", Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR);
Bitmap bitmapp = new Bitmap("pictureBox1.Image");
Image<Bgr, byte> image = new Image<Bgr, byte>(bitmapp);
center.x = image.Width;
center.y = image.Height;
int i;
for (i = 0; i < length; i++)
{
contour[i].x = (int)(center.x * Math.Cos(2 * Math.PI * i / length) + center.x);
contour[i].y = (int)(center.y * Math.Sin(2 * Math.PI * i / length) + center.y);
}
for (i = 0; i < length - 1; i++)
{
CvInvoke.cvLine(dst_img, contour[i], contour[i + 1], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED,0);
}
CvInvoke.cvLine(dst_img, contour[length - 1], contour[0], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED, 0);
IntPtr src_img = image.Ptr;
CvInvoke.cvSnakeImage(src_img, contour, length, snake_param[1].alpha, snake_param[2].beta, snake_param[3].gamma, 1.0f, contour[i], System.Drawing.Size(15, 15), new MCvTermCriteria(1, 0.0), true);
CvInvoke.cvCvtColor(src_img, dst_img, COLOR_CONVERSION.GRAY2RGB);
for (i = 0; i < length - 1; i++)
{
CvInvoke.cvLine(dst_img, contour[i], contour[i + 1], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED, 0);
}
CvInvoke.cvLine(dst_img, contour[length - 1], contour[0], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED, 0);
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
Bitmap bitmappbb = new Bitmap("dst_img");
Image<Bgr, byte> imagee = new Image<Bgr, byte>(bitmapp);
pictureBox2.Image = bitmappbb;
}
}
private void imageBox1_Click_1(object sender, EventArgs e)
{
}
private void panAndZoomPictureBox1_Click(object sender, EventArgs e)
{
}
private void imageBox1_Click_2(object sender, EventArgs e)
{
}
}
}
How can i adjust above error?
This is one of the issues that caused error 1
CvInvoke.cvLine(dst_img, contour[i], contour[i + 1], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED,0);
I'll make it more readable...
CvInvoke.cvLine(
dst_img,
contour[i],
contour[i + 1],
new MCvScalar(255, 0, 0),
2,
lineType: LINE_TYPE.EIGHT_CONNECTED,
0
);
See the 2nd-to-last line is using a named argument (lineType:), but is followed by a non-named argument? How is the compiler meant to know what you mean?
The 2nd error is as #LajosArpad stated, you need to add a new in front of your use of System.Drawing.Size(..).
Instead of
CvInvoke.cvSnakeImage(src_img, contour, length, snake_param[1].alpha, snake_param[2].beta, snake_param[3].gamma, 1.0f, contour[i], System.Drawing.Size(15, 15), new MCvTermCriteria(1, 0.0), true);
you need this:
CvInvoke.cvSnakeImage(src_img, contour, length, snake_param[1].alpha, snake_param[2].beta, snake_param[3].gamma, 1.0f, contour[i], new System.Drawing.Size(15, 15), new MCvTermCriteria(1, 0.0), true);
This should fix the second error. Without the new keyword you do not have a System.Drawing.Size instance.
EDIT:
I am not going to test your code, nor to read it line-by-line, so I expect more information about your first error to give you the solution. Can you tell me on which line is the exception thrown?
Also, might I suggest that you should pay more attention to the tabulation of your code, as it is difficult to read if you write your code in such an unstructured manner. It is not impossible to read, but most of us (including myself) will not read it.
I fixed the last error and that's my new code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Filter = "JPEG|*.jpg|PNG|*.PNG";
if (dialog.ShowDialog() == DialogResult.OK)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Image image = Image.FromFile(dialog.FileName);
pictureBox1.Image = image;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
struct parameter
{
public float alpha { get; set; }
public float beta { get; set; }
public float gamma { get; set; }
};
unsafe private void button3_Click(object sender, EventArgs e)
{
{
int length = 1000;
Point *contour;
Point center = new Point();
var snake_param = new List<parameter>();
snake_param.Add(new parameter { alpha= 0.1f , beta = 0.1f, gamma= 0.1f, });
IntPtr dst_img= new IntPtr();
Bitmap bitmap = new Bitmap("pictureBox1.Image");
Image<Bgr, byte> image = new Image<Bgr, byte>(bitmap);
center.X = image.Width;
center.Y = image.Height;
int i;
for (i = 0; i < length; i++)
{
contour[i].X = (int)(center.X * Math.Cos(2 * Math.PI * i / length) + center.X);
contour[i].Y = (int)(center.Y * Math.Sin(2 * Math.PI * i / length) + center.Y);
}
LINE_TYPE lignetype = new LINE_TYPE();
for (i = 0; i < length - 1; i++)
{
CvInvoke.cvLine(
dst_img,
contour[i],
contour[i + 1],
new MCvScalar(255,0,0),
2,
LINE_TYPE.EIGHT_CONNECTED,
0 );
}
CvInvoke.cvLine
(
dst_img,
contour[length - 1],
contour[0],
new MCvScalar(255,0,0),
2,
LINE_TYPE.EIGHT_CONNECTED,
0
);
IntPtr ctr =new IntPtr();
//public void PixelToInkSpace(
//IntPtr a
//ref Point contour
//);
IntPtr src_img = image.Ptr;
CvInvoke.cvSnakeImage(
src_img,
contour[i],
length,
snake_param.[1].alfa,
snake_param[2].beta,
snake_param[3].gamma,
1,
new System.Drawing.Size(15, 15),
new MCvTermCriteria(1,0.0),
1);
CvInvoke.cvCvtColor(
src_img,
dst_img,
COLOR_CONVERSION.GRAY2RGB );
for (i = 0; i < length - 1; i++)
{
CvInvoke.cvLine(
dst_img,
contour[i],
contour[i + 1],
new MCvScalar(255,0,0),
2,
LINE_TYPE.EIGHT_CONNECTED,
0 );
}
CvInvoke.cvLine(
dst_img,
contour[length - 1],
contour[0],
new MCvScalar(255,0,0),
2,
LINE_TYPE.EIGHT_CONNECTED,
0);
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
Bitmap bitmappbb = new Bitmap("dst_img");
Image<Bgr, byte> imagee = new Image<Bgr, byte>(bitmappbb);
pictureBox2.Image = bitmappbb;
}
}
}
}
But my error now is different as I'm translating my code from c++ to c# ,
I discover that the snake format is
public static void cvSnakeImage(
IntPtr image,
IntPtr points,
int length,
float[] alpha,
float[] beta,
float[] gamma,
int coeffUsage,
Size win,
MCvTermCriteria criteria,
bool calcGradient
)
I didn't find way to convert the variable "contour" with type "Point" to "IntPtr".
And a way to call alfa, beta et gamma as float[];
#Timothy Walters

Change color of panel when mouse clicked?

I am very new to visual C#
I want to make a ticket booking system (like in cinemas), I have created the seats using panel, each seating is 40 * 40
Here's my code:
private void panel2_Paint(object sender, PaintEventArgs e)
{
int a, b;
for (a = 0; a <= 1; a++)
{
for (b = 0; b < 12; b++)
{
Graphics g = e.Graphics;
g.FillRectangle(new SolidBrush(Color.White), b * 40, a * 40, 40, 40);
g.DrawRectangle(new Pen(Color.Black), b * 40, a * 40, 40, 40);
}
}
}
Now I want to change the color of each seating by a mouse click to show what seat has been selected; but so far no luck
You would be better off creating seperate controls for each of your selected seats and handling their Click events. In this example I added 24 PictureBox's to the Panel. I then placed their index in the Tag Property of the Control and attached a common Click Event Handler. I am also using a Bool array to keep track of selected status.
public partial class Form1 : Form
{
bool[] selected = new bool[24];
public Form1()
{
InitializeComponent();
foreach (PictureBox pb in panel1.Controls)
{
pb.Click += new EventHandler(pictureBox_Click);
}
}
private void pictureBox_Click(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
int index ;
if (int.TryParse(pb.Tag.ToString(), out index))
{
if (selected[index])
{
selected[index] = false;
pb.BackColor = Color.White;
}
else
{
selected[index] = true;
pb.BackColor = Color.Red;
}
}
}
}
You can use what you have if you create a boolean array to store the state of the Seat, use the Panel's MouseDown Event to set the variable and Invalidate the screeen rectangle that is accociated with your seat.
Something like this.
public partial class Form1 : Form
{
bool[,] selected = new bool[2,12];
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
int a, b;
for (a = 0; a <= 1; a++)
{
for (b = 0; b < 12; b++)
{
if (selected[a, b])
{
e.Graphics.FillRectangle(new SolidBrush(Color.Red), b * 40, a * 40, 40, 40);
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.White ), b * 40, a * 40, 40, 40);
}
e.Graphics.DrawRectangle(new Pen(Color.Black), b * 40, a * 40, 40, 40);
}
}
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
int xPos, yPos;
xPos = e.X / 40;
yPos = e.Y / 40;
if ((xPos > 11) || (yPos > 1)) return;
if(selected[yPos,xPos])
selected[yPos, xPos] = false;
else
selected[yPos, xPos] = true;
((Panel)sender).Invalidate(new Rectangle(xPos * 40,yPos *40,40,40)) ;
}
}
Rather than using a Graphics object and drawing directly to the form, you may be able to get away with simply setting the BackColor property of the control that corresponds to the selected seat when the OnMouseClick event fires.

Categories

Resources