Results 1 to 5 of 5

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

Threaded View

  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.

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