473,765 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

shaped button with _click event

JustRun
127 New Member
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
13 3753
tlhintoq
3,525 Recognized Expert Specialist
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 New Member
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 Recognized Expert Top Contributor
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 _lastPaintColou r 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 New Member
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 New Member
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 New Member
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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

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

Similar topics

2
1575
by: bershama | last post by:
i want to make a shaped bnt like star or circle something like that .(with the full code)
3
3716
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 button i receive an error message "the expression On Click you entered as an event property setting produced the following error: user-defined type not defined." i just don't understand when i actually used the wizard or have the simple code of...
2
1905
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 the form client rectangle (I use a custom button to close my form). The problem is that when I want to go back to a normal shaped form at runtime, I use the following line of code: this.Region=new System.Drawing.Region(new...
0
900
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 filled. When I step through the code, I see that it is infact being run, and returning: args.IsInvalid = false;
3
2257
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
2787
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 sparkles comming out of the button. When I click this button I need it to shake and have the face scream (no sound required)... This button will be located over a composite background so I would like to have transparent areas where my button is not...
4
3351
by: DzemoT. | last post by:
how to make round button in vb.net?
0
1682
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. This works fine for irregular shaped forms becuase they have a transparency key field, but user controls dont have this field. note i do not want to see the desktop. I want to see any other controls or form behind the irregularly shaped user...
0
1546
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
1229
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 System.Object, ByVal e As System.EventArgs) What harm or limitations would ensue if (ByVal sender As System.Object, ByVal e As System.EventArgs) were changed to (). Thanks, Dean S
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.