I have a user-defined button, and it may have a Style
property which I choose in properties
window a value for it and depending on that value its Image and style will change.
How can I make this property to have some predefined and fixed values in a drop down list in properties
window? and selecting a value causes running a method
-details:
this button may ge开发者_JAVA百科ts specified appearance such pause and play styles. so I made a class for styles:
// style of the button; pause, play, reset, etc
public abstract class ButtonStyle
{
public abstract Image GetImage();
}
// inherited classes of class ButtonStyle
public class PauseButtonStyle : ButtonStyle
{
public override Image GetImage()
{
return CustomButtonLibrary.Properties.Resources.PauseButton;
}
}
public class PlayButtonStyle : ButtonStyle
{
public override Image GetImage()
{
return CustomButtonLibrary.Properties.Resources.PlayButton;
}
}
And there is a method in the button for setting the specified style (pause,play,...):
public void SetStyle(ButtonStyle style)
{
button1.Image = style.GetImage();
}
Now how can I have a property for this custom button in properties
window that this property has some default values like pause
, play
,etc and selecting it causes changing the button's style (with running SetStyle method)
I would make an enum and expose that as the Style
property. Then, have a internal dictionary that keys off the enum value to choose the appropriate ButtonStyle
object to pass to your SetStyle
method.
I think all you need to do is inherit the Button
class and add your Enum:
public class ButtonEx : Button
{
public enum ButtonStateStyles
{
None,
Pause,
Play,
}
private ButtonStateStyles _ButtonStateStyle = ButtonStateStyles.None;
public ButtonStateStyles ButtonStateStyle
{
get { return _ButtonStateStyle; }
set
{
_ButtonStateStyle = value;
switch (_ButtonStateStyle)
{
case ButtonStateStyles.Pause:
{
base.Image = new PauseButtonStyle().GetImage();
break;
}
case ButtonStateStyles.Play:
{
base.Image = new PlayButtonStyle().GetImage();
break;
}
default:
{
base.Image = null;
break;
}
}
}
}
}
精彩评论