如何向我的控件添加新的现有属性?

发布于 2024-12-27 20:52:31 字数 271 浏览 0 评论 0原文

我有自己的控件:

public class newControl : Control
{
}

有一个 Text 属性,但没有 TextAlign 属性。例如,我需要这个类似于 ButtonTextAlign 属性的属性,但我不想从按钮类继承它。

那么我可以只继承 TextAlign 属性吗?如果是,怎么办?

I have my own control:

public class newControl : Control
{
}

There is a Text property, but there is not a TextAlign property. For example, I need this property similar to the TextAlign property of a Button, but I don't want inherit it from a button class.

So can I inherit only TextAlign property? If yes, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

世界和平 2025-01-03 20:52:31

是的,您只需添加即可。内置枚举称为 ContentAlignment

using System.ComponentModel;
using System.Windows.Forms;

public class newControl : Control {

  private ContentAlignment _TextAlign = ContentAlignment.MiddleCenter;

  [Description("The alignment of the text that will be displayed on the control.")]
  [DefaultValue(typeof(ContentAlignment), "MiddleCenter")]
  public ContentAlignment TextAlign {
    get { return _TextAlign; }
    set { _TextAlign = value; }
  }
}

您现在可以决定如何使用此属性。

请注意,我添加了一些有关如何在 PropertyGrid 中使用控件的属性。 DefaultValue 属性不会设置属性的值,它只是确定属性是否以粗体显示。

要使用 TextAlign 属性显示文本,您必须重写 OnPaint 方法并绘制它:

protected override void OnPaint(PaintEventArgs e) {
  switch (_TextAlign) {
    case ContentAlignment.MiddleCenter: {
        TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, Color.Empty, 
                              TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
        break;
      }
    case ContentAlignment.MiddleLeft: {
        TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, Color.Empty,
                              TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
        break;
      }
    // more case statements here for all alignments, etc.

  }
  base.OnPaint(e);
}

Yes, you can just add it. The built in enumeration is called ContentAlignment:

using System.ComponentModel;
using System.Windows.Forms;

public class newControl : Control {

  private ContentAlignment _TextAlign = ContentAlignment.MiddleCenter;

  [Description("The alignment of the text that will be displayed on the control.")]
  [DefaultValue(typeof(ContentAlignment), "MiddleCenter")]
  public ContentAlignment TextAlign {
    get { return _TextAlign; }
    set { _TextAlign = value; }
  }
}

What you do with this property is up to you now.

Note that I added some attributes for how the control is used in the PropertyGrid. The DefaultValue attribute does not set the value of the property, it just determines whether or not the property is displayed in bold or not.

To display the text using your TextAlign property, you would have to override the OnPaint method and draw it:

protected override void OnPaint(PaintEventArgs e) {
  switch (_TextAlign) {
    case ContentAlignment.MiddleCenter: {
        TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, Color.Empty, 
                              TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
        break;
      }
    case ContentAlignment.MiddleLeft: {
        TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, Color.Empty,
                              TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
        break;
      }
    // more case statements here for all alignments, etc.

  }
  base.OnPaint(e);
}
苯莒 2025-01-03 20:52:31

首先,考虑继承 System.Web.UI。 WebControls.WebControl,它有更多与样式对应的属性,例如CssClassAttributes

我建议不要使用 TextAlign 属性,而是简单地将 CSS 类添加到您的页面,并使用 set CssClass 属性,该属性位于基类 WebControl< /代码>。

或者,您可以通过执行以下操作来设置文本对齐方式(但 CSS 类会更清晰):

this.Attributes["style"] = "text-align: center";

当然,您也可以随时添加自己的属性,将正确的 CSS 写入 Attributes 集合。

First, consider inheriting from System.Web.UI.WebControls.WebControl, which has more properties that correspond to styling, such as CssClass and Attributes.

Rather than using the TextAlign property, I would recommend simply adding a CSS class to your page, and use set CssClass property, which is on the base class WebControl.

Alternatively, you could set the text alignment by doing the following (but a CSS class would be cleaner):

this.Attributes["style"] = "text-align: center";

Of course, you could always add your own property that writes the correct CSS to the Attributes collection as well.

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