簡體   English   中英

如何在C#.NET中為子類設置特定屬性?

[英]How do I set specific properties for a subclass in C# .NET?

首先,我要說的是我是.NET的新手,所以如果我的問題太天真或答案太簡單,我會事先道歉。 我做了一些研究並測試了不同的東西,但我無法讓它發揮作用。 我正在嘗試做什么:

我的表單中有很多不同的工具提示,我希望它們具有相同的屬性。 例如:

mySampleTooltip.ShowAlways = true;
mySampleTooltip.IsBalloon = true;
mySampleTooltip.AutomaticDelay = 750;
mySampleTooltip.AutoPopDelay = 32767;

我認為可能有一個更優雅的解決方案,而不是為每個工具提示執行此操作並將代碼粘貼100次。 我的想法是創建一個類ToolTip的子類,我稱之為PermaToolTip並從這個特定的類創建對象(順便說一句是正確的方法還是有更好的方法?)。

我的代碼看起來像這樣:

public class PermaToolTip : ToolTip
{
    private bool _ShowAlways;
    private bool _IsBalloon;
    private int _AutomaticDelay;
    private int _AutoPopDelay;

    // SET ShowAlways Property for subclass
    public bool ShowAlways
    {
        get
        {
            return _ShowAlways;
        }
        set
        {
            _ShowAlways = true;
        }
    }
    // SET IsBalloon Property for subclass
    public bool IsBalloon
    {
        get
        {
            return _IsBalloon;
        }
        set
        {
            _IsBalloon = true;
        }
    }
    // SET AutomaticDelay Property for subclass
    public int AutomaticDelay
    {
        get
        {
            return _AutomaticDelay;
        }
        set
        {
            _AutomaticDelay = 750;
        }
    }
    // SET AutoPopDelay Property for subclass
    public int AutoPopDelay
    {
        get
        {
            return _AutoPopDelay;
        }
        set
        {
            _AutoPopDelay = 32767;
        }
    }
}

但它不起作用。 特別:

1)我在屬性名稱下方得到一條綠線,並出現警告:警告:'ResellerRatingsGUI.PermaToolTip.ShowAlways'隱藏繼承的成員'System.Windows.Forms.ToolTip.ShowAlways'。 如果要隱藏,請使用new關鍵字。

2)我實例化PermaToolTip類的對象,它沒有我想要的屬性。

回答評論:它不是用戶定義的控件。 這是.NET控件。

知道我做錯了什么嗎?

基類已經具有這些屬性。 做你正在做的事情會導致編譯器抱怨(因為你隱藏基類成員而不使用new來表示你想要隱藏它)。

您不需要創建新屬性 - 只需在構造函數中設置基類默認值

public class PermaToolTip : ToolTip
{
     public PermaToolTip()
     {
        // Define defaults differently now
        this.ShowAlways = true;
        this.IsBalloon = true;
        this.AutomaticDelay = 750;
        this.AutoPopDelay = 32767;
     }
}

這將導致您的類使用ToolTip屬性,但具有不同的默認值。

首先,如另一個答案所述,您的子類不應該具有與基類中的成員同名的成員,除非您的意圖是覆蓋它們的行為。 在這種情況下,您應該只覆蓋虛擬抽象成員。

從設計角度來看,我建議采用不同的方法。 看起來子類的唯一目的是創建一個填充了默認值的ToolTip實例。 沒有必要使用繼承來實現這一目標。 相反,你應該考慮某種幫助器,它會為你提供一個填充了默認值的ToolTip實例。 那么你就不會陷入一種你不一定想要或不需要的具體類型。

示例類:

public static class DefaultToolTip 
{
  public static void Create()
  {
    ToolTip tip = new ToolTip();
    tip.ShowAlways = true;
    tip.IsBalloon = true;
    tip.AutomaticDelay = 750;
    tip.AutoPopDelay = 32767;
    return tip;
  }
}

用法:

ToolTip toolTip = DefaultToolTip.Create();

創建一個工具提示,然后使用SetToolTip,這樣你就不需要100個,只需要幾個甚至一個。

直接來自MSDN

  private void Form1_Load(object sender, System.EventArgs e)
  {
     // Create the ToolTip and associate with the Form container.
     ToolTip toolTip1 = new ToolTip();

     // Set up the delays for the ToolTip.
     toolTip1.AutoPopDelay = 5000;
     toolTip1.InitialDelay = 1000;
     toolTip1.ReshowDelay = 500;
     // Force the ToolTip text to be displayed whether or not the form is active.
     toolTip1.ShowAlways = true;

     // Set up the ToolTip text for the Button and Checkbox.
     toolTip1.SetToolTip(this.button1, "My button1");
     toolTip1.SetToolTip(this.checkBox1, "My checkBox1");
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM