如何为用户定义的按钮创建具有已知且固定值的属性?

发布于 2024-12-07 12:17:10 字数 1134 浏览 0 评论 0原文

我有一个用户定义的按钮,它可能有一个 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 窗口该属性有一些默认值,例如 pauseplay选择它会导致更改按钮的样式(通过运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安穩 2024-12-14 12:17:10

我将创建一个枚举并将其公开为 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 appropriate ButtonStyle object to pass to your SetStyle method.

浅暮の光 2024-12-14 12:17:10

我认为您需要做的就是继承 Button 类并添加您的枚举:

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;
          }
      }
    }
  }
}

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;
          }
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文