Hi,
the below code drawing a custom Oval button, The problem is: I want after the click event to change its color many times but it stops at the red color and repaint again, so it always achieve just the first condition: -
using System;
-
using System.ComponentModel;
-
using System.Drawing;
-
using System.Windows.Forms;
-
using System.Drawing.Drawing2D;
-
-
namespace ShappedButton
-
{
-
public partial class ctrlOvalBtn : Button
-
{
-
public ctrlOvalBtn()
-
{
-
InitializeComponent();
-
}
-
private bool _clicked = false;
-
public bool Clicked
-
{
-
get { return _clicked; }
-
set
-
{
-
_clicked = value;
-
Invalidate();
-
}
-
}
-
-
protected override void OnPaint(PaintEventArgs pevent)
-
{
-
Graphics g = pevent.Graphics;
-
Rectangle innerRect = new Rectangle(new Point(0, 0), new Size(this.Width, this.Height));
-
GraphicsPath innerPath = new GraphicsPath();
-
SolidBrush brshFill = new SolidBrush(Color.WhiteSmoke);
-
SolidBrush brshLine = new SolidBrush(Color.Black);
-
-
innerPath.AddEllipse(innerRect);
-
-
Region innerRegion = new Region(innerPath);
-
-
this.Region = innerRegion;
-
-
pevent.Graphics.FillEllipse(brshLine, 0, 0, this.Width, this.Height);
-
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
-
-
if (_clicked == false)
-
{
-
pevent.Graphics.FillEllipse(brshLine, 0, 0, this.Width, this.Height);
-
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
-
}
-
else
-
{
-
if (brshFill.Color.Equals(Color.WhiteSmoke))
-
{
-
brshFill.Color = Color.Red;
-
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
-
}
-
else if (brshFill.Color.Equals(Color.Red))
-
{
-
brshFill.Color = Color.Blue;
-
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
-
}
-
else if (brshFill.Color.Equals(Color.Blue))
-
{
-
brshFill.Color = Color.Brown;
-
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
-
}
-
else
-
{
-
brshFill.Color = Color.WhiteSmoke;
-
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
-
}
-
}
-
-
// Dispose of painting objects
-
brshFill.Dispose();
-
brshLine.Dispose();
-
}
-
-
protected override void OnMouseEnter(EventArgs e)
-
{
-
this.Cursor = Cursors.Hand;
-
base.OnMouseEnter(e);
-
}
-
-
protected override void OnMouseLeave(EventArgs e)
-
{
-
this.Cursor = Cursors.Arrow;
-
base.OnMouseLeave(e);
-
}
-
protected override void OnMouseDown(MouseEventArgs mevent)
-
{
-
_clicked = true;
-
base.OnMouseDown(mevent);
-
}
-
-
protected override void OnMouseUp(MouseEventArgs mevent)
-
{
-
_clicked = true;
-
base.OnMouseUp(mevent);
-
}
-
-
}
-
}
-
-
what should i do !!
You could try ordering it to .Refresh() or .Invalidate() which would force the OnPaint method to execute
13 3734
Personal opinion: If this is to become a button, presumably to be re-used later, then you shouldn't have so much specific purpose code in the the Draw method. In order to make this a re-usable item you will want to give it a true Click event that you can react to and assign event handlers to.
If you give it a Click event, you could then change the color in the Click event handler just as you would with a standard button out of the toolbox.
As for why this is only working the first time... You are assigning the brush color of SmokeWhite in line 31 every time the Draw method is run. Therefore the if condition in line 50 is the only one that can ever be true.
yeah I know the "Why" but actually I don't know the solution, and I tried your way, to set the event in the presentation form, but i didn't work, I tried many thing to pass the color, all of them were useless. it always repaint each time I click on !!!
Right now you're only painting from a set colour, then doing an if statement to see if that colour is any of a set of colours, at which point you change the colour and paint. Let me try to simplify that...
You've got:
A = 0
if A == 0 then A = 0
else if A == 1 then A = 1
else if A == 2 then A = 2
...
Which doesn't really make sense. You're starting from scratch every time you go through. Try making a private member called something like _lastPaintColour and then running your code. When the click event fires, start it off at WhiteSmoke and let your code carry on.
Also, this is only going to happen every paint cycle, which isn't really a predictable pace. I'm not 100% sure, but I think windows only paints when it needs to, so like when something moves over a window.
I see you've got an Invalidate call there, but it's only on the click event. You might want to put it on the mouse down event to force a repaint constantly while the mouse is down. Of course, that'd be very, very slow.
A better solution might be to put a timer in there and have it go off at some kind of set interval to give you a good frame rate. When the timer expires, shuffle the colours and force a repaint.
Till Now i can't stop it from Repainting, Please give me a more clear solution. -
private bool _clicked = false;
-
private Color _brushColor;
-
public SolidBrush brshFill;
-
public SolidBrush brshLine;
-
-
public bool Clicked
-
{
-
get { return _clicked; }
-
set
-
{
-
_clicked = value;
-
Invalidate();
-
}
-
}
-
public Color brushColor
-
{
-
get { return _brushColor; }
-
set { _brushColor = value; }
-
-
}
-
-
public ctrlOvalBtn()
-
{
-
InitializeComponent();
-
}
-
public ctrlOvalBtn(Color _brushColor)
-
{
-
InitializeComponent();
-
brushColor = _brushColor;
-
}
-
-
protected override void OnPaint(PaintEventArgs pevent)
-
{
-
-
Graphics g = pevent.Graphics;
-
Rectangle innerRect = new Rectangle(new Point(0, 0), new Size(this.Width, this.Height));
-
GraphicsPath innerPath = new GraphicsPath();
-
brshFill = new SolidBrush(brushColor);
-
brshLine = new SolidBrush(Color.Black);
-
-
innerPath.AddEllipse(innerRect);
-
-
Region innerRegion = new Region(innerPath);
-
-
this.Region = innerRegion;
-
-
g.FillEllipse(brshLine, 0, 0, this.Width, this.Height);
-
-
//if (_clicked == false)
-
//{
-
// this.brushColor = Color.WhiteSmoke;
-
//g.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
-
//}
-
//else
-
//{
-
if (brushColor.Equals(Color.WhiteSmoke))
-
brushColor = Color.Red;
-
else if (brushColor.Equals(Color.Red))
-
brushColor = Color.Blue;
-
else if (brushColor.Equals(Color.Red))
-
brushColor = Color.Brown;
-
else
-
brushColor = Color.WhiteSmoke;
-
//}
-
-
g.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
-
-
brshFill.Dispose();
-
brshLine.Dispose();
-
}
-
.
what i want to do, is to draw a shape and at every MouseDown, give it a different color (Red, Blue, Brown, White ...)
Thats it.
Hope you help me.
At Laaaast, I got it, as i said All i need is how to stop it from repaint, and its so simple, Invalidate() method
Thanks for everyone :)
but one more question Gary, you said that with MouseDown, its very very slow, then offered a better solution which is the timer, would you please tell me what do u mean with enable a timer, regarding that I dont wanna let it rotate the color unless the Mouse is Down
Thanks
I really didn't know what you wanted. From the way your code was set up, it looked like you wanted the colours to cycle as a kind of animation when the mouse was down. It sounds like you were looking for something different. Either way, for your own knowledge (and fun), here's the code I made when buggering around with the code you posted. It has the timer stuff, so hopefully that explains it. - using System;
-
using System.ComponentModel;
-
using System.Drawing;
-
using System.Windows.Forms;
-
using System.Drawing.Drawing2D;
-
-
namespace ShapedButton
-
{
-
public class OvalButton : Button
-
{
-
private bool _clicked = false;
-
private Color _fillColor = Color.WhiteSmoke;
-
private Color _lastColor = Color.WhiteSmoke;
-
private Timer _timer = new Timer();
-
-
public bool Clicked
-
{
-
get { return _clicked; }
-
set
-
{
-
_fillColor = Color.WhiteSmoke;
-
_clicked = value;
-
Invalidate();
-
}
-
}
-
-
public OvalButton()
-
{
-
_timer.Interval = 100;
-
_timer.Tick += new EventHandler(_timer_Tick);
-
}
-
-
void _timer_Tick(object sender, EventArgs e)
-
{
-
if (_clicked == false)
-
{
-
_fillColor = Color.WhiteSmoke;
-
}
-
else
-
{
-
if (_fillColor.Equals(Color.WhiteSmoke))
-
{
-
_fillColor = Color.Red;
-
}
-
else if (_fillColor.Equals(Color.Red))
-
{
-
_fillColor = Color.Green;
-
}
-
else if (_fillColor.Equals(Color.Green))
-
{
-
_fillColor = Color.Blue;
-
}
-
else
-
{
-
_fillColor = Color.Red;
-
}
-
}
-
-
if (_lastColor != _fillColor)
-
{
-
this.Invalidate();
-
_lastColor = _fillColor;
-
}
-
}
-
-
protected override void OnPaint(PaintEventArgs pevent)
-
{
-
Graphics g = pevent.Graphics;
-
Rectangle innerRect = new Rectangle(new Point(0, 0), new Size(this.Width, this.Height));
-
GraphicsPath innerPath = new GraphicsPath();
-
SolidBrush brshFill = new SolidBrush(_fillColor);
-
SolidBrush brshLine = new SolidBrush(Color.Black);
-
-
innerPath.AddEllipse(innerRect);
-
-
Region innerRegion = new Region(innerPath);
-
-
this.Region = innerRegion;
-
-
pevent.Graphics.FillEllipse(brshLine, 0, 0, this.Width, this.Height);
-
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
-
-
SizeF strSize = pevent.Graphics.MeasureString(this.Text, this.Font);
-
float posX = (float)(this.Width / 2.0 - strSize.Width / 2.0);
-
float posY = (float)(this.Height / 2.0 - strSize.Height / 2.0);
-
pevent.Graphics.DrawString(this.Text, this.Font, brshLine, new PointF(posX, posY));
-
-
// Dispose of painting objects
-
brshFill.Dispose();
-
brshLine.Dispose();
-
}
-
-
protected override void OnMouseEnter(EventArgs e)
-
{
-
this.Cursor = Cursors.Hand;
-
base.OnMouseEnter(e);
-
}
-
-
protected override void OnMouseLeave(EventArgs e)
-
{
-
this.Cursor = Cursors.Arrow;
-
base.OnMouseLeave(e);
-
}
-
protected override void OnMouseDown(MouseEventArgs mevent)
-
{
-
if (_clicked == false)
-
{
-
_clicked = true;
-
//_timer.Stop();
-
_timer_Tick(null, null);
-
_timer.Start();
-
base.OnMouseDown(mevent);
-
}
-
}
-
-
protected override void OnMouseUp(MouseEventArgs mevent)
-
{
-
if (_clicked == true)
-
{
-
_clicked = false;
-
_timer.Stop();
-
_timer_Tick(null, null);
-
//_timer.Start();
-
base.OnMouseUp(mevent);
-
}
-
}
-
}
-
}
Well, Actually, I finished the project in exactly like I want, but with a bug:
the Scenario is: I have a shape was drawn by (override void OnPaint) and this control includes the events: (override void OnMouseEnter, override void OnMouseLeave, override void OnMouseDown and override void OnMouseUp).
The user will click on the button and the color will change, then by choosing "Save" the application save the button's color in the database.
The problem is happening after I execute a select statement that returns the button's colour to reload the correct colour again, it works fine, but the colour doesn't appear in the form unless i hover on the shape !!!!
Isn't there anything can be helpful in this issue.
Thanks
You could try ordering it to .Refresh() or .Invalidate() which would force the OnPaint method to execute
Yeah, I did the Refresh() after a lot of attempts.
Thanks all
Sign in to post your reply or Sign up for a free account.
Similar topics
by: bershama |
last post by:
i want to make a shaped bnt like star or circle something
like that .(with the full code)
|
by: JMCN |
last post by:
hello
this is absolutely ridiculous :) i have a simple form where if you
click the "main menu" command button it should ideally go to the main
menu . however, each time i click the menu command...
|
by: Flix |
last post by:
I need some help to handle the Region property of my Form in the correct
way.
Usually when I create shaped forms in my programs, I modify the Form.Region
property, making it include only a part of...
|
by: Wayne MJ |
last post by:
I have a web control embedded in an aspx page.
On the web control, I have a custom_validator that checks the status
of a dropdown list and compares that to ensure the appropriate text
fields are...
|
by: Max |
last post by:
For some reason my button control just stopped working. In debug mode I
found the _Click event is just not firing. When I click submit, the page
just refreshes. Any idea what's going on?
-Max
|
by: Arturo Toledo |
last post by:
Ok.. I need to create a button. Not a normal one. I need one that has a
STAR shape. I need it to be yellow and with a face. When I rollover this
button I need the face to smile and to have...
|
by: DzemoT. |
last post by:
how to make round button in vb.net?
|
by: dr |
last post by:
C# Irregular shaped user controls.
How to make usercontrol have transparency key for its background image? I
want to have irregular shaped user controls on my form. like a funny shaped
button....
|
by: vabsjava |
last post by:
Hello Everybody.....
I want to use command button which is oval in shape.....
how to convert the button shaped in rectangle to oval....
please me in this regard...
Thank You
|
by: Dean Slindee |
last post by:
When you drop a button on a form, a Click event is created by the designer.
Within the Click event certain event parameters are defined:
Private Sub btnInsert_ButtonPressed(ByVal sender As...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |