Hover event incorrectly triggers - c#

New to stack overflow so hoping someone knows what's going wrong here.
For some reason whenever I hover over a button in my c# forms application, it causes a reset in my code. This seems weird to me considering I don't actually have any hover events on the button. I even tried adding another button and even when I do nothing to it, hovering still causes the reset.
The program is supposed to fill up the tank with colors and the color can be changed while it is filling up. Once it is filled up it resets to empty. It should start filling the next time the slider governing fill speed is interacted with.
It does this correctly but it also starts filling again when I hover over the button.
namespace Assignment5A
{
public partial class MainForm : Form
{
public float streamHeight = 370;
public float lastWaterHeight = 0;
public float waterHeight = 0;
public float waterBottom = 500;
public float fillSpeed = 300;
public Color brushColor = Color.LightBlue;
public Graphics g;
public Pen pen;
public Brush brush = new SolidBrush(Color.LightBlue);
public MainForm()
{
InitializeComponent();
this.Width = 500;
this.Height =600;
this.BackColor = Color.Black;
SpeedTimer.Interval = (int)fillSpeed;
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
g = e.Graphics;
pen = new Pen(Color.White);
g.DrawLine(pen, 50, 200, 50, 500);
g.DrawLine(pen, 350, 200, 350, 500);
g.DrawLine(pen, 50, 500, 350, 500);
SpeedTimer.Start();
}
private void SpeedTimer_Tick(object sender, EventArgs e)
{
if (waterHeight < 270)
{
brush = new SolidBrush(brushColor);
g = this.CreateGraphics();
g.FillRectangle(brush, 108, 136, 20, waterBottom - 136 - waterHeight);
waterHeight += 1f;
g.FillRectangle(brush, 51, waterBottom - waterHeight, 299, waterHeight - lastWaterHeight);
lastWaterHeight = waterHeight;
}
else
{
SpeedTimer.Stop();
waterHeight = 0;
lastWaterHeight = 0;
brush = new SolidBrush(Color.Black);
g.FillRectangle(brush, 51, 136, 299, 364);
}
}
private void Speed_Scroll(object sender, EventArgs e)
{
if (waterHeight < 270)
{
float scrollValue = Speed.Value;
fillSpeed = 300 / scrollValue;
SpeedTimer.Interval = (int)fillSpeed;
}
else
{
brush = new SolidBrush(Color.Black);
g.FillRectangle(brush, 51, 230, 299, 270);
SpeedTimer.Start();
}
}
private void ColorButton_Click(object sender, EventArgs e)
{
SetColor.ShowDialog();
brushColor = SetColor.Color;
}
}
}

OK, my first answer was completely wrong, must have read the question wrong, sorry.
The answer still comes down to your MainForm_Paint event, though. This event is fired whenever the form is drawn or redrawn, and includes anything on it (like your buttons). When your mouse hovers over an element it needs to be redrawn, as well as any of its parents, right back to the form level. It also occurs if the form is resized, comes back into view after being hidden (either partially or completely), goes off screen and comes back on, etc, etc. Lots of different things will trigger a form's Paint event to fire. And in your MainForm_Paint event, you have this line:
SpeedTimer.Start();
...which causes the timer to start and everything starts all over again.
Instead of using MainForm_Paint, I'd probably suggest you use the form's Load event to set up all these initial conditions. That will only fire once after the form has initialised and is shown on screen.

Related

How to change color of ellipse when new ellipse is draw in a timer in C#

I have a function which is drawing an ellipse. I want to make the previously drawn ellipse invisible by changing its color as the same color of its background when a new ellipse is drawn by changing the form size.
This is my function in my class:
class ClassClock
{
public static void drawClock(Point m, int s, Form frm, Color myColor)
{
Graphics paper = frm.CreateGraphics();
Pen myPen = new Pen(myColor);
int w = frm.ClientSize.Width;
int h = frm.ClientSize.Height;
m = new Point(w / 2, h / 2);
s = Math.Min(w, h) / 2;
paper.DrawEllipse(myPen, m.X - s, m.Y - s, s * 2, s * 2);
}
}
and this is my timer:
private void timer1_Tick(object sender, EventArgs e)
{
ClassClock.drawClock(m, s, this, this.BackColor);
ClassClock.drawClock(m, s, this, Color.Black);
}
Can someone help me find a solution to this?
You should not use CreateGraphics like this. Instead, override the OnPaint method of your form and do all your painting in that method.
Windows uses an immediate mode graphics system. That means that once your ellipse is drawn, it's gone, save for the pixels currently on the screen. If the window is minimized or another window is dragged across it, the ellipse will be gone and will have to be repainted. That is what the OnPaint method is for.
Here's a simple form that changes the color of your circle when a button is clicked. To run this code, you will need to add a button to the form and name it btnChangeColor.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Property to hold the current color. This could be a private field also.
public Color CurrentColor { get; set; } = Color.Red;
//Used to generate a random number when the button is clicked.
private Random rnd = new Random();
//All painting of the form should be in this method.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//Use the graphics event provided to you in PaintEventArgs
Graphics paper = e.Graphics;
int w = this.ClientSize.Width;
int h = this.ClientSize.Height;
Point m = new Point(w / 2, h / 2);
int s = Math.Min(w, h) / 2;
//It is important to dispose of any pens you create
using (Pen myPen = new Pen(CurrentColor))
{
paper.DrawEllipse(myPen, m.X - s, m.Y - s, s * 2, s * 2);
}
}
//When the button is clicked, the `CurrentColor` property is set to a random
//color and the form is refreshed to get it to repaint itself.
private void btnChangeColor_Click(object sender, EventArgs e)
{
//Change the current color
CurrentColor = Color.FromArgb(255, rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
//Refresh the form so it repaints itself
this.Refresh();
}
}

How to operate on drawn graphics

I created two Rectangles. I want to add events on them. For example when mouse hover on one, the hovered one will change color, can do resize or drag them(rectangles) to other place...
I was just wondering if I could control the drawn graphic, or it will like Microsoft Paint that after you painted, the object can not be operate unless you clear canvas and do redraw.
Is it possible to control a drawn graphic, thanks for any suggestion.
Simple code of my drawn graphics
private void Form1_Paint(object sender, PaintEventArgs e)
{
// Create pen.
Pen blackPen = new Pen(Color.Black, 3);
// Create rectangle.
Rectangle rect1 = new Rectangle(20, 20, 250, 250);
Rectangle rect2 = new Rectangle(70, 70, 150, 150);
// Draw rectangle to screen.
e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rect1);
e.Graphics.FillRectangle(Brushes.LightBlue, rect2);
}
Also, you can create your own control like:
class RectangleControl : Control
{
public void FillRectangle(Color color)
{
this.BackColor = color;
}
}
Then :
private void Form1_Paint(object sender, PaintEventArgs e)
{
RectangleControl rect1 = new RectangleControl() { Parent = this, Left = 20, Top = 20, Width = 250, Height = 250 };
rect1.FillRectangle(Color.DeepSkyBlue);
RectangleControl rect2 = new RectangleControl() { Parent = rect1, Left = 50, Top = 50, Width = 150, Height = 150 };
rect2.FillRectangle(Color.LightBlue);
rect1.MouseHover += Rect1_MouseHover;
rect2.MouseLeave += Rect2_MouseLeave;
}
private void Rect2_MouseLeave(object sender, EventArgs e)
{
(sender as RectangleControl).BackColor = Color.Yellow;
}
private void Rect1_MouseHover(object sender, EventArgs e)
{
(sender as RectangleControl).BackColor = Color.LightBlue;
}
You can use Panel control instead.
Just add 2 panel controls as you would like them to be arranged and add 2 event handlers:
private void panel1_MouseHover(object sender, EventArgs e)
{
panel1.BackColor = Color.Yellow;
}
private void panel1_MouseLeave(object sender, EventArgs e)
{
panel1.BackColor = Color.LightBlue;
}
You can monitor the MouseMove.
Here's code using MouseMove and some brush values used by the rectangles.
using System.Drawing;
using System.Windows.Forms;
namespace Question_Answer_WinForms_App
{
public partial class Form1 : Form
{
public Brush outerRectangleBrush = Brushes.DeepSkyBlue;
public Brush innerRectangleBrush = Brushes.LightBlue;
public Form1()
{
InitializeComponent();
Paint += Form1_Paint;
MouseMove += Form1_MouseMove;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
var isWithinOuterRectangle = e.Location.X >= 20
&& e.Location.X <= 250 + 20
&& e.Location.Y >= 20
&& e.Location.Y <= 250 + 20;
var isWithinInnerRectangle = e.Location.X >= 70
&& e.Location.X <= 150 + 70
&& e.Location.Y >= 70
&& e.Location.Y <= 150 + 70;
if (isWithinOuterRectangle)
{
if (isWithinInnerRectangle)
{
outerRectangleBrush = Brushes.DeepSkyBlue;
innerRectangleBrush = Brushes.Red;
Refresh();
}
else
{
outerRectangleBrush = Brushes.Red;
innerRectangleBrush = Brushes.LightBlue;
Refresh();
}
}
else
{
outerRectangleBrush = Brushes.DeepSkyBlue;
innerRectangleBrush = Brushes.LightBlue;
Refresh();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
// Create pen.
Pen blackPen = new Pen(Color.Black, 3);
// Create rectangle.
Rectangle rect1 = new Rectangle(20, 20, 250, 250);
Rectangle rect2 = new Rectangle(70, 70, 150, 150);
// Draw rectangle to screen.
e.Graphics.FillRectangle(outerRectangleBrush, rect1);
e.Graphics.FillRectangle(innerRectangleBrush, rect2);
}
}
}
#FJF, writing a ms-paint like application is not a complicated task. Windows Forms applications are using GDI+ to render graphics. so you can write a simple paint application using WindowsForms.
#nexolini uses a panel to do some draws. actually Ms-Paint does the same somehow. Ms-Paint is a Single-Layer Editor. So you can't resize all objects anytime you wanted (but as I said before you can assume that you have a panel for each newly drawn Shapes; Something like what Ms-Paint does).
So what is the problem?
Ms-Paint doesn't tracks your mouse movements and it doesn't needed (as it's a single layer). You can do all of it's tasks using these Answers.
e.g: for adding a Fill Color tool you can use a getpixel and putpixel to do a recursive algorithm on you image. and you are not needed to know which shape you are working on.
all the other tasks could be implemented easy.
for multi-layer Editors I will prefer to use a more powerful framework (but it's also could be implemented in Windows forms in a bad way), Like WPF. WPF uses DirectX to render your graphics and you are able to write an smooth Editor. WPF is designed to handle your graphical request so it does better on graphics.
#Reza_Aghaei 's comments are useful for Windows Forms.

Draw circle in new position without removing previous circle?

In fact , I want to draw circle in new position each time double-click and without remove before circle ,It should be noted that, I used PictureBox.
public Point postionCursor { get; set; }
List<Point> points = new List<Point>();
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
postionCursor = this.PointToClient(new Point(Cursor.Position.X - 25, Cursor.Position.Y - 25));
points.Add(postionCursor);
pictureBox1.Invalidate();
pictureBox1.Paint += new PaintEventHandler(pic_Paint);
}
private void pic_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
foreach (Point pt in points)
{
Pen p = new Pen(Color.Tomato, 2);
SolidBrush myb = new SolidBrush(Color.White);
g.DrawEllipse(p, postionCursor.X, postionCursor.Y, 20, 20);
g.FillEllipse(myb, postionCursor.X, postionCursor.Y, 20, 20);
p.Dispose();
}
}
You're not using the pt variable in the foreach loop.
foreach (Point pt in points)
{
using(Pen p = new Pen(Color.Tomato, 2))
using(SolidBrush myb = new SolidBrush(Color.White))
{
g.FillEllipse(myb, pt.X, pt.Y, 20, 20);
g.DrawEllipse(p, pt.X, pt.Y, 20, 20);
}
}
In your code, you were just overwriting the circle in the same location for every Point in the points list.
Also, as Reza mentioned in the comments, you don't need to attach the PaintEventHandler event hanlder every time the PictureBox is clicked, you just need to do it once.
So I got to thinking, and then Visual Studio-ing, that perhaps we don't even need the foreach loop. I still maintain a List so we know where the user has clicked, but there's no need to loop through it and redraw everything every time.
I realize this doesn't handle the case where the underlying list is modified, but nor does the original sample. Here's my entire Form1 class:
public partial class Form1 : Form
{
private const int CircleDiameter = 20;
private const int PenWidth = 2;
private readonly List<Point> _points = new List<Point>();
public Form1()
{
InitializeComponent();
pictureBox1.Paint += (sender, args) =>
{
_points.ForEach(p => DrawPoint(p, args.Graphics));
};
}
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
var cursorLocation = pictureBox1.PointToClient(Cursor.Position);
_points.Add(cursorLocation);
var circleArea = new Rectangle(
cursorLocation.X - CircleDiameter/2 - PenWidth,
cursorLocation.Y - CircleDiameter/2 - PenWidth,
CircleDiameter + PenWidth*2,
CircleDiameter + PenWidth*2);
pictureBox1.Invalidate(circleArea);
}
private static void DrawPoint(Point point, Graphics graphics)
{
point.X -= CircleDiameter / 2;
point.Y -= CircleDiameter / 2;
using (var pen = new Pen(Color.Tomato, PenWidth))
using (var brush = new SolidBrush(Color.White))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawEllipse(pen, point.X, point.Y, CircleDiameter, CircleDiameter);
graphics.FillEllipse(brush, point.X, point.Y, CircleDiameter, CircleDiameter);
}
}
}
Update 1:
So I updated the code to use the Paint event which has the foreach loop. However, I don't Invalidate (and Paint) every time a circle is added - there's no need for that. Just adding a circle by drawing means the control only invalidates and re-paints the region where the new circle was added.
Try setting a breakpoint on the DrawAllPoints method. You'll see it only happens during full invalidation operations such as minimizing and restoring.
Update 2:
After further chat, I agree the Invalidate method is superior. Code updated to use Invalidate with a rectangle to invalidate.
And now it's looking very much like the OP :)

Modify Fill Colour from Outside a Paint Event Handler in C# and Visual Studio 2012

What I Would Like to Do
Create and fill a rectangle, initially with a grey colour, then randomize that rectangle's fill colour with the press of a button.
What I Cannot Seem to Do
Modify properties of the aforementioned rectangle after it is initially drawn.
Random Colour Generator
public static readonly Random random = new Random();
public static readonly object syncLock = new object();
public static int RandomNumber( int min, int max )
{
lock(syncLock) {
return random.Next(min, max);
}
}
public Color randColourPicker() {
Color randColour = Color.FromArgb(RandomNumber(0, 255), RandomNumber(0, 255), RandomNumber(0, 255));
return randColour;
}
Some randColourPicker() Calls
public void randomizeColours() {
this.BackColor = randColourPicker();
clrRandLabel.ForeColor = randColourPicker();
randomizeButton.BackColor = randColourPicker();
randomizeButton.ForeColor = randColourPicker();
loopCheckbox.ForeColor = randColourPicker();
}
Rectangle Painting
private void clrRandForm_Paint( object sender, PaintEventArgs e ) {
int x = 266;
int y = 105;
int width = 274;
int height = 172;
Rectangle clrRect = new Rectangle(x, y, width, height);
SolidBrush greyBrush = new SolidBrush(Color.FromArgb(75, 75, 75));
SolidBrush randBrush = new SolidBrush(randColourPicker());
Graphics clrGraphic = mainBox.CreateGraphics();
clrGraphic.FillEllipse(greyBrush, clrRect);
greyBrush.Dispose();
randBrush.Dispose();
clrGraphic.Dispose();
}
More Info
I have a button, and it's click event is bound to execute the randomizeColours() call, which in turn, swaps around the colours of various elements of the UI. I also have a check box that, when checked, runs through randomizeColours() on loop with a timer. I would like to do something similar, but with the fill colour of clrRect, but I cannot seem to figure out how I would access clrGraphic.FillRectangle(greyBrush, clrRect) outside of the PaintEvent function, so I can modify it to clrGraphic.FillRectangle(randBrush, clrRect). randBrush is a brush I created with a random colour using randColourPicker().
Programs Used
Microsoft Visual Studio Express 2012
P.S.
Sorry if this question is a duplicate, I couldn't quite figure out what to search for... Also, sorry if my code is cringe-worthy to some, I'm not really that good with the OOP approach or C-styled languages. :)
To change the Rectangle color you have to draw it again. You can keep your paint event function but with some changes.
First, declare a global boolean First_time (initially set with true) that will check if you you want the greybrush or the Randbrush.
boolean First_Time;
Then in your Paint event :
private void clrRandForm_Paint( object sender, PaintEventArgs e ) {
int x = 266;
int y = 105;
int width = 274;
int height = 172;
Rectangle clrRect = new Rectangle(x, y, width, height);
SolidBrush greyBrush = new SolidBrush(Color.FromArgb(75, 75, 75));
SolidBrush randBrush = new SolidBrush(randColourPicker());
Graphics clrGraphic = mainBox.CreateGraphics();
if(First_time)
clrGraphic.FillEllipse(greyBrush, clrRect);
else
clrGraphic.FillEllipse(randBrush, clrRect);
First_Time = false;
greyBrush.Dispose();
randBrush.Dispose();
clrGraphic.Dispose();
}
And then in your button event, call:
this.Invalidate(); // or this.Refresh()
So that the Paint event is called again xD.
I hope my advise was usefull.

Turn a panel into a circle in C# Visual Studio 2010

I'm trying to make a basic soccer game in C#, and ive almost completed the field except for the various arcs and circles that are fairly important in the game, especially to set the bounds the computer's players cannot pass while their teammate/opponent is lining up for the kick.
So, all the methods I've tried havent worked because apparently I'm using fields like types, but I'm copying the code exactly. But I don't think its very important to show the buggy code, for a start I deleted it and I'd like the circles to be there permanently, not from when debugging starts.
So this is what I need: panels with round borders that stay round, and a way to put it in my code, which I'll post if necessary. Visual Studio C# Express 2010.
All help appreciated, thanks
One easy way to draw a circle on a panel is to inherit from Panel and override the OnPaint method. In this method you would call DrawEllipse on the Graphics object gotten from the event args. On point of interest is that the size is set to Width-1 and Height-1. This stops the right and bottom of the circle from dissapearing out of the Panel control.
One enhancement I have put in this code is to constrain the width & height in the OnResize method, this ensures your panel is always a circle, in oppose to an Ellipse (which can have different width and height). Simply drag this control onto a windows form and have a play in the designer.
public class CirclePanel : Panel
{
public CirclePanel()
{
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawEllipse(Pens.Black, 0,0,this.Width-1,this.Height-1);
}
protected override void OnResize(EventArgs e)
{
this.Width = this.Height;
base.OnResize(e);
}
}
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Pen mypen;
Color mycolor=Color.Red;
Graphics mygraph;
int xloc = 50, yloc = 50;
public Form1()
{
InitializeComponent();
mygraph = CreateGraphics();
}
private void Form1_Load(object sender, EventArgs e)
{
}
float x = 270, y = 0.5f;
int xmover = 100, ymover = 48;
private void timer1_Tick(object sender, EventArgs e)
{
mygraph.DrawEllipse(new Pen(Color.Red), xloc, yloc, 102, 102);
mygraph.FillEllipse(new SolidBrush(Color.Red), xmover++, ymover++, 4, 4);
mycolor = this.BackColor;
mygraph.DrawPie(new Pen(mycolor), xloc+1, yloc+1, 100, 100, x-1, y);
mygraph.DrawPie(new Pen (Color.Red), xloc + 1, yloc + 1, 100, 100, x++, y);
mygraph.FillEllipse(new SolidBrush(mycolor), xmover-1, ymover - 1, 4, 4);
mygraph.DrawEllipse(new Pen(Color.Red), 100, 50, 5, 5);
}
}
}

Categories

Resources