Results 1 to 7 of 7

Thread: [RESOLVED] Updating the Visual Studio Properties Window

  1. #1

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Resolved [RESOLVED] Updating the Visual Studio Properties Window

    I'm designing a custom toolbox component. Some of the public properties are interdependent. At present, when the user changes one of these values in the Designer, the dependent value doesn't update until the user clicks on it. Does anyone know how I can "refresh" the Properties window to show the new value of the dependent property?

    BB

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Updating the Visual Studio Properties Window

    Perhaps you can use the RaiseComponentChanged method of the control designer. I'm not sure if that works but it's worth a try. If you are using a custom designer already you can just call
    Code:
    Me.RaiseComponentChanged(TypeDescriptor.GetProperties(Me.Control)("PropertyNameHere"), Nothing, Nothing)
    I'm trying it now but I can only work on it for 2 minutes every 5/10 minutes so it's not going very fast

    EDIT
    It doesn't seem to work

    Here's the code I tried:
    Code:
    Imports System.ComponentModel.Design
    Imports System.Windows.Forms.Design
    Imports System.ComponentModel
    
    <Designer(GetType(UserControl1.UserControl1Designer))> _
    Public Class UserControl1
    
        Private _Prop As String
    
        Private Event PropChanged As EventHandler
    
        ' Main property
        Public Property Prop() As String
            Get
                Return _Prop
            End Get
            Set(ByVal value As String)
                _Prop = value
    
                ' raise event when changed
                Me.OnPropChanged()
            End Set
        End Property
    
        ' Dependent property
        Public Property Prop2() As String
            Get
                Return Me.Prop
            End Get
            Set(ByVal value As String)
                Me.Prop = value
            End Set
        End Property
    
        Private Sub OnPropChanged()
            RaiseEvent PropChanged(Me, EventArgs.Empty)
        End Sub
    
        ' Designer
        Friend Class UserControl1Designer
            Inherits ControlDesigner
    
            ' Attach PropChanged event
            Public Overrides Sub Initialize(ByVal component As System.ComponentModel.IComponent)
                MyBase.Initialize(component)
    
                Dim uc As UserControl1 = DirectCast(Me.Control, UserControl1)
                AddHandler uc.PropChanged, AddressOf PropChanged
            End Sub
    
            ' RaiseComponentChanged to notify a change... Doesn't seem to work :(
            Private Sub PropChanged(ByVal sender As Object, ByVal e As EventArgs)
                Me.RaiseComponentChanged(TypeDescriptor.GetProperties(Me.Control)("Prop"), Nothing, Nothing)
                Me.RaiseComponentChanged(TypeDescriptor.GetProperties(Me.Control)("Prop2"), Nothing, Nothing)
            End Sub
        End Class
    End Class
    But it still doesn't update Prop2 until I select it in the property grid. Perhaps there's other methods you can call here to get it to update, but I'm not sure.

  3. #3

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Updating the Visual Studio Properties Window

    Thanks for all your effort Nick. I've just started scratching at the surface of the huge area of component design. (In fact, I'm not even sure what a ControlDesigner is for, let alone using one. Presumably that means I'm using the default Designer.)

    Anyway, your posting prompted me to have another rummage around in MSDN and this time I luckily found the answer -- the PropertyDescriptor Class. It offers a dozen or so useful methods and properties that affect the Properties Window, for example GetValue, SetValue and CanResetValue. I just tried SetValue out on a simple example and it works as expected. So: Resolved.

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] Updating the Visual Studio Properties Window

    Cool. Just so you know, a ControlDesigner can be used to extend the design-time experience of any custom controls (or UserControls). Things that I have used them for include:

    1. Enabled design-time support for many controls, such as a TabControl on a UserControl. Normally, you can't 'control' the TabControl because it is on the UserControl, but by using a custom designer you can get it to respond to, for example, mouse events, and behave almost like it is in run-time. For a good but complex example, see my Wizard control. For an easier example, see my expandable groupbox.

    2. Changing the snap lines.

    3. Provide 'smart tags' for controls, which appear when you click the [>] icon on the top-right corner.

    4. Control how the control is selectable and resizable.

    5. Control which control can be parented by (or to, can't remember) this control.

    And probably many more I can't remember right now.

    You are probably using the default designer yes. If you don't specify a Designer attribute on your class, it uses the default.

  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [RESOLVED] Updating the Visual Studio Properties Window

    I think the best way (finally, I know something about an attribute) is the RefreshProperties() attribute:
    Code:
    <System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All)> Public Property ....
    You don't even have to write any handling code.

  6. #6

  7. #7

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: [RESOLVED] Updating the Visual Studio Properties Window

    Perfect, minitech. It's an elegant alternative to the PropertyDescriptor. Hyperresolved. BB

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