How would I create a 6x7 grid inside of a panel box in windows form C# on visual studio. I have tried using DrawLine, and Graphics but it is not working.
May someone assist me?
When running the program I was hoping it would look like this:
Hopefully your code looked something like:
private void Form1_Load(object sender, EventArgs e)
{
panel1.SizeChanged += Panel1_SizeChanged;
panel1.Paint += Panel1_Paint;
}
private void Panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int cols = 7;
int rows = 6;
int width = panel1.Width / cols;
int height = panel1.Height / rows;
for(int col=1; col<cols; col++)
{
e.Graphics.DrawLine(Pens.Black, new Point(col * width, 0), new Point(col * width, panel1.Height));
}
for(int row=1; row<rows; row++)
{
e.Graphics.DrawLine(Pens.Black, new Point(0, row * height), new Point(panel1.Width, row * height));
}
}
private void Panel1_SizeChanged(object sender, EventArgs e)
{
panel1.Invalidate();
}
Related
I'm doing a school project using C# in which I designed a Form in C# that I can draw on and now I need to copy what I draw into a 9 different Images and save them. So far, I tried using the copy from screen function like that:
Size s = this.Size;
Bitmap memoryImage = new Bitmap(s.Width, s.Height, this.CreateGraphics());
Graphics.FromImage(memoryImage).CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
memoryImage.Save(#"C:\Users\omerm\Desktop\projectFinals\ProjectFiles\C-Sharp\Drawing\WinFormsApp1\Digits\1.jpg");
My problem is that the image I get from this code includes parts of my screen that aren't part of my Form. And I also need to copy 9 different images who are fractions of this one instead of just the one and make sure those 9 are around the same shape(squares). does anyone know how to do that?
I'm terrible at explaining this so think that I need to take the original image and cut into 9 like in a tic tac toe board and save each one of them as a different image in the shape of squares instead of the original.
full code:
namespace WinFormsApp1
{
public partial class Form1 : Form
{
Pen Pen = new Pen(Color.Black, 2);
int pX, pY;
Graphics g;
public object MemoryImage { get; private set; }
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
pX = e.X;
pY = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Graphics g = pictureBox1.CreateGraphics();
g.DrawLine(Pen, pX, pY, e.X, e.Y);
pX = e.X;
pY = e.Y;
}
}
private void button1_Click(object sender, EventArgs e)
{
Size s = this.Size;
Bitmap memoryImage = new Bitmap(s.Width, s.Height, this.CreateGraphics());
Graphics.FromImage(memoryImage).CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
memoryImage.Save(#"C:\Users\omerm\Desktop\projectFinals\ProjectFiles\C-Sharp\Drawing\WinFormsApp1\Digits\1.jpg");
}
private void ChooseColor(object sender, EventArgs e)
{
Pen.Color = ((PictureBox)sender).BackColor;
}
}
}
Tried looking online but what I look for is really specific.
Here's an example of persisting the data in a structure and drawing it in the Paint() event. Additionally, I've demonstrated how to tell the PictureBox to draw itself instead of copying the screen:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private List<Point> curSquiggle;
private List<List<Point>> Squiggles = new List<List<Point>>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
curSquiggle = new List<Point>();
curSquiggle.Add(e.Location);
Squiggles.Add(curSquiggle);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
curSquiggle.Add(e.Location);
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach(List<Point> squiggle in Squiggles)
{
e.Graphics.DrawLines(Pens.Black, squiggle.ToArray());
}
}
private void button1_Click_1(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(pictureBox1.ClientRectangle.Width, pictureBox1.ClientRectangle.Height);
pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
// Do something with the bmp, like display it in another form:
Form frm = new Form();
frm.AutoSize = true;
PictureBox pb = new PictureBox();
pb.SizeMode = PictureBoxSizeMode.AutoSize;
pb.Image = bmp;
pb.Location = new Point(0, 0);
frm.Controls.Add(pb);
frm.Show();
}
}
Example run:
Here's a different version of the save code which splits the main picture into 9 pieces:
private void button1_Click_1(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(pictureBox1.ClientRectangle.Width, pictureBox1.ClientRectangle.Height);
pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
int cols = 3;
int rows = 3;
int cellWidth = bmp.Width / cols;
int cellHeight = bmp.Height / rows;
Rectangle destRect = new Rectangle(0, 0, cellWidth, cellHeight);
List<Bitmap> cells = new List<Bitmap>();
for(int r=0; r<rows; r++)
{
for(int c=0; c<cols; c++)
{
Bitmap cell = new Bitmap(cellWidth, cellHeight);
Rectangle srcRect = new Rectangle(c * cellWidth, r * cellHeight, cellWidth, cellHeight);
using (Graphics g = Graphics.FromImage(cell))
{
g.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel);
}
cells.Add(cell);
}
}
foreach(Bitmap cell in cells)
{
Form frm = new Form();
frm.AutoSize = true;
frm.AutoSizeMode = AutoSizeMode.GrowAndShrink;
PictureBox pb = new PictureBox();
pb.SizeMode = PictureBoxSizeMode.AutoSize;
pb.Image = cell;
pb.Location = new Point(0, 0);
frm.Controls.Add(pb);
frm.Show();
}
}
Splitting into nine pieces:
I'm trying to learn C# by developing a small little breakout game. It is coming along fine but I am struggling with moving the ball up, down, left and right based on the user clicks. I don't even know where to start for it as my code is that messy. Here is my code:
public partial class Form2 : Form
{
Graphics drawingArea, paperBall, paperBat;
Pen penBall;
SolidBrush brushBAt = new SolidBrush(Color.Black);
private int x, y, xChange, yChange;
Random ranNum;
public Form2()
{
InitializeComponent();
drawingArea = playground.CreateGraphics();
paperBall = playground.CreateGraphics();
paperBat = playground.CreateGraphics();
lblLives.Text = "5";
lblScore.Text = "0";
ranNum = new Random();
drawBricks();
}
private void btnYes_Click(object sender, EventArgs e)
{
drawBricks();
drawBat();
drawBall();
}
public void drawBricks()
{
SolidBrush blackpen = new SolidBrush(Color.Black);
SolidBrush redpen = new SolidBrush(Color.Red);
SolidBrush yellowpen = new SolidBrush(Color.Yellow);
SolidBrush bluepen = new SolidBrush(Color.Blue);
SolidBrush greenpen = new SolidBrush(Color.Green);
drawingArea.FillRectangle(blackpen, 0, 0, 80, 25);
drawingArea.FillRectangle(redpen, 150, 0, 80, 25);
drawingArea.FillRectangle(yellowpen, 350, 0, 80, 25);
drawingArea.FillRectangle(bluepen, 550, 0, 80, 25);
drawingArea.FillRectangle(greenpen, 750, 0, 80, 25);
}
public void drawBat()
{
paperBat = playground.CreateGraphics();
playground.MouseMove += new
System.Windows.Forms.MouseEventHandler(picDraw_MouseHover);
//drawBall();
}
private void picDraw_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
//paperBat.Clear(Color.White);
}
private void picDraw_MouseHover(object sender, System.Windows.Forms.MouseEventArgs e)
{
paperBat.FillRectangle(brushBAt, e.X + -30, playground.Height - 15, 75, 10); //First one: high up it is Second one: how long it is Third one:
drawBricks();
}
public void drawBall()
{
penBall = new Pen(Color.Red);
penBall.Width = 10;
timer1.Interval = 50;
timer1.Enabled = true;
x = ranNum.Next(-10, playground.Height); //MAkes the ball spawn randomly
y = ranNum.Next(-10, playground.Width); //MAkes the ball spawn randomly
xChange = ranNum.Next(1, 20); yChange = ranNum.Next(1, 20); //Changes speed
//drawBricks();
//drawBat();
}
private void timer1_Tick(object sender, EventArgs e)
{
x = x + xChange;
y = y + yChange;
if (x >= playground.Width)
xChange = -xChange;
if (y >= playground.Height)
yChange = -yChange;
if (x <= 0)
xChange = -xChange;
if (y <= 0)
yChange = -yChange;
// paperBall.Clear(Color.White);
paperBat.Clear(Color.White);
paperBall.DrawEllipse(penBall, x, y, 10, 10); //Height and width of ball
drawBricks();
}
private void btnUp_Click(object sender, EventArgs e)
{
}
private void btnRight_Click(object sender, EventArgs e)
{
}
private void btnLeft_Click(object sender, EventArgs e)
{
}
private void btnDown_Click(object sender, EventArgs e)
{
}
}
}
Can anyone help me please?
You will need to add a KeyDown event handler to your picDraw, then code what each key does (changing the ball position).
Here's an example from Microsoft: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeypress(v=vs.110).aspx
You migth need to look up ascii key codes for specific keys if i remember correctly.
In the future consider using Unity as at the moment you are not using the correct technology for what you are trying to achieve.
first sorry for my English. I am programming application in windows forms. It is something like Packet Tracer. I have four buttons. When I click on them, they dynamicaly create pictureboxes with picture of Router or Switch,.... Each time I click on the button, new picture box(Switch or Router,...), is created. I can also move with this pictureboxes by mouse.
I need to create a button, which connects selected pictureboxes with line(Cable). This pictureboxes should be selected by click on them. It sholud be able to move with this objects(movable line).
Here 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.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
PictureBox[] picturebox = new PictureBox[100];
public Form1()
{
InitializeComponent();
}
private void router_Click(object sender, EventArgs e)
{
++a;
picturebox[a] = new PictureBox();
picturebox[a].Name = "picturebox" + a;
picturebox[a].Location = new Point(0 + (a-1) *100,100);
picturebox[a].Size = new Size(70, 70);
picturebox[a].BorderStyle = BorderStyle.None;
picturebox[a].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[a]);
picturebox[a].Image = Image.FromFile(#"D:\\router.jpg");
picturebox[a].Refresh();
picturebox[a].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[a].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[a].MouseUp += new MouseEventHandler(picMouseUp);
}
private void Form1_Load(object sender, EventArgs e)
{
}
int x = 0;
int y = 0;
bool drag = false;
private void picMouseDown(object sender, MouseEventArgs e)
{
// Get original position of cursor on mousedown
x = e.X;
y = e.Y;
drag = true;
}
private void picMouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
PictureBox pb = (PictureBox)sender;
// Get new position of picture
pb.Top += e.Y - y;
pb.Left += e.X - x;
pb.BringToFront();
}
}
private void picMouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
private void switch1_Click(object sender, EventArgs e)
{
++b;
picturebox[b] = new PictureBox();
picturebox[b].Name = "picturebox" + b;
picturebox[b].Location = new Point(0 + (b - 1) * 100, 180);
picturebox[b].Size = new Size(70, 70);
picturebox[b].BorderStyle = BorderStyle.None;
picturebox[b].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[b]);
picturebox[b].Image = Image.FromFile(#"D:\HP ProBook 450\Desktop\Grafika\switch1.png");
picturebox[b].Refresh();
picturebox[b].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[b].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[b].MouseUp += new MouseEventHandler(picMouseUp);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pc_Click(object sender, EventArgs e)
{
++c;
picturebox[c] = new PictureBox();
picturebox[c].Name = "picturebox" + c;
picturebox[c].Location = new Point(0 + (c - 1) * 100, 260);
picturebox[c].Size = new Size(70, 70);
picturebox[c].BorderStyle = BorderStyle.None;
picturebox[c].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[c]);
picturebox[c].Image = Image.FromFile(#"D:\HP ProBook 450\Desktop\pc.jpg");
picturebox[c].Refresh();
picturebox[c].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[c].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[c].MouseUp += new MouseEventHandler(picMouseUp);
}
private void server_Click(object sender, EventArgs e)
{
++d;
picturebox[d] = new PictureBox();
picturebox[d].Name = "picturebox" + d;
picturebox[d].Location = new Point(0 + (d - 1) * 100, 340);
picturebox[d].Size = new Size(70, 70);
picturebox[d].BorderStyle = BorderStyle.None;
picturebox[d].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[d]);
picturebox[d].Image = Image.FromFile(#"D:\HP ProBook 450\Desktop\server.png");
picturebox[d].Refresh();
picturebox[d].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[d].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[d].MouseUp += new MouseEventHandler(picMouseUp);
}
}
}
THank you for your help.
You need to invalidate the parent when you add a picturebox or when you move a picturebox:
(picMouseMove and 4 times in the click handlers, it would be better to use 1 function)
Invalidate();
This is an example OnPaint, drawing lines between the pictureboxes as they are located in the Controls collection: (your picturebox array seems very weird, you always add controls at index 1, always overwriting the previous entry?! i'd suggest using a List if you need to keep a reference to them)
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var pictureBoxes = Controls.OfType<PictureBox>().ToArray();
if (pictureBoxes.Length > 1)
{
for (int i = 1; i < pictureBoxes.Length; i++)
{
DrawLineBetween(e.Graphics, pictureBoxes[i - 1], pictureBoxes[i]);
}
}
}
This function can be used to draw a line between 2 of your boxes:
private void DrawLineBetween(Graphics g, PictureBox from, PictureBox to)
{
g.DrawLine(Pens.Black,
new Point(from.Left + from.Width / 2, from.Top + from.Height / 2),
new Point(to.Left + to.Width / 2, to.Top + to.Height / 2));
}
----- full sample below -----
I refactored your full example, and added the code above to start you off with a working example:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
List<PictureBox> pictureboxes = new List<PictureBox>();
public Form1()
{
InitializeComponent();
}
private void AddPictureBox(string imagePath)
{
var pb = new PictureBox();
pb.Name = "picturebox" + pictureboxes.Count;
pb.Location = new Point(pictureboxes.Count * 100, 100);
pb.Size = new Size(70, 70);
pb.BorderStyle = BorderStyle.None;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(pb);
pb.Image = Image.FromFile(imagePath);
pb.Refresh();
pb.MouseDown += new MouseEventHandler(picMouseDown);
pb.MouseMove += new MouseEventHandler(picMouseMove);
pb.MouseUp += new MouseEventHandler(picMouseUp);
pictureboxes.Add(pb);
Invalidate();
}
private void router_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\\router.jpg");
}
private void Form1_Load(object sender, EventArgs e)
{
}
int x = 0;
int y = 0;
bool drag = false;
private void picMouseDown(object sender, MouseEventArgs e)
{
// Get original position of cursor on mousedown
x = e.X;
y = e.Y;
drag = true;
}
private void picMouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
PictureBox pb = (PictureBox)sender;
// Get new position of picture
pb.Top += e.Y - y;
pb.Left += e.X - x;
pb.BringToFront();
Invalidate();
}
}
private void picMouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
private void switch1_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\Grafika\switch1.png");
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pc_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\pc.jpg");
}
private void server_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\server.png");
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (pictureboxes.Count > 1)
{
var arr = pictureboxes.ToArray();
for (int i = 1; i < arr.Length; i++)
{
DrawLineBetween(e.Graphics, arr[i - 1], arr[i]);
}
}
}
private void DrawLineBetween(Graphics g, PictureBox from, PictureBox to)
{
g.DrawLine(Pens.Black,
new Point(from.Left + from.Width / 2, from.Top + from.Height / 2),
new Point(to.Left + to.Width / 2, to.Top + to.Height / 2));
}
}
}
i have button and i want the button background change when i aim my cursor at the button
so i use mouseenter and mouseleave,it work but i want smoother effect when it change the background
sorry for bad english, and i'm green at c#
please help me
my script
private void button1_Click(object sender, EventArgs e)
{
}
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.administrator_icon));
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.alhaq));
}
}
The idea is the same as in this answer. Instead of using a Timer to change the alpha of the BackColor you will want to change the Index of an ImageList of your Button.
Changing it is very similar to the other post:
private void button3_MouseEnter(object sender, EventArgs e)
{
timer1.Interval = 25;
timer1.Start();
}
private void button3_MouseLeave(object sender, EventArgs e)
{
timer1.Stop();
button3.ImageIndex = 0;
index = 1;
}
int index = 1;
private void timer1_Tick(object sender, EventArgs e)
{
index++; ;
button3.ImageIndex = index;
if (index >= imageList1.Images.Count)
{ timer1.Stop(); button3.ImageIndex = 1; index = 1; }
}
The differences are in the ImageList. Unless you want to create it frame by frame, a simple routine to create it dynamically will do the job. First you must assign the inactive and the active images to index 0 and 1 respectively. Then calling this will create step extra images from image nr 1:
public void makeTransPix(int steps)
{
using (Bitmap bmp = new Bitmap( imageList1.Images[1] ) )
{
for (int t = 0; t < steps; t++)
{
Bitmap tBmp = new Bitmap(bmp.Width, bmp.Height);
using (var g = Graphics.FromImage(tBmp))
{
Rectangle R = new Rectangle(0, 0, bmp.Width, bmp.Height);
using (var brush = new SolidBrush(button1.BackColor))
g.FillRectangle(brush, R);
var colorMatrix = new ColorMatrix();
colorMatrix.Matrix33 = t / (float)steps;
var imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(
colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(bmp, R, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttributes);
}
imageList1.Images.Add(tBmp);
}
}
}
You can adjust the steps to vary the speed. You can call it in the Form_Shown event. Make sure all image sizes fit and give your ImageList a ColorDepth of 32!
So I've been putting this graphics transformation program together and suddenly some change I can't figure out has made the app unresponsive. The menus no longer function, and it's supposed to draw axes and a grid on one of the panels... nothing. Any ideas?
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 System.IO;
namespace Transformer
{
public partial class Transformer : Form
{
/* Initialize parameters */
private bool drawAxes = true;
private bool drawGrid = true;
private List<ObjectSettings> dispObjects = new List<ObjectSettings>();
/* Initialize form */
public Transformer()
{
InitializeComponent();
}
private void Transformer_Load(object sender, EventArgs e)
{
// Populate available objects listbox
string currentDir = Directory.GetCurrentDirectory();
string[] fileEntries = Directory.GetFiles(currentDir + #"\Objects");
foreach (string s in fileEntries) {
int start = s.LastIndexOf(#"\");
int end = s.LastIndexOf(#".");
availObjectsListBox.Items.Add(s.Substring(start + 1, end - start - 1));
} // end foreach
}
/* Paint graphics */
// Paint main form
private void Transformer_Paint(object sender, PaintEventArgs e)
{
}
// Paint graphics panel
private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e)
{
Rectangle r = splitContainer2.Panel1.ClientRectangle;
Graphics g = splitContainer2.Panel1.CreateGraphics();
Pen axisPen = new Pen(Color.Gray, 2.0f);
Pen gridPen = new Pen(Color.Gray, 1.0f);
g.Clear(Color.White);
if (drawAxes) {
g.DrawLine(axisPen, r.Left + 0.5f * r.Width, r.Top, r.Left + 0.5f * r.Width, r.Bottom);
g.DrawLine(axisPen, r.Left, r.Top + 0.5f * r.Height, r.Right, r.Top + 0.5f * r.Height);
}
if (drawGrid) {
// Vertical lines
int xVal = 0;
int xCenter = r.Width / 2;
g.DrawLine(gridPen, xCenter, r.Top, xCenter, r.Bottom);
for (int i = 0; i < 10; i++) {
xVal += r.Width / 20;
g.DrawLine(gridPen, xCenter + xVal, r.Top, xCenter + xVal, r.Bottom);
g.DrawLine(gridPen, xCenter - xVal, r.Top, xCenter - xVal, r.Bottom);
}
// Horizontal lines
int yVal = 0;
int yCenter = r.Height / 2;
g.DrawLine(gridPen, r.Left, yCenter, r.Right, yCenter);
for (int i = 0; i < 10; i++) {
yVal += r.Height / 20;
g.DrawLine(gridPen, r.Left, yCenter + yVal, r.Right, yCenter + yVal);
g.DrawLine(gridPen, r.Left, yCenter - yVal, r.Right, yCenter - yVal);
}
}
// foreach object in displayed objects
// keep list of displayed objects and their settings (make struct)
g.Dispose();
axisPen.Dispose();
gridPen.Dispose();
}
/* File menu */
private void saveImageToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
/* Options menu */
private void axesOnoffToolStripMenuItem_Click(object sender, EventArgs e)
{
if (drawAxes == true)
drawAxes = false;
else
drawAxes = true;
}
private void gridOnoffToolStripMenuItem_Click(object sender, EventArgs e)
{
if (drawGrid == true)
drawGrid = false;
else
drawGrid = true;
}
/* Help menu */
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox dlg = new AboutBox();
dlg.ShowDialog();
}
/* Other stuff */
private void timer1_Tick(object sender, EventArgs e)
{
Invalidate();
}
// ">>" button
private void availToDispButton_Click(object sender, EventArgs e)
{
dispObjectsListBox.Items.Add(availObjectsListBox.SelectedItem);
}
// "<<" button
private void dispToAvailButton_Click(object sender, EventArgs e)
{
availObjectsListBox.Items.Add(dispObjectsListBox.SelectedItem);
dispObjectsListBox.Items.Remove(dispObjectsListBox.SelectedItem);
}
// Clear all button
private void clearAllButton_Click(object sender, EventArgs e)
{
}
// Update preview box
private void availObjectsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Thanks!
Try commenting out (separately) the "load" and "paint" code, see which is the problem.
If the problem is the paint... I wonder - rather than creating your own Graphics, use the one given to you? Namely, e.Graphics. Note that you didn't create this, so it isn't your job to Dispose() it (so don't do that). I would also cache the Pen etc in fields rather than create them each time. Note that if you do create a Pen (etc) in a method, then using is a better way to Dispose() it.
There is also a foreach comment in the paint code that suggests something has been removed - this may be relevant to the problem...
If this is happening when loading then obviously the amount of files in the directory could be causing the GUI thread to hang.
Other then that my quick look through only makes me think to check the bools you are using to control drawing and to make sure that the panel you are using the paint event for is actually visible.
You should also check that your timer is actually ticking, and check its interval.
I would also look at using the using statement, or at least a finally block for your dispose. But that isn't what your question is about.
Most of those are obvious and you might have already checked them all before posting here, but I thought I would put up something in case you had missed it. Hopefully I will get a chance to look through in more detail and spot something else.