How to create a custom property in User Control of enum type in C#
Hi,
I am very much new to .net Compact Framework.
I am creating a user control.I have to expose a custom property to the user so that he can select the value of the property among a predefined values.I found that we have to use enum.
That is I want to make the property in the designer be a dropdown list.
I have tried with the following code..
public enum ArrowColor { Red, Blue, Green, Magenta, Pink, Orange, Black, Yellow };
private ArrowColor m_arrowForeColor = ArrowColor.Blue;
public ArrowColor ArrowForeColor
{
get
{
return m_arrowForeColor;
}
set
{
this.m_arrowForeColor = (ArrowColor)value;
}
}
But i cannot see the peroperty on the properties window of the user control,when used the control in another application.
I tried by adding another test proeprty of type bool and it did work.i can see true and false values in the drop down.
Am i making any mistake?Do i need to do anything to make it work?
Appreciate the help in advance..
Re: How to create a custom property in User Control of enum type in C#
you have to fill the various enum values inside the get function before return i suppose :
public ArrowColor ArrowForeColor
{
get
{
m_arrowForeColor = ArrowColor.Blue;
m_arrowForeColor = ArrowColor.Red;
m_arrowForeColor = ArrowColor.Green;
m_arrowForeColor = ArrowColor.Magenta;
m_arrowForeColor = = ArrowColor.Pink;
m_arrowForeColor = ArrowColor.Orange;
m_arrowForeColor = ArrowColor.Black;
m_arrowForeColor = ArrowColor.Yellow;
return m_arrowForeColor;
}
}
Re: How to create a custom property in User Control of enum type in C#
Oh NO !! Not this way
Code:
public ArrowColor ArrowForeColor
{
get
{
m_arrowForeColor = ArrowColor.Blue;
m_arrowForeColor = ArrowColor.Red;
m_arrowForeColor = ArrowColor.Green;
m_arrowForeColor = ArrowColor.Magenta;
m_arrowForeColor = = ArrowColor.Pink;
m_arrowForeColor = ArrowColor.Orange;
m_arrowForeColor = ArrowColor.Black;
m_arrowForeColor = ArrowColor.Yellow;
return m_arrowForeColor;
}
}
Heee ??? Never seen what this should handle. You are overwriting your internal value one after the other so at least you only and everytime will get yellow. You don't need to do enums if you dont want you can use your coloor class itself, because as it seems to me you have your own self declared colors. Look how this works The theme is called TypeConverter and I have written about that in my article series Part1 about how to create a dockable panel
The link you can find in the bottom of this page. This series explains the basics of how to create a userdefined control and by that also explains how to have your own classes used as types in the property Grid :D Easy to read because done for C# beginners. Lots of pictures
http://www.codeguru.com/csharp/cshar...le.php/c14179/
1 Attachment(s)
Re: How to create a custom property in User Control of enum type in C#
If you simple want to use enum your code should look like
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace CGColorControlTest {
// defined outside the class !
public enum ArrowColor {Red, Green,Magenta, Pink,Orange,Black,Yellow};
public partial class MyControl : UserControl {
private ArrowColor _foreColor;
public MyControl() {
InitializeComponent();
}
[Browsable(true)]
public ArrowColor ForeColor {
get { return _foreColor; }
set { _foreColor = value; }
}
}
}
The result looks this way. And BTW Please use codetags as its in our forum rules. Any formatting goes lost when not using them and nobody wants to read codesausage
Re: How to create a custom property in User Control of enum type in C#
thanks for the correction Johnny..
opps!! some how i had messed up while replying to that post..yes.. all the values were going to be overwritten and only the last value would had been returned...