簡體   English   中英

引用類的繼承屬性的正確方法?

[英]Correct way to reference a class's inherited property?

我很肯定這很簡單,但是我已經完全凍結了大腦。

我正在嘗試創建自己的控件,該控件將為文本框添加一些特定功能,最終希望該控件在我的工具箱中可用,以在整個項目中使用。

例如,我想將此新控件添加到表單中,將.Text屬性更改為“ Something”,然后對New()子句中的.Text屬性執行一些邏輯,但是me.text始終返回“ ”,這意味着我的邏輯失敗了。

Public Class Class1
    Inherits Windows.Forms.TextBox

    Public Sub New()
        If Me.Text = "Something" Then
            MessageBox.Show("Do something")
        End If
    End Sub
End Class

關於如何克服這一障礙的任何建議?

提前致謝。


繼續...

好的,所以我認為這里還有更多上下文。

我想嘗試這種方法,以使自己重新進入OOP,因為我認為(!)這將是一個簡單的起點。

我肯定有一個可以下載的類可以滿足我的所有需求,但是我想學習,而不僅僅是從任何地方復制/編寫代碼。

因此,目標是要有一個文本框,該文本框還將提供“標題”屬性。 此屬性將包含在文本框中顯示的文本,直到單擊它為止,即“您的姓名”,然后單擊以輸入您的姓名(基本名稱)。

發布的原始代碼試圖證明我在New()方法中獲取.Text屬性值時遇到的問題,因為這是我試圖在.text和Caption之間進行比較的地方。 現在,代碼已更改,我不再這樣做。

我目前遇到的主要問題是...當我將控件添加到Windows窗體中時,文本框(.text)為空,我希望使用標題填充它。

下面發布的代碼是經過反復試驗和驗證的地方。 它不是太漂亮,還有一些事情需要更詳細地考慮,但是最終我當前的問題是讓.text屬性填充我想要的內容並在設計器中顯示。

Public Class TextBoxWithCaption
    Inherits Windows.Forms.TextBox

    Dim Caption_Text As String = "Caption"                               ' Define default caption property value
    Dim Caption_TextForeColor As Color = SystemColors.ControlDarkDark

    Dim NoCaption_TextForeColor As Color = SystemColors.WindowText

    Public Sub New()

        MyBase.New()
        Me.Text = Caption
        Me.Width = 150
        Me.ForeColor = Caption_TextForeColor
    End Sub

    Property Caption() As String
        Get
            Return Caption_Text
        End Get
        Set(value As String)

            ' Caption has been changed.
            '

            If Me.Text = Caption_Text Or String.IsNullOrEmpty(Me.Text) Then
                ' Caption property has changed, textbox has no user defined value, update the textbox with the new caption
                Me.Text = value
            End If

            Caption_Text = value

        End Set

    End Property


    Private Sub TextBoxWithCaption_GotFocus(sender As Object, e As EventArgs) Handles Me.GotFocus

        If Me.Text = Caption Then
            ' If the caption is displayed, then clear it ready for the user to type
            Me.Text = ""
            Me.ForeColor = NoCaption_TextForeColor
        End If
    End Sub


    Private Sub TextBoxWithCaption_LostFocus(sender As Object, e As EventArgs) Handles Me.LostFocus

        If Me.Text = "" Then
            ' If the user has not typed anything, restore the caption
            Me.Text = Caption
            Me.ForeColor = Caption_TextForeColor
        End If
    End Sub

End Class

Text屬性的初始值將始終是一個空字符串。

通過設計器更改屬性時,將始終更改屬性之前調用構造函數。 這是因為必須先創建控件,然后才能對其進行修改。 修改尚未初始化的Class1變量的Text屬性將引發NullReferenceException

如果您在InitializeComponent()方法中查看表單的.Designer.vb文件,它將看起來像這樣:

Private Sub InitializeComponent()
    Me.components = New System.ComponentModel.Container()
    Me.Class11 = New YourNamespace.Class1()
    Me.SuspendLayout()
    '
    'Class11
    '
    Me.Class11.Text = "Something"

    ...
End Sub

簡而言之:除非立即將值傳遞給構造函數,否則您將無法做任何事情。

您仍然可以這樣做,這是正確的方法,因為您希望對基礎類型調用構造函數

Public Sub New()
    MyBase.New() '<-- this will init yout base text box and now you can use it

    Me.Text = "Something" 

End Sub

但是,如果您擁有類似Resharper的工具,它會警告您有關“構造函數中的虛擬成員調用”的信息,這可能會導致您的屬性產生意外結果。 您可以在線閱讀。 要解決此問題,請添加此屬性

Public Overrides NotOverridable Property Text As String
    Get
        Return MyBase.Text
    End Get
    Set (value as string)
        MyBase.Text = value
    End Set
End Property

暫無
暫無

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

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