Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Will this work for a user control Property?

Status
Not open for further replies.

MrPeanut

Technical User
Nov 1, 2004
12
US
Hey all,

I have a control and for this control I want to create a property (eg. 'ItemName'). During design time I want the property to have a list of options that are populated from a Dataset. Is this possible?

I know I could use an Enumeration for the property list, but the values in the list are not static so the enumeration will not work.

Thanks in advance!
 
You will need a custom designer class to do this. See MSDN for IDesigner and DesignerAttribute. Sorry, I don't have time to post any examples other than the one below from MSDN. Hopefully this will get you started. Good Luck!
Code:
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Windows.Forms

' ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref3/html/T_System_ComponentModel_Design_IDesigner.htm

' A DesignerAttribute associates the example IDesigner with an example control.
<DesignerAttribute(GetType(ExampleIDesigner))> _
Public Class TestControl
    Inherits System.Windows.Forms.UserControl

    Public Sub New()
    End Sub
End Class

<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Class ExampleIDesigner
    Implements System.ComponentModel.Design.IDesigner

    ' Local reference to the designer's component.
    Private component_ As IComponent

    ' Public accessor to the designer's component.
    Public ReadOnly Property Component() As System.ComponentModel.IComponent Implements IDesigner.Component
        Get
            Return component_
        End Get
    End Property

    Public Sub New()
    End Sub

    Public Sub Initialize(ByVal component As System.ComponentModel.IComponent) Implements IDesigner.Initialize
        ' This method is called after a designer for a component is created,
        ' and stores a reference to the designer's component.
        Me.component_ = component
    End Sub

    ' This method peforms the 'default' action for the designer. The default action 
    ' for a basic IDesigner implementation is invoked when the designer's component 
    ' is double-clicked. By default, a component associated with a basic IDesigner 
    ' implementation is displayed in the design-mode component tray.
    Public Sub DoDefaultAction() Implements IDesigner.DoDefaultAction
        ' Shows a message box indicating that the default action for the designer was invoked.
        MessageBox.Show("The DoDefaultAction method of an IDesigner implementation was invoked.", "Information")
    End Sub

    ' Returns a collection of designer verb menu items to show in the 
    ' shortcut menu for the designer's component.
    Public ReadOnly Property Verbs() As System.ComponentModel.Design.DesignerVerbCollection Implements IDesigner.Verbs
        Get
            Dim verbs_ As New DesignerVerbCollection()
            Dim dv1 As New DesignerVerb("Display Component Name", New EventHandler(AddressOf Me.ShowComponentName))
            verbs_.Add(dv1)
            Return verbs_
        End Get
    End Property

    ' Event handler for displaying a message box showing the designer's component's name.
    Private Sub ShowComponentName(ByVal sender As Object, ByVal e As EventArgs)
        If Not (Me.Component Is Nothing) Then
            MessageBox.Show(Me.Component.Site.Name, "Designer Component's Name")
        End If
    End Sub

    ' Provides an opportunity to release resources before object destruction.
    Public Sub Dispose() Implements IDisposable.Dispose
    End Sub

End Class
 
I forgot to mention this is for a Web Project. I can't use the System.Windows.Forms class library.

I was browsing around and people made mention to using the UITypeEditor and I'm wondering within that, how would I allow the property to set it's values to that of the record set?

The code I have so far:

Code:
Imports System.Web
Imports System.Data
Imports System.Data.Common
Imports System.ComponentModel
Imports System.Drawing.Design
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports System.Security.Permissions

Namespace MyControls

    Public Class PropertyDropDownListEditor
        Inherits System.Drawing.Design.UITypeEditor

        Public Overrides Function GetEditStyle(ByVal context As _
                                       ITypeDescriptorContext) As UITypeEditorEditStyle
            Return UITypeEditorEditStyle.DropDown
        End Function

        Public Overloads Overrides _
              Function EditValue(ByVal context As ITypeDescriptorContext, _
                                 ByVal provider As IServiceProvider, _
                                 ByVal value As Object) As Object

            If context Is Nothing _
                OrElse provider Is Nothing _
                OrElse context.Instance Is Nothing Then
                Return MyBase.EditValue(provider, value)
            End If

            Try
                'Put Data here to populate property's list values?  IF so that's where I need some help.

            Catch ex As Exception
                Throw New Exception(ex.Message)
            End Try
            Return MyBase.EditValue(context, provider, value)
        End Function

    End Class

<-- more code -->
End Namespace
 
How I do it for other things is make a collection of a class. Make a class that will contain all of the information you need for a record and then make a collection of that class on the control. You could also make a collection of structures/objects. Really there are a lot of ways you could do it. I tend to use classes rather than structures because I usually end up adding my own events to that class.

-I hate Microsoft!
-Forever and always forward.
 
Hey all,

I tried creating Array List with all the values and in Design Time when I click the property I get a blank screen displayed. The Object collection Editor is blank.

I want to make it so during design when I click the proprty in the Propertygrid it will display the values of my list.

Code:
    Public NotInheritable Class MyTextBox
        Inherits System.Web.UI.WebControls.TextBox

        Public _FieldNames As ArrayList

        Public Property FieldNameList() As ArrayList
            Get
                Return _FieldNames
            End Get
            Set(ByVal value As ArrayList)
                _FieldNames = value
            End Set
        End Property


        Public Function RetrieveNames() As DataSet
            Dim Ds As New DataSet
            Dim db As Database = DatabaseFactory.CreateDatabase()
            Dim dbcInsert As DbCommand = db.GetStoredProcCommand("GetNames")

            Try
                Ds = db.ExecuteDataSet(dbcInsert)
                Return Ds

            Catch ex As Exception
                Throw New Exception(ex.Message)
            End Try

        End Function

        Public Function CreateNameList() As ArrayList
            Dim returnList As New ArrayList()
            Dim ds As DataSet
            Dim i As Integer


            'Obtain the list of FieldNames into a dataset
            ds = RetrieveNames()
            Try

                'Read through the Dataset and populate the ArrayList
                For i = 0 To ds.Tables(0).Rows.Count - 1
                    returnList.Add(ds.Tables(0).Rows(i).Item(i).ToString())
                    Console.WriteLine(returnList.Item(i).ToString())
                Next

                Return returnList

            Catch ex As Exception
                Throw New Exception(ex.Message)
            End Try

        End Function

        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            FieldNameList = CreateNameList()
            MyBase.OnPreRender(e)
        End Sub
 
I want to make it so during design when I click the proprty in the Propertygrid it will display the values of my list.
If you figure that out let me know because I've only had simple properties (string/int/etc) show up. Things like arrays/collections/never do.

-I hate Microsoft!
-Forever and always forward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top