Click here to Skip to main content
15,893,668 members
Home / Discussions / C#
   

C#

 
AnswerRe: Socket Sample Pin
led mike4-May-06 11:27
led mike4-May-06 11:27 
AnswerRe: Socket Sample Pin
Ravi Bhavnani4-May-06 11:46
professionalRavi Bhavnani4-May-06 11:46 
QuestionHow to send email using C#.NET v1.0 Pin
SumitBiswasBabai4-May-06 11:01
SumitBiswasBabai4-May-06 11:01 
AnswerRe: How to send email using C#.NET v1.0 Pin
led mike4-May-06 12:14
led mike4-May-06 12:14 
AnswerRe: How to send email using C#.NET v1.0 Pin
BillWoodruff5-May-06 0:19
professionalBillWoodruff5-May-06 0:19 
QuestionIStillImage app freeze ? Pin
Christian Graus4-May-06 10:30
protectorChristian Graus4-May-06 10:30 
AnswerRe: IStillImage app freeze ? Pin
leppie5-May-06 0:44
leppie5-May-06 0:44 
QuestionSql-question Pin
JelleM4-May-06 10:21
JelleM4-May-06 10:21 
AnswerRe: Sql-guestion Pin
Christian Graus4-May-06 10:32
protectorChristian Graus4-May-06 10:32 
GeneralRe: Sql-guestion Pin
JelleM4-May-06 10:59
JelleM4-May-06 10:59 
GeneralRe: Sql-guestion Pin
Christian Graus4-May-06 11:01
protectorChristian Graus4-May-06 11:01 
GeneralRe: Sql-guestion Pin
JelleM4-May-06 11:37
JelleM4-May-06 11:37 
GeneralRe: Sql-guestion Pin
Christian Graus4-May-06 11:43
protectorChristian Graus4-May-06 11:43 
GeneralRe: Sql-guestion Pin
JelleM4-May-06 11:52
JelleM4-May-06 11:52 
GeneralRe: Sql-guestion Pin
Christian Graus4-May-06 12:04
protectorChristian Graus4-May-06 12:04 
GeneralRe: Sql-guestion Pin
JelleM5-May-06 0:24
JelleM5-May-06 0:24 
GeneralRe: Sql-guestion Pin
Drew McGhie5-May-06 3:42
Drew McGhie5-May-06 3:42 
GeneralRe: Sql-guestion Pin
JelleM8-May-06 22:52
JelleM8-May-06 22:52 
GeneralRe: Sql-guestion Pin
led mike4-May-06 11:53
led mike4-May-06 11:53 
QuestionHiding Properties at Design Time Pin
SnoopyJ4-May-06 10:16
SnoopyJ4-May-06 10:16 
I'm writing a custom control (Visual Studio 2003, C-Sharp). This is a very simple control that draws a circle or a polygon. The control exposes two properties: ShapeStyle and ShapeSides. ShapeStyle is an enumerated property that can be either "Circle" or "Polygon". If the user picks "Circle" from the IDE Property Grid, the control draws a circle. If the user picks "Polygon", then the control will use the value in ShapeSides (an integer) to sketch a triangle (3 sides), square (4 sides), pentagon (5 sides), etc.

I don't want the "ShapeSides" property displayed on the PropertyGrid, if "ShapeStyle" is set to "Circle". I don't think I can change a Property Attribute programatically.

Below is the code for the control. Any help will be greatly appreciated.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace TestApp
{
	/// <summary>
	/// Summary description for ShapeControl.
	/// </summary>
	public class ShapeControl : Control
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private Container components = null;

		private Color shapeColor;
		private ShapeStyle shapeStyle;
		private int shapeSides;

		public ShapeControl()
		{
			// Enable Double-Buffering and other painting parameters
			base.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
				ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw |
				ControlStyles.SupportsTransparentBackColor, true);
			this.shapeColor = Color.Red;
			this.shapeStyle = ShapeStyle.Circle;
			this.shapeSides = 4;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
					components.Dispose();
			}
			base.Dispose(disposing);
		}

		protected override void OnPaint(PaintEventArgs pe)
		{
			base.OnPaint(pe);
			pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

			// Find minimum side
			int minSide = Math.Min(pe.ClipRectangle.Width, pe.ClipRectangle.Height);
			// Create Rectangle on center of control
			PointF center = new PointF((pe.ClipRectangle.X + pe.ClipRectangle.Right - minSide)/2f, (pe.ClipRectangle.Y + pe.ClipRectangle.Bottom - minSide)/2f);
			RectangleF rect = new RectangleF(center, new Size(minSide, minSide));
			if (this.shapeStyle == ShapeStyle.Circle)
			{
				pe.Graphics.FillEllipse(new SolidBrush(this.shapeColor), rect);
			}
			else
			{
				PointF[] points = new PointF[this.shapeSides];
				for (int i = 0; i < this.shapeSides; i++)
				{
					points[i].X = (float) (minSide/2f*Math.Cos(i/((float) this.shapeSides)*Math.PI*2)) + center.X+minSide/2f;
					points[i].Y = (float) (minSide/2f*Math.Sin(i/((float) this.shapeSides)*Math.PI*2)) + center.Y+minSide/2f;
				}
				pe.Graphics.FillPolygon(new SolidBrush(this.shapeColor), points);
			}
		}

		#region Component Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}

		#endregion

		protected override Size DefaultSize
		{
			get { return new Size(200, 200); }
		}

		[Category("Appearance")]
		[DefaultValue(typeof (Color), "Red")]
		[Description("The shape color.")]
		public Color ShapeColor
		{
			get { return this.shapeColor; }
			set
			{
				this.shapeColor = value;
				this.Invalidate();
			}
		}

		[Category("Appearance")]
		[DefaultValue(typeof (ShapeStyle), "Circle")]
		[Description("The shape style.")]
		[RefreshProperties(RefreshProperties.All)]
		public ShapeStyle ShapeStyle
		{
			get { return this.shapeStyle; }
			set
			{
				this.shapeStyle = value;
				if (this.shapeStyle == ShapeStyle.Circle)
				{
					// I want to set ShapeSides to be a read only, or non-browsable property
					// at design time
				}
				this.Invalidate();
			}
		}

		[Category("Appearance")]
		[DefaultValue(4)]
		[Description("The shape number of sides.")]
		[Browsable(true)]  // How can I change this attribute depending on the condition above?
		public int ShapeSides
		{
			get { return this.shapeSides; }
			set
			{
				if (value > 2)
				{
					this.shapeSides = value;
					this.Invalidate();
				}
			}
		}
	}

	/// <summary>Style of Shape to display.</summary>
	public enum ShapeStyle
	{
		// Fields
		Circle,
		Polygon
	}
}

AnswerRe: Hiding Properties at Design Time Pin
Josh Smith4-May-06 10:52
Josh Smith4-May-06 10:52 
Questionpassing Form as refernce to called from dynamically Pin
abhinish4-May-06 9:39
abhinish4-May-06 9:39 
AnswerRe: passing Form as refernce to called from dynamically Pin
led mike4-May-06 9:51
led mike4-May-06 9:51 
AnswerRe: passing Form as refernce to called from dynamically Pin
Josh Smith4-May-06 9:56
Josh Smith4-May-06 9:56 
QuestionPrint Visio Document with C#.NET ????? Pin
nicolas santana4-May-06 9:17
nicolas santana4-May-06 9:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.