[英]How can I allow for the modifications of properties that are initialized in the constructor of an inherited control (button) C# winforms
我正在嘗試創建一個繼承的控件,該控件具有一些默認值,這些默認值可以節省我一些時間,因為這些控件在每種形式上都相同。
我為此創建了2個類。 繼承UltraButton的基類,在其中放置了所有按鈕都通用的值。 在這種情況下,ButtonStyle,圖像大小和控件大小。
這是課程:
public class BaseButton : Infragistics.Win.Misc.UltraButton
{
public BaseButton()
{
//We set the properties of the base object.
ButtonStyle = Infragistics.Win.UIElementButtonStyle.Popup;
ImageSize = new Size(32, 32);
Size = new Size(100, 62);
}
[Browsable(false)]
public new Size Size
{
get { return base.Size; }
set { base.Size = value; }
}
}
現在,我需要創建一個Reset按鈕(從BaseButton繼承),該按鈕將指定要用於ResetBt的圖像,文本和控件的默認錨點:
public class ResetBt : BaseButton
{
private readonly string _text;
public ResetBt()
{
Appearance.Image = Properties.Resources.Restart_32_32;
_text = "&Reset";
base.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
}
[Browsable(false)]
public override string Text
{
get { return _text; }
}
}
這是我發現問題的地方。 如果嘗試在設計時更改Anchor設置,則可以但在運行時更改傳遞給構造函數的值,從而覆蓋設計時輸入的所有內容。 對於按鈕的圖像和“文本”值來說,這種行為很好,但是對於其他屬性(例如“錨點”),這是不合適的。
將默認值傳遞給繼承的控件的某些屬性並仍允許在設計時進行修改的最佳方法是什么?
謝謝您的幫助。
也許嘗試其他構造函數:
public BaseButton(Image image, Anchor anchor)
{
Image = image;
Anchor = anchor
}
public ResetBt(Image image,Anchor anchor): Base(Image image,Anchor anchor)
{
_text = "&Reset";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.