Link to home
Start Free TrialLog in
Avatar of Clif
ClifFlag for United States of America

asked on

Web Custom Control - Multi-level Properties

I am creating a Web Custom Control based on a calendar dll I wrote.

The DLL has a class called CellProperties which had properties such as backcolor, forecolor, etc.  Then when I instantialed the dll I would set properties such as:

objCalendar.Heading.BackColor = Color.AliceBlue
objCalendar.Heading,ForeColor = Color.Black
etc...

Now, in creating this as a Web Custom Control, I want the properties to appear similar to the way Font shows up in the Propoerties screen.  That is, a title of Font with a "plus" box.  Then clicking on the "plus" will drop down the sub properties.

How do I do this?

I tried the following, but it doesn't work:

    <Bindable(True), Category("Appearence"), DefaultValue("")> Property [Heading]() As myCalendar.CellProperties
        Get
            Return m_propHeading
        End Get
        Set(ByVal Value As BCP.Web.Calendar.CellProperties)
            m_propHeading = Value
        End Set
    End Property
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

Have a look on the following article:

http://www.theserverside.net/developmentor/downloads/whitepapers/designint.pdf

HTH, nauman
Avatar of Clif

ASKER

Being the lazy person I am...

Is there any example in Visual Basic?
Avatar of Justin_W
Justin_W

First, you need to make your custom property Public.  E.g.:

<Bindable(True), Category("Appearence"), DefaultValue("")> Public Property [Heading]() As myCalendar.CellProperties

All of the properties in myCalendar.CellProperties that you want to expose should also be Public.
Actually, the .Net way is very easy either if you code in C# or VB. You just need to have concepts of Object Oriented programming, and since VB.net is an object oriented language, you will need to learn that too :) Its not that diffcult and I think you will be able to learn it quickly. While learning just glance on the way objects or statement defined in both the language.

Also have a look at the following URL cuz I think it has some good stuf:

http://www.microsoft.com/downloads/details.aspx?familyid=3ff9c915-30e5-430e-95b3-621dccd25150&displaylang=en

HTH, nauman
ASKER CERTIFIED SOLUTION
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Clif

ASKER

The tutorial is also in C#.

I can code pretty darn well in VB.  I can also read a good bit of C (and C++ and C#).  But there are a few things I don't get.

For example, in the tutorial you posted:

   [TypeConverter(typeof(PersonConverter))]
   public class Person {
      private string firstName = "";
      private string lastName = "";
      private int    age = 0;
   
      public int Age {
         get {
            return age;
         }
         set {
            age = value;
         }
      }
     
      public string FirstName {
         get {
            return firstName;
         }
         set {
            this.firstName = value;
         }
      }

      public string LastName {
         get {
            return lastName;
         }
         set {
            this.lastName = value;
         }
      }      
   }

I can convert all of the above into VB except for the first line.

What the heck does this mean?
   [TypeConverter(typeof(PersonConverter))]


Also, again from the tutorial:
internal class PersonConverter : ExpandableObjectConverter {
     
   public override bool CanConvertFrom(
         ITypeDescriptorContext context, Type t) {

         if (t == typeof(string)) {
            return true;
         }
         return base.CanConvertFrom(context, t);
   }
.
.
.
}

Is this the same as (in VB)
Private Class PersonConverter
    Inherits ExpandableObjectConverter
    Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, t As Type)
        If TypeOf(t) Is String Then
            Return True
        End If
        'In the next line, what is the VB equivelant of 'base'?
        Return base.CanConvertFrom(context, t)
   End Function
.
.
.
End Class
> What the heck does this mean?
   [TypeConverter(typeof(PersonConverter))]

It is the C# version of Attributes:
   <TypeConverterAttribute(typeof(PersonConverter))> Public Class Person
Private Class PersonConverter
    Inherits ExpandableObjectConverter
    Friend Overrides Function CanConvertFrom(context As ITypeDescriptorContext, t As Type)
        If TypeOf(t) Is String Then
            Return True
        End If
        'In the next line, what is the VB equivelant of 'base'?
        Return base.CanConvertFrom(context, t)
   End Function
.
.
.
End Class
base -> mybase
Avatar of Clif

ASKER

Ok, using the code from the link provided by nauman_ahmed at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/vsnetpropbrow.asp
I rewrote it from C# to VB.net.

It isn't working.  Where did I go wrong?

'Step 1:
'Tried:
'       <TypeConverterAttribute(typeof(PersonConverter))> Public Class Person
' as JustinW suggested, error "Is Expected")
<TypeConverter("PersonConverter")> Public Class Person
    Private m_sFirstName As String = ""
    Private m_sLastName As String = ""
    Private m_nAge As Integer = 0

    Public Property Age() As Integer
        Get
            Return m_nAge
        End Get
        Set(ByVal Value As Integer)
            m_nAge = Value
        End Set
    End Property

    Public Property FirstName() As String
        Get
            Return m_sFirstName
        End Get
        Set(ByVal Value As String)
            m_sFirstName = Value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return m_sLastName
        End Get
        Set(ByVal Value As String)
            m_sLastName = Value
        End Set
    End Property

'Step 2:
    Private Class PersonConverter
        Inherits ExpandableObjectConverter

        Public Overloads Function CanConvertFrom(ByVal context As ITypeDescriptorContext, _
                                                 ByVal t As Type) As Boolean

            If TypeOf (t) Is String Then
                Return True
            End If
            Return MyBase.CanConvertFrom(context, t)
        End Function

'Changed from Overrides to Overloads because VB.net didn't like the former
        Public Overloads Function ConvertFrom(ByVal context As ITypeDescriptorContext, _
                                              ByVal info As System.Globalization.CultureInfo, _
                                              ByVal value As Object) As Object

            Try
                If TypeOf (value) Is String Then
                    Dim s As String = CType(value, String)
                    ' parse the format "Last, First (Age)"
                    '
                    Dim comma As Integer = s.IndexOf(",")
                    If comma <> -1 Then
                        ' now that we have the comma, get
                        ' the last name.
                        Dim last As String = s.Substring(0, comma)
                        Dim paren As Integer = s.LastIndexOf("(")
                        If paren <> -1 And _
                              s.LastIndexOf(")") = s.Length - 1 Then
                            ' pick up the first name
                            Dim first As String = _
                                  s.Substring(comma + 1, paren - comma - 1)
                            ' get the age
                            Dim age As Integer = Int32.Parse(s.Substring(paren + 1, s.Length - paren - 2))
                            Dim p As Person = New Person
                            p.Age = age
                            p.LastName = last.Trim()
                            p.FirstName = first.Trim()
                            Return p
                        End If
                    End If
                End If
            Catch
                ' if we got this far, complain that we
                ' couldn't parse the string
                '
                Throw New ArgumentException("Can not convert '" + CType(value, String) + "' to type Person")

            End Try
            Return MyBase.ConvertFrom(context, info, value)
        End Function

'Changed from Overrides to Overloads because VB.net didn't like the former
        Public Overloads Function ConvertTo(ByVal context As ITypeDescriptorContext, _
                                            ByVal culture As System.Globalization.CultureInfo, _
                                            ByVal value As Object, _
                                            ByVal destType As Type) As Object
            If TypeOf (destType) Is String And TypeOf (value) Is Person Then
                Dim p As Person = CType(value, Person)
                ' simply build the string as "Last, First (Age)"
                Return p.LastName & ", " & p.FirstName & " (" & p.Age.ToString() & ")"
            End If
            Return MyBase.ConvertTo(context, culture, value, destType)
        End Function
    End Class
End Class

'Step 3 (Goes in Web Custom Control class):
    Private p As Person = New Person
    Public Property [Person]() As Person
        Get
            Return p
        End Get
        Set(ByVal Value As Person)
            p = Value
        End Set
    End Property
Avatar of Clif

ASKER

Ok, I found a great C# to VB Translator here:
http://authors.aspalliance.com/aldotnet/examples/translate.aspx

The code produced from the demo provided works (almost).  The properties show up on the properties list as I wanted.

Actually I wasn't too far off in the translation I did.  :)


At any rate, as I said it *almost* works.  Everything is fine except the Web Custom Control doesn't respond to the properties.

Be on the look out for another question from me on this matter.  :)

Thanks.