Percent TypeConverter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ljlevend

    Percent TypeConverter

    Is there a TypeConverter that converts Doubles to percent values in a PropertyGrid? The Windows.Forms.F orm.Opacity property seems to use the TypeConverter that I want.

    Thank you,
    Lance
  • scorpion53061

    #2
    Re: Percent TypeConverter

    View this thread from Jay Harlow and Charles Law. It might help.

    -------------

    Hi Jay

    Thanks for confirming what I was beginning to suspect.

    Using the example that I just used to Armin, I would then have

    <code>
    Public Structure MyValueClass

    Public Function ToPercent() As Double
    End Function

    Public Function ToAbsolute() As Integer
    End Function

    End Structure

    Dim d As Double
    Dim i As Integer

    d = mc.Value.ToPerc ent()
    i = mc.Value.ToAbso lute()
    </code>

    showing that I must explicitly call ToAbsolute rather than just write

    i = mc.Value

    Charles


    "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in
    message
    news:eZoCs0dMEH A.2468@TK2MSFTN GP11.phx.gbl...
    [color=blue]
    > Charles,
    > Neither VB.NET 2002 nor VB.NET 2003 support what you are attempting, per[/color]

    se.
    [color=blue]
    >
    > You could always:
    >
    > Public Structure MyValueClass
    >
    > Public Function ToPercent() As String
    > End Function
    >
    > Public Function ToInteger() As Integer
    > End Function
    >
    > End Structure
    >[/color]
    [color=blue][color=green]
    > > i = mc.Value.ToInte ger()
    > > Textbox1.Text = mc.Value.ToPerc ent()[/color]
    >
    >[/color]
    [color=blue]
    > With VB.NET 2005 (Whidbey) we will be able to override CType, the[/color]

    conversion
    [color=blue]
    > operator, so you will be able to do something like (based on the CTP[/color]

    release
    [color=blue]
    > of Whidbey):
    >
    > Public Structure MyValueClass
    >
    > Public Function ToPercent() As String
    > End Function
    >
    > Public Function ToInteger() As Integer
    > End Function
    >
    > Public Shared Widening Operator CType(value As MyValueClass) As
    > Integer
    > Return value.ToInteger ()
    > End Operator
    >
    > Public Shared Narrowing Operator CType(value As Integer) As
    > MyValueClass
    > Return New MyValueClass(va lue)
    > End Operator
    >
    > End Structure
    >
    > i = mc.Value
    > mc.Value = CType(i, MyValueClass)
    >
    > A Widening CType operator does not require CType, while a Narrowing CType
    > operator does.
    >
    > Hope this helps
    > Jay
    >
    > "Charles Law" <blank@nowhere. com> wrote in message
    > news:eycXnhcMEH A.1192@TK2MSFTN GP11.phx.gbl...[/color]
    [color=blue][color=green]
    > > In a VB.NET app, I can write
    > >
    > > Dim s As String = 3.ToString
    > >
    > > This is because 3 is of type Int32, which is a structure, which has a[/color][/color]
    [color=blue]
    > method[/color]
    [color=blue][color=green]
    > > ToString.
    > >
    > > I would also like to be able to write
    > >
    > > Dim s As String = 3.ToPercent
    > >
    > > by supplying my own implementation of ToPercent. If I could do this,[/color][/color]

    then
    [color=blue]
    > I[/color]
    [color=blue][color=green]
    > > would be able to do what I ultimately wish to do, and that is write
    > > something like the following:
    > >
    > > Dim mc As New MyClass
    > > Dim i As Integer
    > >
    > > i = mc.Value
    > > Textbox1.Text = mc.Value.ToPerc ent
    > >
    > > I have tried to make Value of type MyValueClass, for example, which[/color][/color]
    [color=blue]
    > inherits[/color]
    [color=blue][color=green]
    > > from Int32, but Int32 is not inheritable, so that is a non-starter. In[/color][/color]

    any
    [color=blue][color=green]
    > > case, the first assignment gives a compile time error because there is[/color][/color]

    no
    [color=blue][color=green]
    > > default property of MyValueClass, and I can't set one unless it takes
    > > parameters (which it doesn't).
    > >
    > > Can anyone suggest how this might be done?
    > >
    > > TIA
    > >
    > > Charles
    > >
    > >[/color]
    >[/color]
    [color=blue]
    >[/color]

    "ljlevend" <ljlevend@discu ssions.microsof t.com> wrote in message
    news:545C88E3-41D2-418A-B9D4-F79C720E82AC@mi crosoft.com:[color=blue]
    > Is there a TypeConverter that converts Doubles to percent values in a
    > PropertyGrid? The Windows.Forms.F orm.Opacity property seems to use the
    > TypeConverter that I want.
    >
    > Thank you,
    > Lance[/color]

    Comment

    • ljlevend

      #3
      Re: Percent TypeConverter

      Thanks for the help, but that isn't quite what I'm looking for. I need a TypeConverter class rather than a technique that converts a value. I could create my own TypeConverter class, but I want to make sure that there isn't a standard percent TypeConverter before doing so.

      Lance


      "scorpion53 061" wrote:
      [color=blue]
      > View this thread from Jay Harlow and Charles Law. It might help.
      >
      > -------------
      >
      > Hi Jay
      >
      > Thanks for confirming what I was beginning to suspect.
      >
      > Using the example that I just used to Armin, I would then have
      >
      > <code>
      > Public Structure MyValueClass
      >
      > Public Function ToPercent() As Double
      > End Function
      >
      > Public Function ToAbsolute() As Integer
      > End Function
      >
      > End Structure
      >
      > Dim d As Double
      > Dim i As Integer
      >
      > d = mc.Value.ToPerc ent()
      > i = mc.Value.ToAbso lute()
      > </code>
      >
      > showing that I must explicitly call ToAbsolute rather than just write
      >
      > i = mc.Value
      >
      > Charles
      >
      >
      > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in
      > message
      > news:eZoCs0dMEH A.2468@TK2MSFTN GP11.phx.gbl...
      >[color=green]
      > > Charles,
      > > Neither VB.NET 2002 nor VB.NET 2003 support what you are attempting, per[/color]
      >
      > se.
      >[color=green]
      > >
      > > You could always:
      > >
      > > Public Structure MyValueClass
      > >
      > > Public Function ToPercent() As String
      > > End Function
      > >
      > > Public Function ToInteger() As Integer
      > > End Function
      > >
      > > End Structure
      > >[/color]
      >[color=green][color=darkred]
      > > > i = mc.Value.ToInte ger()
      > > > Textbox1.Text = mc.Value.ToPerc ent()[/color]
      > >
      > >[/color]
      >[color=green]
      > > With VB.NET 2005 (Whidbey) we will be able to override CType, the[/color]
      >
      > conversion
      >[color=green]
      > > operator, so you will be able to do something like (based on the CTP[/color]
      >
      > release
      >[color=green]
      > > of Whidbey):
      > >
      > > Public Structure MyValueClass
      > >
      > > Public Function ToPercent() As String
      > > End Function
      > >
      > > Public Function ToInteger() As Integer
      > > End Function
      > >
      > > Public Shared Widening Operator CType(value As MyValueClass) As
      > > Integer
      > > Return value.ToInteger ()
      > > End Operator
      > >
      > > Public Shared Narrowing Operator CType(value As Integer) As
      > > MyValueClass
      > > Return New MyValueClass(va lue)
      > > End Operator
      > >
      > > End Structure
      > >
      > > i = mc.Value
      > > mc.Value = CType(i, MyValueClass)
      > >
      > > A Widening CType operator does not require CType, while a Narrowing CType
      > > operator does.
      > >
      > > Hope this helps
      > > Jay
      > >
      > > "Charles Law" <blank@nowhere. com> wrote in message
      > > news:eycXnhcMEH A.1192@TK2MSFTN GP11.phx.gbl...[/color]
      >[color=green][color=darkred]
      > > > In a VB.NET app, I can write
      > > >
      > > > Dim s As String = 3.ToString
      > > >
      > > > This is because 3 is of type Int32, which is a structure, which has a[/color][/color]
      >[color=green]
      > > method[/color]
      >[color=green][color=darkred]
      > > > ToString.
      > > >
      > > > I would also like to be able to write
      > > >
      > > > Dim s As String = 3.ToPercent
      > > >
      > > > by supplying my own implementation of ToPercent. If I could do this,[/color][/color]
      >
      > then
      >[color=green]
      > > I[/color]
      >[color=green][color=darkred]
      > > > would be able to do what I ultimately wish to do, and that is write
      > > > something like the following:
      > > >
      > > > Dim mc As New MyClass
      > > > Dim i As Integer
      > > >
      > > > i = mc.Value
      > > > Textbox1.Text = mc.Value.ToPerc ent
      > > >
      > > > I have tried to make Value of type MyValueClass, for example, which[/color][/color]
      >[color=green]
      > > inherits[/color]
      >[color=green][color=darkred]
      > > > from Int32, but Int32 is not inheritable, so that is a non-starter. In[/color][/color]
      >
      > any
      >[color=green][color=darkred]
      > > > case, the first assignment gives a compile time error because there is[/color][/color]
      >
      > no
      >[color=green][color=darkred]
      > > > default property of MyValueClass, and I can't set one unless it takes
      > > > parameters (which it doesn't).
      > > >
      > > > Can anyone suggest how this might be done?
      > > >
      > > > TIA
      > > >
      > > > Charles
      > > >
      > > >[/color]
      > >[/color]
      >[color=green]
      > >[/color]
      >
      > "ljlevend" <ljlevend@discu ssions.microsof t.com> wrote in message
      > news:545C88E3-41D2-418A-B9D4-F79C720E82AC@mi crosoft.com:[color=green]
      > > Is there a TypeConverter that converts Doubles to percent values in a
      > > PropertyGrid? The Windows.Forms.F orm.Opacity property seems to use the
      > > TypeConverter that I want.
      > >
      > > Thank you,
      > > Lance[/color]
      >
      >[/color]

      Comment

      • ljlevend

        #4
        Re: Percent TypeConverter

        Thanks for the help, but that isn't quite what I'm looking for. I need a TypeConverter class rather than a technique for converting the value. I could create my own TypeConverter, but I want to make sure that there isn't a standard TypeConverter for converting percents before doing so.

        Lance


        "scorpion53 061" wrote:
        [color=blue]
        > View this thread from Jay Harlow and Charles Law. It might help.
        >
        > -------------
        >
        > Hi Jay
        >
        > Thanks for confirming what I was beginning to suspect.
        >
        > Using the example that I just used to Armin, I would then have
        >
        > <code>
        > Public Structure MyValueClass
        >
        > Public Function ToPercent() As Double
        > End Function
        >
        > Public Function ToAbsolute() As Integer
        > End Function
        >
        > End Structure
        >
        > Dim d As Double
        > Dim i As Integer
        >
        > d = mc.Value.ToPerc ent()
        > i = mc.Value.ToAbso lute()
        > </code>
        >
        > showing that I must explicitly call ToAbsolute rather than just write
        >
        > i = mc.Value
        >
        > Charles
        >
        >
        > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in
        > message
        > news:eZoCs0dMEH A.2468@TK2MSFTN GP11.phx.gbl...
        >[color=green]
        > > Charles,
        > > Neither VB.NET 2002 nor VB.NET 2003 support what you are attempting, per[/color]
        >
        > se.
        >[color=green]
        > >
        > > You could always:
        > >
        > > Public Structure MyValueClass
        > >
        > > Public Function ToPercent() As String
        > > End Function
        > >
        > > Public Function ToInteger() As Integer
        > > End Function
        > >
        > > End Structure
        > >[/color]
        >[color=green][color=darkred]
        > > > i = mc.Value.ToInte ger()
        > > > Textbox1.Text = mc.Value.ToPerc ent()[/color]
        > >
        > >[/color]
        >[color=green]
        > > With VB.NET 2005 (Whidbey) we will be able to override CType, the[/color]
        >
        > conversion
        >[color=green]
        > > operator, so you will be able to do something like (based on the CTP[/color]
        >
        > release
        >[color=green]
        > > of Whidbey):
        > >
        > > Public Structure MyValueClass
        > >
        > > Public Function ToPercent() As String
        > > End Function
        > >
        > > Public Function ToInteger() As Integer
        > > End Function
        > >
        > > Public Shared Widening Operator CType(value As MyValueClass) As
        > > Integer
        > > Return value.ToInteger ()
        > > End Operator
        > >
        > > Public Shared Narrowing Operator CType(value As Integer) As
        > > MyValueClass
        > > Return New MyValueClass(va lue)
        > > End Operator
        > >
        > > End Structure
        > >
        > > i = mc.Value
        > > mc.Value = CType(i, MyValueClass)
        > >
        > > A Widening CType operator does not require CType, while a Narrowing CType
        > > operator does.
        > >
        > > Hope this helps
        > > Jay
        > >
        > > "Charles Law" <blank@nowhere. com> wrote in message
        > > news:eycXnhcMEH A.1192@TK2MSFTN GP11.phx.gbl...[/color]
        >[color=green][color=darkred]
        > > > In a VB.NET app, I can write
        > > >
        > > > Dim s As String = 3.ToString
        > > >
        > > > This is because 3 is of type Int32, which is a structure, which has a[/color][/color]
        >[color=green]
        > > method[/color]
        >[color=green][color=darkred]
        > > > ToString.
        > > >
        > > > I would also like to be able to write
        > > >
        > > > Dim s As String = 3.ToPercent
        > > >
        > > > by supplying my own implementation of ToPercent. If I could do this,[/color][/color]
        >
        > then
        >[color=green]
        > > I[/color]
        >[color=green][color=darkred]
        > > > would be able to do what I ultimately wish to do, and that is write
        > > > something like the following:
        > > >
        > > > Dim mc As New MyClass
        > > > Dim i As Integer
        > > >
        > > > i = mc.Value
        > > > Textbox1.Text = mc.Value.ToPerc ent
        > > >
        > > > I have tried to make Value of type MyValueClass, for example, which[/color][/color]
        >[color=green]
        > > inherits[/color]
        >[color=green][color=darkred]
        > > > from Int32, but Int32 is not inheritable, so that is a non-starter. In[/color][/color]
        >
        > any
        >[color=green][color=darkred]
        > > > case, the first assignment gives a compile time error because there is[/color][/color]
        >
        > no
        >[color=green][color=darkred]
        > > > default property of MyValueClass, and I can't set one unless it takes
        > > > parameters (which it doesn't).
        > > >
        > > > Can anyone suggest how this might be done?
        > > >
        > > > TIA
        > > >
        > > > Charles
        > > >
        > > >[/color]
        > >[/color]
        >[color=green]
        > >[/color]
        >
        > "ljlevend" <ljlevend@discu ssions.microsof t.com> wrote in message
        > news:545C88E3-41D2-418A-B9D4-F79C720E82AC@mi crosoft.com:[color=green]
        > > Is there a TypeConverter that converts Doubles to percent values in a
        > > PropertyGrid? The Windows.Forms.F orm.Opacity property seems to use the
        > > TypeConverter that I want.
        > >
        > > Thank you,
        > > Lance[/color]
        >
        >[/color]

        Comment

        • Claes Bergefall

          #5
          Re: Percent TypeConverter

          Check out the OpacityConverte r class
          That's what the opacity property uses

          /claes

          "ljlevend" <ljlevend@discu ssions.microsof t.com> wrote in message
          news:545C88E3-41D2-418A-B9D4-F79C720E82AC@mi crosoft.com...[color=blue]
          > Is there a TypeConverter that converts Doubles to percent values in a[/color]
          PropertyGrid? The Windows.Forms.F orm.Opacity property seems to use the
          TypeConverter that I want.[color=blue]
          >
          > Thank you,
          > Lance[/color]


          Comment

          • ljlevend

            #6
            Re: Percent TypeConverter

            That's exactly what I was looking for. Thanks!

            "Claes Bergefall" wrote:
            [color=blue]
            > Check out the OpacityConverte r class
            > That's what the opacity property uses
            >
            > /claes
            >
            > "ljlevend" <ljlevend@discu ssions.microsof t.com> wrote in message
            > news:545C88E3-41D2-418A-B9D4-F79C720E82AC@mi crosoft.com...[color=green]
            > > Is there a TypeConverter that converts Doubles to percent values in a[/color]
            > PropertyGrid? The Windows.Forms.F orm.Opacity property seems to use the
            > TypeConverter that I want.[color=green]
            > >
            > > Thank you,
            > > Lance[/color]
            >
            >
            >[/color]

            Comment

            Working...