473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

shaped button with _click event

JustRun
127 100+
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:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Drawing.Drawing2D;
  6.  
  7. namespace ShappedButton
  8. {
  9.     public partial class ctrlOvalBtn : Button
  10.     {
  11.         public ctrlOvalBtn()
  12.         {
  13.             InitializeComponent();
  14.         }
  15.         private bool _clicked = false;
  16.         public bool Clicked
  17.         {
  18.             get { return _clicked; }
  19.             set
  20.             {
  21.                 _clicked = value;
  22.                 Invalidate();
  23.             }
  24.         }
  25.  
  26.         protected override void OnPaint(PaintEventArgs pevent)
  27.         {
  28.             Graphics g = pevent.Graphics;
  29.             Rectangle innerRect = new Rectangle(new Point(0, 0), new Size(this.Width, this.Height));
  30.             GraphicsPath innerPath = new GraphicsPath();
  31.             SolidBrush brshFill = new SolidBrush(Color.WhiteSmoke);
  32.             SolidBrush brshLine = new SolidBrush(Color.Black);
  33.  
  34.             innerPath.AddEllipse(innerRect);
  35.  
  36.             Region innerRegion = new Region(innerPath);
  37.  
  38.             this.Region = innerRegion;
  39.  
  40.             pevent.Graphics.FillEllipse(brshLine, 0, 0, this.Width, this.Height);
  41.             pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
  42.  
  43.             if (_clicked == false)
  44.             {
  45.                 pevent.Graphics.FillEllipse(brshLine, 0, 0, this.Width, this.Height);
  46.                 pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
  47.             }
  48.             else
  49.             {
  50.                 if (brshFill.Color.Equals(Color.WhiteSmoke))
  51.                 {
  52.                     brshFill.Color = Color.Red;
  53.                     pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
  54.                 }
  55.                 else if (brshFill.Color.Equals(Color.Red))
  56.                 {
  57.                     brshFill.Color = Color.Blue;
  58.                     pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
  59.                 }
  60.                 else if (brshFill.Color.Equals(Color.Blue))
  61.                 {
  62.                     brshFill.Color = Color.Brown;
  63.                     pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
  64.                 }
  65.                 else
  66.                 {
  67.                     brshFill.Color = Color.WhiteSmoke;
  68.                     pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
  69.                 }
  70.             }
  71.  
  72.             // Dispose of painting objects
  73.             brshFill.Dispose();
  74.             brshLine.Dispose();
  75.         }
  76.  
  77.         protected override void OnMouseEnter(EventArgs e)
  78.         {
  79.             this.Cursor = Cursors.Hand;
  80.             base.OnMouseEnter(e);
  81.         }
  82.  
  83.         protected override void OnMouseLeave(EventArgs e)
  84.         {
  85.             this.Cursor = Cursors.Arrow;
  86.             base.OnMouseLeave(e);
  87.         }
  88.         protected override void OnMouseDown(MouseEventArgs mevent)
  89.         {
  90.             _clicked = true;
  91.             base.OnMouseDown(mevent);
  92.         }
  93.  
  94.         protected override void OnMouseUp(MouseEventArgs mevent)
  95.         {
  96.             _clicked = true;
  97.             base.OnMouseUp(mevent);
  98.         }
  99.  
  100.     }
  101. }
  102.  
  103.  
what should i do !!
Jan 26 '10 #1

✓ answered by tlhintoq

You could try ordering it to .Refresh() or .Invalidate() which would force the OnPaint method to execute

13 3734
tlhintoq
3,525 Expert 2GB
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.
Jan 26 '10 #2
JustRun
127 100+
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 !!!
Jan 27 '10 #3
GaryTexmo
1,501 Expert 1GB
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.
Jan 27 '10 #4
JustRun
127 100+
Till Now i can't stop it from Repainting, Please give me a more clear solution.

Expand|Select|Wrap|Line Numbers
  1. private bool _clicked = false;
  2.         private Color _brushColor;
  3.         public SolidBrush brshFill;
  4.         public SolidBrush brshLine;
  5.  
  6.         public bool Clicked
  7.         {
  8.             get { return _clicked; }
  9.             set
  10.             {
  11.                 _clicked = value;
  12.                 Invalidate();
  13.             }
  14.         }
  15.         public Color brushColor
  16.         {
  17.             get { return _brushColor; }
  18.             set { _brushColor = value; }
  19.  
  20.         }
  21.  
  22.         public ctrlOvalBtn()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.         public ctrlOvalBtn(Color _brushColor)
  27.         {
  28.             InitializeComponent();
  29.             brushColor = _brushColor;
  30.         }
  31.  
  32.         protected override void OnPaint(PaintEventArgs pevent)
  33.         {
  34.  
  35.             Graphics g = pevent.Graphics;
  36.             Rectangle innerRect = new Rectangle(new Point(0, 0), new Size(this.Width, this.Height));
  37.             GraphicsPath innerPath = new GraphicsPath();
  38.             brshFill = new SolidBrush(brushColor);
  39.             brshLine = new SolidBrush(Color.Black);
  40.  
  41.             innerPath.AddEllipse(innerRect);
  42.  
  43.             Region innerRegion = new Region(innerPath);
  44.  
  45.             this.Region = innerRegion;
  46.  
  47.             g.FillEllipse(brshLine, 0, 0, this.Width, this.Height);
  48.  
  49.             //if (_clicked == false)
  50.             //{
  51.             //    this.brushColor = Color.WhiteSmoke;
  52.                 //g.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
  53.             //}
  54.             //else
  55.             //{
  56.                 if (brushColor.Equals(Color.WhiteSmoke))
  57.                     brushColor = Color.Red;
  58.                 else if (brushColor.Equals(Color.Red))
  59.                     brushColor = Color.Blue;
  60.                 else if (brushColor.Equals(Color.Red))
  61.                     brushColor = Color.Brown;
  62.                 else
  63.                     brushColor = Color.WhiteSmoke;
  64.             //}
  65.  
  66.             g.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
  67.  
  68.             brshFill.Dispose();
  69.             brshLine.Dispose();
  70.         }
  71.  
.
Jan 31 '10 #5
JustRun
127 100+
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.
Jan 31 '10 #6
JustRun
127 100+
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 :)
Jan 31 '10 #7
JustRun
127 100+
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
Jan 31 '10 #8
GaryTexmo
1,501 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Drawing.Drawing2D;
  6.  
  7. namespace ShapedButton
  8. {
  9.     public class OvalButton : Button
  10.     {
  11.         private bool _clicked = false;
  12.         private Color _fillColor = Color.WhiteSmoke;
  13.         private Color _lastColor = Color.WhiteSmoke;
  14.         private Timer _timer = new Timer();
  15.  
  16.         public bool Clicked
  17.         {
  18.             get { return _clicked; }
  19.             set
  20.             {
  21.                 _fillColor = Color.WhiteSmoke;
  22.                 _clicked = value;
  23.                 Invalidate();
  24.             }
  25.         }
  26.  
  27.         public OvalButton()
  28.         {
  29.             _timer.Interval = 100;
  30.             _timer.Tick += new EventHandler(_timer_Tick);
  31.         }
  32.  
  33.         void _timer_Tick(object sender, EventArgs e)
  34.         {
  35.             if (_clicked == false)
  36.             {
  37.                 _fillColor = Color.WhiteSmoke;
  38.             }
  39.             else
  40.             {
  41.                 if (_fillColor.Equals(Color.WhiteSmoke))
  42.                 {
  43.                     _fillColor = Color.Red;
  44.                 }
  45.                 else if (_fillColor.Equals(Color.Red))
  46.                 {
  47.                     _fillColor = Color.Green;
  48.                 }
  49.                 else if (_fillColor.Equals(Color.Green))
  50.                 {
  51.                     _fillColor = Color.Blue;
  52.                 }
  53.                 else
  54.                 {
  55.                     _fillColor = Color.Red;
  56.                 }
  57.             }
  58.  
  59.             if (_lastColor != _fillColor)
  60.             {
  61.                 this.Invalidate();
  62.                 _lastColor = _fillColor;
  63.             }
  64.         }
  65.  
  66.         protected override void OnPaint(PaintEventArgs pevent)
  67.         {
  68.             Graphics g = pevent.Graphics;
  69.             Rectangle innerRect = new Rectangle(new Point(0, 0), new Size(this.Width, this.Height));
  70.             GraphicsPath innerPath = new GraphicsPath();
  71.             SolidBrush brshFill = new SolidBrush(_fillColor);
  72.             SolidBrush brshLine = new SolidBrush(Color.Black);
  73.  
  74.             innerPath.AddEllipse(innerRect);
  75.  
  76.             Region innerRegion = new Region(innerPath);
  77.  
  78.             this.Region = innerRegion;
  79.  
  80.             pevent.Graphics.FillEllipse(brshLine, 0, 0, this.Width, this.Height);
  81.             pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
  82.  
  83.             SizeF strSize = pevent.Graphics.MeasureString(this.Text, this.Font);
  84.             float posX = (float)(this.Width / 2.0 - strSize.Width / 2.0);
  85.             float posY = (float)(this.Height / 2.0 - strSize.Height / 2.0);
  86.             pevent.Graphics.DrawString(this.Text, this.Font, brshLine, new PointF(posX, posY));
  87.  
  88.             // Dispose of painting objects
  89.             brshFill.Dispose();
  90.             brshLine.Dispose();
  91.         }
  92.  
  93.         protected override void OnMouseEnter(EventArgs e)
  94.         {
  95.             this.Cursor = Cursors.Hand;
  96.             base.OnMouseEnter(e);
  97.         }
  98.  
  99.         protected override void OnMouseLeave(EventArgs e)
  100.         {
  101.             this.Cursor = Cursors.Arrow;
  102.             base.OnMouseLeave(e);
  103.         }
  104.         protected override void OnMouseDown(MouseEventArgs mevent)
  105.         {
  106.             if (_clicked == false)
  107.             {
  108.                 _clicked = true;
  109.                 //_timer.Stop();
  110.                 _timer_Tick(null, null);
  111.                 _timer.Start();
  112.                 base.OnMouseDown(mevent);
  113.             }
  114.         }
  115.  
  116.         protected override void OnMouseUp(MouseEventArgs mevent)
  117.         {
  118.             if (_clicked == true)
  119.             {
  120.                 _clicked = false;
  121.                 _timer.Stop();
  122.                 _timer_Tick(null, null);
  123.                 //_timer.Start();
  124.                 base.OnMouseUp(mevent);
  125.             }
  126.         }
  127.     }
  128. }
Feb 1 '10 #9
JustRun
127 100+
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 !!!!
Feb 5 '10 #10
JustRun
127 100+
Isn't there anything can be helpful in this issue.

Thanks
Feb 7 '10 #11
tlhintoq
3,525 Expert 2GB
You could try ordering it to .Refresh() or .Invalidate() which would force the OnPaint method to execute
Feb 7 '10 #12
JustRun
127 100+
Yeah, I did the Refresh() after a lot of attempts.

Thanks all
Feb 8 '10 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: bershama | last post by:
i want to make a shaped bnt like star or circle something like that .(with the full code)
3
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...
2
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...
0
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...
3
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
3
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...
4
by: DzemoT. | last post by:
how to make round button in vb.net?
0
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....
0
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
6
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
1
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)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.