Reputation: 1133
I made a custom control for WinForms in C#. One of it's properties is an audio stream that will be played at some point. Is there a way I can tell VS that this property should be chosen from the resource manifest (specifically audio resources) with a property or similar?
The commented property is an audio stream.
This is how it shows in the WinForms designer.
This is what you get when you click the edit [...] button. I want this to be replaced by an [audio] resource chooser.
Upvotes: 2
Views: 700
Reputation: 1258
I'm posting a code below that shows how I would do it myself.
Just some observations:
AudioName
which will be set in the designer. This property refers to the resource name in your assembly. The Audio
(read-only) property of type Stream you are interested in, will then be retrieved from the chosen resource name.As Marc-André Jutras pointed in his answer, you'll need to code your own UITypeEditor
or TypeConverter
to retrieve the Audio resources from your assembly. I've chosen in this case to implement a custom TypeConverter
.
Public Class Form1
<System.ComponentModel.TypeConverter(GetType(AudioConverter)), System.ComponentModel.DefaultValue("(none)")>
Public Property AudioName As String
<System.ComponentModel.Browsable(False)> 'This will make the property not appear in the designer
Public ReadOnly Property Audio As Stream
Get
If String.IsNullOrWhiteSpace(AudioName) OrElse
AudioName = "(none)" Then
Return Nothing
End If
Try
Return My.Resources.ResourceManager.GetStream(AudioName)
Catch ex As Exception
Return Nothing
End Try
End Get
End Property
End Class
Public Class AudioConverter
Inherits System.ComponentModel.TypeConverter
Public Overrides Function GetStandardValuesSupported(context As System.ComponentModel.ITypeDescriptorContext) As Boolean
Return True
End Function
' Set the property to be informed only from predefined (standard) values
Public Overrides Function GetStandardValuesExclusive(context As System.ComponentModel.ITypeDescriptorContext) As Boolean
Return True
End Function
' Get possible values that will be inserted in the drop-down list
Public Overrides Function GetStandardValues(context As System.ComponentModel.ITypeDescriptorContext) As System.ComponentModel.TypeConverter.StandardValuesCollection
Dim returnList As New System.Collections.Generic.List(Of String)
returnList.Add("(none)")
For Each resItem As System.Collections.DictionaryEntry In My.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.InvariantCulture, True, False)
Dim resourceEntry As String = resItem.Key
If TypeOf resourceEntry Is String Then
returnList.Add(resourceEntry)
End If
Next
Return New System.ComponentModel.TypeConverter.StandardValuesCollection(returnList)
End Function
End Class
Upvotes: 0
Reputation: 21024
What you are looking for is:
[Editor(typeof(MyAudioEditor), typeof(UITypeEditor)]
This attribute let you specify a specific editor to be used when that property is displayed in a property grid.
You can then derive and create your new editor from a base type. The base type must be or must derive from System.Drawing.Design.UITypeEditor.
In most case, when the Editor is invoked, you pop-up a form of your choice, and bind its return value to your property.
UITypeEditor has 4 virtual methods and one virtual property that let you change all the behavior when someone interact with your property or when your property is painted in the grid.
Upvotes: 1