Valleriani
Valleriani

Reputation: 193

.NET User Controls -- Subclass with dropdown?

I am making a control, but I am having an issue.

Right now it works fine doing things such as:

Public Class CircularProgressBar : Inherits UserControl
Private _barWidth As Integer = 4
Public Property BarWidth() As Integer
    Get
        Return _barWidth
    End Get
    Set(ByVal v As Integer)
        _barWidth = v : Invalidate()
    End Set
End Property

..etc
End Class

The above for example will show in the properties window just fine. However, I want 1-5 bars to be displayed in the same control, it would work fine if I just defined an array/list or did _barWidth1, _barWidth2, etc. But obviously there is more values then just width and it looks very ugly in the properties page.

I wanted something like a DROPDOWN per bar, so I thought about adding a class, I.e.

Public Class CircularProgressBar : Inherits UserControl
    Public Class BarTest
     ....subclasscode
    End Class
..Class Code
End Class

However, the dropdown for the values won't appear. They do If I do Public Class BarTest : Inherits UserControl as well, but then it comes with a ton of not needed values. What I need is some way to have the drop down, but not with all the extras that inherting the usercontrol has. I am probably missing something very small.

Thanks!

Upvotes: 1

Views: 133

Answers (1)

Its not entirely clear what is being edited/saved or what these are - since you are talking about a property editor their nature is of some import.

UIType Editors are not a stylistic choice. There are several built-in which VS will automatically use based on the Type. Even if you write your own, you usually inherit from some existing base class so they don't often end up looking too dissimilar.

In the simplest case, maybe an exposed array would work (again, very little is known about what you are up to).

Public Class DemoBarControl
    Inherits UserControl

    Private barz As Int32()
   <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
    Public Property BarSizes As Int32()
        Get
            Return barz
        End Get
        Set(value As Int32())
            For n As Int32 = 0 To Math.Min(value.Count-1, 4)
                barz(n) = If(value(n) > 0, value(n), 0)
            Next
        End Set
    End Property

    Friend Function ShouldSerializeBarSizes() ...
    Friend Sub ResetBarSizes...

    Public Sub New()
        barz = {0, 1, 2, 3, 4}
    End Sub
    ...

This results in the following display:

enter image description here

Is that perhaps what you mean by a drop down?

That is the ExpandableObjectConverter automatically used in this case. The display is a little rough because it is just a naked array, but it is simple. Notice the ... button which will open the standard collection editor and allow the user to add new elements (sort of). Since the actual storage is an array, extras are filtered out in the Setter (and you can test ranges), but it might confuse the user. Serialization is handled by VS:

'
'DemoBarControl2
'
Me.DemoBarControl2.BarSizes = New Integer() {2, 5, 2, 3, 4}

So, all would seem to be well. It seems like there might be more to defining a Bar (such as which one). If so, you could define a Bar class to store {Index, Width, Color} - whatever, and expose a collection of them.

Upvotes: 1

Related Questions