locked
How to Override the TEXT property of a CLASS? Anyone please?

    Question

  •  

    Hi to the experts,

     

    I have built a CLASS called CustomMsgBox with a vertical scrollbar on the RichTextBox and I want to

    OVERRIDE ( or will it be SHADOW ? )

    the TEXT property so that any text is written and read from the rtb RichTextBox.

     

    Additionally I always want this CustomMsgBox to open in the Center ( or Centre ) of the screen.

     

    Unfortunately I can not Inherit from MessageBox as it has not got an accessible NEW SUB.

    If it has a PRIVATE NEW Sub can it be overridden?

     

    Does CLOSE also call DISPOSE?

     

    Please help.

     

    This is what i have so far.

     

    Thanks, in advance, for any help.

     

     

     

     

    Regards,

     

    John

     

     

    Code Block

     

    Option Strict On

    Public Class Form1

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

    Dim cMsgBox As New CustomMsgBox

    cMsgBox.Show()

     

    End Sub

    End Class

     

     

     

     

     

     

    My CLASS code.

     

    Code Block

     

    Public Class CustomMsgBox

     

    Inherits System.Windows.Forms.Form

     

    Friend WithEvents btn As New Windows.Forms.Button

    Friend WithEvents rtb As New Windows.Forms.RichTextBox

     

    Public Sub New()

    Me.ControlBox = False

    Dim aPoint As Point

    Me.Width = 450

    Me.Height = My.Computer.Screen.WorkingArea.Height

    aPoint.X = 300

    aPoint.Y = 0

    Me.Location = aPoint

    aPoint.X = 5

    aPoint.Y = 5

    rtb.Location = aPoint

    rtb.Width = Me.Width - 20

    rtb.Height = Me.Height - 120

    rtb.ScrollBars = RichTextBoxScrollBars.Vertical

    Me.Controls.Add(rtb)

    btn.Width = 50

    btn.Height = 20

    aPoint.X = (Me.Width \ 2) - (btn.Width \ 2)

    aPoint.Y = Me.Height - 100

    btn.Location = aPoint

    btn.Text = "OK"

    Me.Controls.Add(btn)

    Me.Text = "Output message."

    End Sub

     

     

    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click

    Me.Close()

    End Sub

     

    'Public Property Text() As String

    'Get

    'Return rtb.Text

    'End Get

    'Set(ByVal value As String)

    'rtb.Text = value

    'End Set

    'End Property

     

    End Class

     

     

     

     

    Tuesday, December 04, 2007 11:50 AM

Answers

  • Yep, interesting. The property is first accessed when the form is initialized (within the base class contructor) and that's before any of the class variables are initialized.

     

    Just change the property to the following:

    Code Block

    Public Overrides Property Text() As String

    Get

    If Me.rtb Is Nothing Then

    Return MyBase.Text

    End If

    Return Me.rtb.Text

    End Get

    Set(ByVal value As String)

    Me.rtb.Text = value

    End Set

    End Property

     

     

    --

    Regards,

    Daniel Kuppitz

    Tuesday, December 04, 2007 2:31 PM

All replies

  •  John Oliver (UK)MSP, VSIP wrote:

    I have built a CLASS called CustomMsgBox with a vertical scrollbar on the RichTextBox and I want to

    OVERRIDE ( or will it be SHADOW ? )

    the TEXT property so that any text is written and read from the rtb RichTextBox.

     

    You can override the Text property, it's virtual.

     

     John Oliver (UK)MSP, VSIP wrote:

    Additionally I always want this CustomMsgBox to open in the Center ( or Centre ) of the screen.

     

    Code Block

    Me.StartPosition = FormStartPosition.CenterScreen

     

     

     John Oliver (UK)MSP, VSIP wrote:

    Unfortunately I can not Inherit from MessageBox as it has not got an accessible NEW SUB.

    If it has a PRIVATE NEW Sub can it be overridden?

     

    No.

     

     John Oliver (UK)MSP, VSIP wrote:

    Does CLOSE also call DISPOSE?

     

    Let that be the problem of the GC. You don't need to call Dispose explicitly.

     

    --

    Regards,

    Daniel Kuppitz

    Tuesday, December 04, 2007 12:18 PM
  •  Daniel Kuppitz wrote:

     

    You can override the Text property, it's virtual.

     

     

    Regards,

    Daniel Kuppitz

     

    Hi Daniel,

     

    Thanks for your post but I want to know how to override the the text property as well please.

     

    Thanks for the other answers by the way.

     

    What do you mean by GC please?

     

     

     

     

    Regards,

     

    John.

     

    Tuesday, December 04, 2007 12:28 PM
  •  John Oliver (UK)MSP, VSIP wrote:

    Thanks for your post but I want to know how to override the the text property as well please.

     

    Code Block

    Public Overrides Property Text() As String

    Get

    Return Me.rtb.Text

    End Get

    Set(ByVal value As String)

    Me.rtb.Text = value

    End Set

    End Property

     

     

     John Oliver (UK)MSP, VSIP wrote:

    What do you mean by GC please?

     

    GC = Garbage Collector

     

    The Form base class will trigger all the relevant Dispose stuff.

     

    --

    Regards,

    Daniel Kuppitz

Tuesday, December 04, 2007 12:54 PM
  • Hi ALL,

     

    I have decided to add a method to set the RichTextBox text or Append to it.

    This is so that the TEXT property affects the TitleBar text separately.

     

    I would still like to know how to OverRide though.

     

    Other than that I will settle for this.

     

    Add one button to a FORM to try this code please.

     

    Go to the PROJECT menu and select ADD CLASS to add the code in the second code snippet below.

     

     

     

     

    Regards,

     

    John

     

     

    Code Block

     

    Option Strict On

    Imports System.Environment

    Public Class Form1

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

    Dim cMsgBox As New CustomMsgBox

    cMsgBox.Text_For_The_Message = "This is test line of text."

    For index As Integer = 1 To 1000

    cMsgBox.Text = "Showing 1000 lines...."

    cMsgBox.Append_Text_To_The_Message = "This is line " & index.ToString & NewLine

    Next

    cMsgBox.Show()

    End Sub

    End Class

     

     

     

    My CLASS code.

     

     

    Code Block

     

    Public Class CustomMsgBox

    Inherits System.Windows.Forms.Form

    Friend WithEvents btn As New Windows.Forms.Button

    Friend WithEvents rtb As New Windows.Forms.RichTextBox

    Public Sub New()

    Me.ControlBox = False

    Me.StartPosition = FormStartPosition.CenterScreen

    Dim aPoint As Point

    Me.Width = 450

    Me.Height = My.Computer.Screen.WorkingArea.Height

    aPoint.X = 300

    aPoint.Y = 0

    Me.Location = aPoint

    aPoint.X = 5

    aPoint.Y = 5

    rtb.Location = aPoint

    rtb.Width = Me.Width - 20

    rtb.Height = Me.Height - 120

    rtb.ScrollBars = RichTextBoxScrollBars.Vertical

    Me.Controls.Add(rtb)

    btn.Width = 50

    btn.Height = 20

    aPoint.X = (Me.Width \ 2) - (btn.Width \ 2)

    aPoint.Y = Me.Height - 100

    btn.Location = aPoint

    btn.Text = "OK"

    Me.Controls.Add(btn)

    Me.Text = "Output message."

    End Sub

    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click

    Me.Close()

    End Sub

    Public Property Text_For_The_Message() As String

    Get

    Return rtb.Text

    End Get

    Set(ByVal value As String)

    rtb.Text = value

    End Set

    End Property

    Public WriteOnly Property Append_Text_To_The_Message() As String

    Set(ByVal value As String)

    rtb.AppendText(value)

    End Set

    End Property

    End Class

     

     

    Tuesday, December 04, 2007 1:08 PM
  •  Daniel Kuppitz wrote:

     

    Code Block

    Public Overrides Property Text() As String

    Get

    Return Me.rtb.Text

    End Get

    Set(ByVal value As String)

    Me.rtb.Text = value

    End Set

    End Property

     

     

     

    Regards,

    Daniel Kuppitz

     

    Hi Daniel,

     

    Before I mark your last post As Answer, do you know why this happens?

     

    Thanks for that but with the following code the RETURN section gives a

    OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT error.

     

    Why when I am using NEW in the main code?

     

     

     

     

    Regards,

     

    John

     

     

    Code Block

     

    Option Strict On

    Imports System.Environment

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim cMsgBox As New CustomMsgBox

    cMsgBox.Show()

     

    cMsgBox.Text = "Showing 1000 lines...."

     

    For index As Integer = 1 To 1000

    cMsgBox.Text &= "This is line " & index.ToString & NewLine

    Next

     

    End Sub

    End Class

     

     

     

     

     

    Code Block

    Public Class CustomMsgBox

    Inherits System.Windows.Forms.Form

    Friend WithEvents btn As New Windows.Forms.Button

    Friend WithEvents rtb As New Windows.Forms.RichTextBox

    Public Sub New()

    Me.SuspendLayout()

    Me.ControlBox = False

    Me.StartPosition = FormStartPosition.CenterScreen

    Dim aPoint As Point

    Me.Width = 450

    Me.Height = My.Computer.Screen.WorkingArea.Height

    aPoint.X = 300

    aPoint.Y = 0

    Me.Location = aPoint

    aPoint.X = 5

    aPoint.Y = 5

    rtb.Location = aPoint

    rtb.Width = Me.Width - 20

    rtb.Height = Me.Height - 120

    rtb.ScrollBars = RichTextBoxScrollBars.Vertical

    Me.Controls.Add(rtb)

    btn.Width = 50

    btn.Height = 20

    aPoint.X = (Me.Width \ 2) - (btn.Width \ 2)

    aPoint.Y = Me.Height - 100

    btn.Location = aPoint

    btn.Text = "OK"

    Me.Controls.Add(btn)

    Me.Text = "Output message."

    Me.ResumeLayout(False)

    End Sub

    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click

    Me.Close()

    End Sub

     

     

     

     

    Public Overrides Property Text() As String

    Get

    Return Me.rtb.Text

    End Get

    Set(ByVal value As String)

    Me.rtb.Text = value

    End Set

    End Property

     

    End Class

     

     

    Tuesday, December 04, 2007 1:36 PM
  • Yep, interesting. The property is first accessed when the form is initialized (within the base class contructor) and that's before any of the class variables are initialized.

     

    Just change the property to the following:

    Code Block

    Public Overrides Property Text() As String

    Get

    If Me.rtb Is Nothing Then

    Return MyBase.Text

    End If

    Return Me.rtb.Text

    End Get

    Set(ByVal value As String)

    Me.rtb.Text = value

    End Set

    End Property

     

     

    --

    Regards,

    Daniel Kuppitz

    Tuesday, December 04, 2007 2:31 PM
  •  Daniel Kuppitz wrote:

    Yep, interesting. The property is first accessed when the form is initialized (within the base class contructor) and that's before any of the class variables are initialized.

     

    Just change the property to the following:

    Code Block

    Public Overrides Property Text() As String

    Get

    If Me.rtb Is Nothing Then

    Return MyBase.Text

    End If

    Return Me.rtb.Text

    End Get

    Set(ByVal value As String)

    Me.rtb.Text = value

    End Set

    End Property

     

     

    --

    Regards,

    Daniel Kuppitz

     

    Hi Daniel,

     

    Thanks for all your help.

    The point you make highlighted above is something worth remembering I think.

     

    I changed it to this as I prefer it this way.>>

     

    Code Block

     

    Public Overrides Property Text() As String

     

    Get

    If Me.rtb Is Nothing Then

    Return MyBase.Text

    Else

    Return Me.rtb.Text

    End If

    End Get

     

    Set(ByVal value As String)

    Me.rtb.Text = value

    End Set

     

    End Property

     

     

     

     

     

     

    Regards,

     

    John

     

    Tuesday, December 04, 2007 4:39 PM