如何为用户定义的按钮创建具有已知且固定值的属性?
我有一个用户定义的按钮,它可能有一个 Style
属性,我在 properties
窗口中选择它的值,并且根据该值,它的图像和样式将发生变化。
如何使该属性在 properties
窗口的下拉列表中具有一些预定义的固定值?选择一个值会导致运行一个方法
-详细信息:
此按钮可能会获得指定的外观,例如暂停和播放样式。所以我为样式创建了一个类:
// 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;
}
}
并且按钮中有一个用于设置指定样式的方法(暂停,播放,...):
public void SetStyle(ButtonStyle style)
{
button1.Image = style.GetImage();
}
现在我如何在 properties
窗口该属性有一些默认值,例如 pause
、play
等选择它会导致更改按钮的样式(通过运行 SetStyle 方法)
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 gets 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将创建一个枚举并将其公开为
Style
属性。然后,使用一个内部字典来关闭枚举值,以选择适当的ButtonStyle
对象传递给您的SetStyle
方法。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 appropriateButtonStyle
object to pass to yourSetStyle
method.我认为您需要做的就是继承
Button
类并添加您的枚举:I think all you need to do is inherit the
Button
class and add your Enum: