locked
How to Enumerate special characters (bullets) in a custom control RRS feed

  • Question

  • I am building a custom control to display a bulleted list for a WinForms application and have it functioning as I would like, but would also prefer to have an enumeration for various characters to use as bullets ( •, », ¤, «) for use within the Property declaration to make it easy to choose rather than remember the Atl+Char codes. I have tried declaring as String or Char, but the Error List keeps coming up with "Character is not valid."

    Is it possible to have these characters as an enumeration, or do I just live with having to type the Alt codes. I have set them to • as default in the Sub New() routine as the most commomly used. I have another custom control where the enumeration as plain text works as it should — a text description is most appropriate for that list.

    Any help would be much appreciated.

    Friday, August 25, 2017 7:00 AM

Answers

  • I am building a custom control to display a bulleted list for a WinForms application and have it functioning as I would like, but would also prefer to have an enumeration for various characters to use as bullets ( •, », ¤, «) for use within the Property declaration to make it easy to choose rather than remember the Atl+Char codes.

    That requirement seems like a constant rather than an enumeration. See:
    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/constants-enums/how-to-declare-a-constant

    • Marked as answer by jwp789 Thursday, August 31, 2017 9:45 AM
    Friday, August 25, 2017 8:06 AM
  • I was hoping to be able to place the actual bullet character (•, », ¤, «) in the dropdown list instead of the written description.

    What you are referring to is a custom UITypeEditor.  There is an example here:

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/creating-a-wf-control-design-time-features

    (about 3/4 of the way through) where the custom UITypeEditor will display a square or circle according to the user selection. In that case the graphics is displayed with a custom control,  but I'm not sure that your case will require that, as your 'graphic' is simply a special character.  But the principal is the same - the custom type editor allows you to create the dis0lay for the property editor.   I am sure I have done this in the past, but I can't recall where at the moment - I will try to dig it out.

    To get your account verified, go here: https://social.msdn.microsoft.com/Forums/en-US/home?forum=reportabug and post a request into the most recent "Verify my account' thread.

    • Marked as answer by jwp789 Thursday, August 31, 2017 9:45 AM
    Monday, August 28, 2017 10:20 AM

All replies

  • I am building a custom control to display a bulleted list for a WinForms application and have it functioning as I would like, but would also prefer to have an enumeration for various characters to use as bullets ( •, », ¤, «) for use within the Property declaration to make it easy to choose rather than remember the Atl+Char codes.

    That requirement seems like a constant rather than an enumeration. See:
    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/constants-enums/how-to-declare-a-constant

    • Marked as answer by jwp789 Thursday, August 31, 2017 9:45 AM
    Friday, August 25, 2017 8:06 AM
  •  but would also prefer to have an enumeration for various characters to use as bullets ( •, », ¤, «) for use within the Property declaration to make it easy to choose rather than remember the Atl+Char codes. I have tried declaring as String or Char, but the Error List keeps coming up with "Character is not valid."

    Is it possible to have these characters as an enumeration, or do I just live with having to type the Alt codes. I have set them to • as default in the Sub New() routine as the most commomly used. I have another custom control where the enumeration as plain text works as it should — a text description is most appropriate for that list.

    Any help would be much appreciated.

    Hi jwp789,

    enumerations compile out as sealed classes, inheriting from Enum, with public static fields carrying the name of your enumeration members, thus, you're asking the compiler to name fields things like •, », ¤, «" and that's not accepted.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 25, 2017 9:53 AM
  • Of course it is own preference but for me are enums from the time we used paper tape to program. 

    You can simply use a shared class for that 

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox1.Text = bullets.dot
        End Sub
        Public Class bullets
            Public Shared ReadOnly Property dot As Char = "•"c
            Public Shared ReadOnly Property square As Char = "¤"c
        End Class
    End Class
    


    Success
    Cor

    Friday, August 25, 2017 1:53 PM
  • To use an enum with descriptions use the following:

    Enum with descriptions:

    Public Enum FileView
        <Description("Details")>
        Details
        <Description("List")>
        List
        <Description("Small Icons")>
        SmallIcons
        <Description("Large Icons")>
        LargeIcons
        <Description("X-Large Icons")>
        XLargeIcons
    End Enum
    

    Get Description from Enum:

        Public Function GetEnumDescription(ByVal EnumConstant As [Enum]) As String
            Dim fi As FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
            Dim attr() As DescriptionAttribute = DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
    
            If attr.Length > 0 Then
                Return attr(0).Description
            Else
                Return EnumConstant.ToString()
            End If
        End Function
    

    In the enum you can set the value (it should be the value of the key code for the bullet you want to use.

    Public Enum InterfaceColors
        MistyRose = &HE1E4FF&
        SlateGray = &H908070&
        DodgerBlue = &HFF901E&
        DeepSkyBlue = &HFFBF00&
        SpringGreen = &H7FFF00&
        ForestGreen = &H228B22&
        Goldenrod = &H20A5DA&
        Firebrick = &H2222B2&
    End Enum


    Lloyd Sheen

    Friday, August 25, 2017 2:31 PM
  • Hi Cherry

    I have tried declaring the characters as constants, with an error message re casting a string to an integer.

    I did think I had come up against a limitation on how I "wished" enumerations would work, and maybe i can put a Select Case statement in the property's Get section and assign the character from their based on the choice at design time. This is probably the simplest work around for me, as I am an amateur creating some computer based learning programs and not a proper programmer.

    Thanks for the clarification.

    Hi RoshanPriyaKumar

    My custom label draws text with a shadow for better readability/presentation and special borders, and I have been unable to work out to do this with RTF boxes, which seems way too complicated for my level of expertise., but thanks for the link (which I had already seen on prior searches).

    Hi Cor

    My label is its own class, and I am unsure about declaring a class-within-a-class.

    Thanks for the suggestion.

    Hi Lloyd Sheen

    This looks interesting, and I will give it a try, but the complexity (from my limited expertise) looks a little daunting, but I love to try new ideas and see if I can make it work for me.

    Thanks for the suggestion.
    Saturday, August 26, 2017 3:45 AM
  • I have tried declaring the characters as constants, with an error message re casting a string to an integer.

    It's not clear where an integer would come into it - the constant would be a string or a character and would be used as a string.    Or are you trying to refer to your bullets using an index number?

    My label is its own class, and I am unsure about declaring a class-within-a-class.

    Are you referring to defining the class or declaring the class instance?   A shared class would typically be defined in a module, and does not need a variable for the instance.
    https://msdn.microsoft.com/en-us/library/4hbsxy95%28v=vs.90%29.aspx

    Saturday, August 26, 2017 6:29 AM
  • G'day Lloyd Sheen

    I am trying out your suggestion/s - do they all work together to provide my solution, or is it 3 alternatives for the same thing?

    With the second part — the GetEnumDescription function — I get an error message:

    "Type 'FieldInfo' is not defined."

    Do I need an Import statement to access another namespace containing this field?

    Regards

    John


    Saturday, August 26, 2017 6:29 AM


  • Hi Cor

    My label is its own class, and I am unsure about declaring a class-within-a-class.

    Thanks for the suggestion.

    Why don't you try first the simple things before you start typing long parts of code

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim myLabel As New MyUserControl
            Controls.Add(myLabel)
            myLabel.Text = MyUserControl.bullets.dot
        End Sub
        Public Class MyUserControl
            Inherits Label
            Public Class bullets
                Public Shared ReadOnly Property dot As Char = "•"c
                Public Shared ReadOnly Property square As Char = "¤"c
            End Class
        End Class
    End Class


    Success
    Cor


    Saturday, August 26, 2017 7:29 AM
  • All are part of the same solution.  

    The import is : 

    Imports System.Reflection


    Lloyd Sheen

    Saturday, August 26, 2017 11:18 AM
  • Hi Roshan

    I am building a custom control I will use in many places as a substitute for a Label control and overriding the OnPaint method. As such, it does not have a load event, and I need to be able to place it in different locations on several forms.

    Thanks

    Sunday, August 27, 2017 7:12 AM
  • G'day LLoyd

    Still working on it. The Import removes the error message, but I cannot se the characters after rebuilding the solution to reflect the changes. I will keep at it.

    Thanks

    Sunday, August 27, 2017 7:14 AM
  • Hi Roshan

    I am building a custom control I will use in many places as a substitute for a Label control and overriding the OnPaint method. As such, it does not have a load event, and I need to be able to place it in different locations on several forms.

    Thanks

    Are you so stupid or do you want us to think you are. If somebody like me put something in the load event than it is only that it is the quickest way to make a sample. Be aware that Rhoshan did only copy my code, so that is the same like mine.

    Success
    Cor

    Sunday, August 27, 2017 4:26 PM
  • Still working on it. The Import removes the error message, but I cannot se the characters after rebuilding the solution to reflect the changes. I will keep at it.

    I think that you are making this much more complicated than it needs to be.   You should show the code you are currently using, or some pseudo code that demonstrates how you expect to be able to use these bullets in the custom control code, because that will indicate the best way to construct what you need to make that code work. 

    Sunday, August 27, 2017 10:53 PM
  • G'day Cor, and thank you for giving me my first experience of being trolled. I must say I do not like it. I am sorry if I misunderstood your post, but when I have asked for help previously and code was posted, this was meant to be copied and pasted  and tested/modified to suit my needs, so my bad for not realising yours was by way of a fast sample!

    As for thanking Roshan (who did acknowledge your post) for your suggestion, my apologies again for going straight to the replies and not reading every post from the beginning and double checking who posted what first, although after looking back, I thought you should be pleased that someone else thought your suggestion was worth pushing to me again as a good proposal. Again, I was trying to make sure I thanked everyone for their efforts in helping me, and I apologise for incorrectly attributing your suggestion to someone else.

    Regards

    John

    Monday, August 28, 2017 9:33 AM
  • Hi Acamar

    Following is a sample of the output from my custom control. It draws exactly the way I want it to, but I was looking for a way to enter the bullet character more easily at design time.

    Cannot post the screenshot yet as my account has not yet been verified. Sorry

    No screen grab of the enumeration I am using at present for the same reason, but it would have looked similar to:

    BulletShape     Rounded

                            Chevron_Right

                            Splat

                            Chevron_LeftI was hoping to be able to place the actual bullet character (•, », ¤, «) in the dropdown list instead of the written description.

    I have decided to abandon that idea and change things to use following Property code:

    =========Code Snippet==============

          <Description("Bullet character FOR ALL BULLETS. Alt+ codes:" & vbCrLf & "• = Alt+ 0149, » = 0187, ¤ = 164, « = 0171.")> _
        <Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design", GetType(UITypeEditor))> _
      Public Property BulletShape As BulletChar
            Get     ' choose the 3D Border Effect style
                Return m_bulletshape
                Select Case m_bulletshape
                    Case 1                              ' •
                        m_bulletToUse = "•"
                    Case BulletChar.Chevron_Right       ' »
                        m_bulletToUse = "»"
                    Case BulletChar.Splat               ' ¤
                        m_bulletToUse = "¤"
                    Case BulletChar.Chevron_Left        ' «
                        m_bulletToUse = "«"
                End Select
            End Get
            Set(ByVal value As BulletChar)
                m_bulletshape = value
                Invalidate()
            End Set
        End Property
    ================================

    Apologies for not using the correct method of inserting the snippet, as putting my code up is another first, and I have not had time to go through those learning steps yet — I did not expect to ever post my code!

    Thanks again to everyone who has taken time to offer help and the many suggestions whish I will look at more closely to see where I can add them into mu own little knowledge base.

    Monday, August 28, 2017 9:53 AM
  • I was hoping to be able to place the actual bullet character (•, », ¤, «) in the dropdown list instead of the written description.

    What you are referring to is a custom UITypeEditor.  There is an example here:

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/creating-a-wf-control-design-time-features

    (about 3/4 of the way through) where the custom UITypeEditor will display a square or circle according to the user selection. In that case the graphics is displayed with a custom control,  but I'm not sure that your case will require that, as your 'graphic' is simply a special character.  But the principal is the same - the custom type editor allows you to create the dis0lay for the property editor.   I am sure I have done this in the past, but I can't recall where at the moment - I will try to dig it out.

    To get your account verified, go here: https://social.msdn.microsoft.com/Forums/en-US/home?forum=reportabug and post a request into the most recent "Verify my account' thread.

    • Marked as answer by jwp789 Thursday, August 31, 2017 9:45 AM
    Monday, August 28, 2017 10:20 AM
  • Hi,

    You Could Try This

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Label1.Text = "¤"
        End Sub
    End Class

    Thanks,

    Monday, August 28, 2017 10:58 AM
  • G'day again Acamar

    1st, thanks for the suggestion

    2nd, Whew!! that seems like a lot of work just so I can avoid typing Alt-0149 to place a bullet. Even more so since I have a workaround within the Property Get section.

    I think this has reached the stage of requiring far more effort to implement than I would save if I could get it running.

    I will mark as Answer:
    • Cherry's post for explaining that I was asking for something not normally done — it finally got through to me that "Enumeration" meant get the number in the list and not the text!
    • Your post, even though I will not try to implement it because even though it would do what I ask, I am a little too daunted to try and implement a custom UI editor and be confident it is bug free to issue to others.

    Thank you to everyone for your help.

     

    Thursday, August 31, 2017 9:45 AM