Results 1 to 5 of 5

Thread: [RESOLVED] Using properties of a hosted control in the Designer

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Resolved [RESOLVED] Using properties of a hosted control in the Designer

    I have created a custom TextProgressBar that is an item in the StatusStrip drop-down list and is hosted via the ToolStripControlHost. I would like to expose the TextProgressBar properties Minimum, Maximum, and Step in the Designer.

    Here are the 2 classes (ToolStripTextProgressBar and TextProgressBar):

    Code:
    Imports System.Windows.Forms.Design '...required for ToolStripItemDesignerAvailability
    <ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip Or ToolStripItemDesignerAvailability.StatusStrip)>
    Public Class ToolStripTextProgressBar
    
        Inherits ToolStripControlHost
    
        '...call the base constructor passing in a TextProgressBar instance
        Public Sub New()
            MyBase.New(New TextProgressBar)
            CType(Control, TextProgressBar).Style = ProgressBarStyle.Continuous
        End Sub
    
        Public ReadOnly Property ProgressBarControl As TextProgressBar
            Get
                Return CType(Control, TextProgressBar)
            End Get
        End Property
    
        '...expose the ToolStripTextProgressBar.Value as a property (IDE Designer)
        Public Property Value As Integer
            Get
                Return Me.ProgressBarControl.Value
            End Get
            Set
                Me.ProgressBarControl.Value = Value
            End Set
        End Property
    
    End Class
    Code:
    Public Class TextProgressBar
    
        Inherits ProgressBar
    
        Public Overrides Property Text As String
        '...http://www.pinvoke.net/default.aspx/Constants/WM.html
        '   WM_PAINT = x0F (decimal value = 15...CDec("&H0F") = 15)
        Private Const WmPaint = 15  '...occurs when the control needs repainting
    
        '...WndProc receives all messages directed to the current window. In order to
        '   avoid overwriting your text, make sure the default code executes first,
        '   then call the custom routine to display the overlaid text
    
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            MyBase.WndProc(m)
            If m.Msg = WmPaint Then
                PaintText()
            End If
        End Sub
    
        Private Sub PaintText()
    
            Dim s As String = Text  '...this is the TextProgressBar.Text property
            Dim percent As Integer
            Dim textSize As SizeF
    
            '...display either a percentage or custom text
            If s = "" AndAlso Maximum - Minimum <> 0 Then
                '...display a percentage
                percent = CInt(100 * (Value - Minimum) / (Maximum - Minimum))
                s = percent.ToString & "%"
            Else
                '...display custom text
            End If
    
            '...get the graphics object and calculate drawing parameters based on the current Font specs
            Using g As Graphics = Me.CreateGraphics()
                textSize = g.MeasureString(s, Me.Font)
                Using b = New SolidBrush(ForeColor)
                    g.DrawString(s, Me.Font, Brushes.Black, CSng(Me.Width / 2 - textSize.Width / 2), CSng(Me.Height / 2 - textSize.Height / 2))
                End Using
            End Using
    
            textSize = Nothing
    
        End Sub
    
    End Class
    I've successfully exposed the ToolStripTextProgressBar property Value as shown above in the ToolStripTextProgressBar class.

    When I try to do the same for the Minimum or Maximum properties, they appear in the Designer as properties of the hosted ToolStripTextProgressBar, but the Designer will not accept a manual edit of the property.

    Code:
        '...expose the ToolStripTextProgressBar.Minimum as a property (IDE Designer)
        Public Property Minimum As Integer
            Get
                Return Me.ProgressBarControl.Minimum
            End Get
            Set
                Me.ProgressBarControl.Minimum = Minimum
            End Set
        End Property
    For example, when manually setting Minimum property to "1", the Designer immediately resets it to "0".

    Note that I can set the Minimum property at runtime via the ProgressBarControl property of a ToolStripTextProgressBar:

    Code:
    tspbMainCountdown.ProgressBarControl.Minimum = 1
    I need some help to figure this out. Thanks.
    Last edited by Mark@SF; Apr 7th, 2020 at 06:41 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using properties of a hosted control in the Designer

    This is wrong:
    Code:
        '...expose the ToolStripTextProgressBar.Minimum as a property (IDE Designer)
        Public Property Minimum As Integer
            Get
                Return Me.ProgressBarControl.Minimum
            End Get
            Set
                Me.ProgressBarControl.Minimum = Minimum
            End Set
        End Property
    A property setter ALWAYS has a parameter named value and that is what you use in the setter. That parameter is generally implicit these days but you could make it explicit:
    Code:
        '...expose the ToolStripTextProgressBar.Minimum as a property (IDE Designer)
        Public Property Minimum As Integer
            Get
                Return Me.ProgressBarControl.Minimum
            End Get
            Set(value As Integer)
                Me.ProgressBarControl.Minimum = Minimum
            End Set
        End Property
    Your other property worked because the property itself was named Value. You thought you were using the property in the setter but you were actually using the parameter. You need to use that parameter in ALL setters:
    Code:
        '...expose the ToolStripTextProgressBar.Minimum as a property (IDE Designer)
        Public Property Minimum As Integer
            Get
                Return Me.ProgressBarControl.Minimum
            End Get
            Set
                Me.ProgressBarControl.Minimum = Value
            End Set
        End Property
    One thing you will find is that, if you include an explicit parameter then you can name it value but, if it's implicit, the VB code editor will change it to Value, i.e. change the casing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: Using properties of a hosted control in the Designer

    jmc -

    Thanks! I've got the Minimum and Maximum properties working now.

    I am getting a "Keyword is not valid as an identifier." error with the my code for the Step property:

    Code:
        '...expose the ToolStripTextProgressBar.Step as a property (IDE Designer)
        Public Property Step As Integer
            Get
                Return Me.ProgressBarControl.Step
            End Get
            Set
                Me.ProgressBarControl.Step = Value
            End Set
        End Property
    The compiler does not like the "Step" in the "Public Property Step As Integer" statement. Can you help me with this also?

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: Using properties of a hosted control in the Designer

    jmc -

    Thanks! I've got the Minimum and Maximum properties working now.

    I am getting a "Keyword is not valid as an identifier." error with the my code for the Step property:

    Code:
        '...expose the ToolStripTextProgressBar.Step as a property (IDE Designer)
        Public Property Step As Integer
            Get
                Return Me.ProgressBarControl.Step
            End Get
            Set
                Me.ProgressBarControl.Step = Value
            End Set
        End Property
    The compiler does not like the "Step" in the "Public Property Step As Integer" statement. Can you help me with this also?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: Using properties of a hosted control in the Designer

    jmc -

    I got it...enclosing "Step" in brackets was the solution:

    Code:
        '...expose the ToolStripTextProgressBar.Step as a property (IDE Designer)
        Public Property [Step] As Integer
            Get
                Return Me.ProgressBarControl.Step
            End Get
            Set
                Me.ProgressBarControl.Step = Value
            End Set
        End Property
    Thank you again for your help!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width