Question CheckedListBox in PropGrid

jackgreat

Active member
Joined
Apr 17, 2006
Messages
35
Programming Experience
1-3
Hi Guys,

I am using a propertygrid in vb.net (2008). I am able to populate it and save & get its values. So far so good.

I wanted to know how can I impliment a checked list box in propertygrid. (Shown in Image). Basically i have for example - first name, middle name and last name in property grid. I want to ask him which one he want to show or all. So in this way (checkedlistbox) he can select which one or all. To achieve something like this I guess a checkedlistbox need to be there in propgrid.


Will be glad if anyone can tell me how to do that?

Thanks,

Cheers,
JG
 

Attachments

  • propgrid.jpg
    propgrid.jpg
    87.7 KB · Views: 62
a little help

You could define a flags Enum with the options, a property of this type, and write a UITypeEditor class for it. Here's an existing one Rory Primrose | Bitwise/Flags Enum UITypeEditor

Thanks for the input dude. Though I got the code for UITypeEditor in the link you provided but how do I use it.

Can you provide me a small code or something which shows propertygrid being populated the way we want. I kinda got stuck here. :confused:
 
From help index "flags enumerations": Designing Flags Enumerations
From help for "property statement": Property Statement

Can you read these two topics, then write a flags enum for your options and property to select them? This is pretty basic stuff. You can also get some help from the IDE; write "prop" and press Tab key twice to let the IDE insert the standard Property code snippet where you just "fill in the blanks".

Then you have to apply the Editor atttribute to the property to select your special UITypeEditor. These two help topic will give you relevant information:

From help for "EditorAttribute class": EditorAttribute Class (System.ComponentModel)
From help for "UITypeEditor class": UITypeEditor Class (System.Drawing.Design)

The latter has a code sample where you can see they apply the Editor attribute to a property (the Angle property of AngleEditorTestControl class).

Give it a go and ask again if you're stuck.
 
thanks but still need clarifications

Thanks a lot for your help John. I really appreciate.

Well I was already aware of Enum and Property keywords and I have used them before to populate propertygrid. What is actually causing issues it the UITypeEditor and EditorAttribute. I am unable to get a hang of them.

Basically I was able to populate propertygrid with couple of values string, number and boolean. Infact I came across a class available on the web with the name PropertyGridEx which help you to add and manipulate propertygrid easily and dynamically.

A code for example -

VB.NET:
PropGrid.Properties.Add(New PropertySpec("Age", "System.Single", "Basic Parameters", "Age for Client"))

The above code helped me create a row in propertygrid which takes an input age and its numeric. This way I have been able to add string, boolean & number.

I tried adding checkedlistbox but I failed.

All I need is code to add a row whereby a checked list box as we have been talking about appears.

If you can write a short line or provide me with a small sample it will be fantastic.

Thanks a lot buddy, :)

Jack
 
What is actually causing issues it the UITypeEditor and EditorAttribute. I am unable to get a hang of them.
It is just an attribute to add to the property, same as the Flags attribute. Did you also see the code sample I referred to? Here is it copied in case you couldn't find it:
<BrowsableAttribute(True), EditorAttribute(GetType(AngleEditor), GetType(System.Drawing.Design.UITypeEditor))> _
Public Property Angle() As Double
Can you see that "EnumEditor" is your "AngleEditor" ?
Infact I came across a class available on the web with the name PropertyGridEx
I don't know the classes you mention. The regular PropertyGrid control is used to display the properties of one or more instances of a defined class. If your control can handle dynamic properties where user defined types and editors can be specified, then it would be possible. If it was I wouldn't be surprised if it was done similar to this:
VB.NET:
Dim spec As New PropertySpec("NameParts", GetType(NameParts), "Appearance", "Sets the name parts.", NameParts.First, GetType(FlagsEditor), GetType(EnumConverter))
(ok, I found a PropertySpec class on the web that worked like this. :p)
 
unable to progress ...?

Thanks for help dude but unfortunately I am still in a spot of bother.

Do you think I can use PropertyGridEx to solve my problems of checkedlistbox.

VB.NET:
Dim spec As New PropertySpec("NameParts", GetType(NameParts), "Appearance", "Sets the name parts.", NameParts.First, GetType(FlagsEditor), GetType(EnumConverter))

[B]What is flageditor & nameparts [/B]?

If you can give me a working sample it will be great. Thanks a lot.
 
"FlagsEditor" is the UITypeEditor, if you copied the editor from the link I posted then yours is called "EnumEditor".

"NameParts" is your enum that has the name parts options.
Do you think I can use PropertyGridEx to solve my problems of checkedlistbox.
As I said, this class unknown to me. The regular PropertyGrid will display a CheckedListBox if you apply the Editor atribute to a flags enum property, specifying a flags editor such as the EnumEditor.
 
"FlagsEditor" is the UITypeEditor, if you copied the editor from the link I posted then yours is called "EnumEditor".

"NameParts" is your enum that has the name parts options.

As I said, this class unknown to me. The regular PropertyGrid will display a CheckedListBox if you apply the Editor atribute to a flags enum property, specifying a flags editor such as the EnumEditor.

I tried using the code -

VB.NET:
Dim spec As New PropertySpec("NameParts", GetType(NameParts), "Appearance", "Sets the name parts.", NameParts.First, GetType(FlagsEditor), GetType(EnumConverter))

- Cont.. Properly this time by declaring nameparts as enum and using Enum converted and enum editor . I was kinda hopeful but it didnt work. In their class in default value stage an error arose (obj ref not set ..error).

Can you give me a sample code which does using the old fashioned way (fixed class). I have not been able to move ahead.

Thanks.
 
The same class I tested had same error; (a) rewrite the class to fix the error, or (b) don't set a default value, or (c) set a value for the property before attaching to grid (or use some event to provide a value for prop).

Can you give me a sample code which does using the old fashioned way (fixed class). I have not been able to move ahead.
That's back to post 4, post what you have so far, and I'll take a look. The task is quite elementary and you have indicated that you already know how to write properties and enums, applying the attribute is also quite easy with the information you have been provided so far, so I wonder what the problem is.
 
going on...

I tried working with the whole thing but there are two fundamental problems that I am facing.

- The items in checkedlistbox will change dynamically. So even if the code works, I will need to use Enum and thus things will become kinda static (hardcoded). How do I tackle that?

- I wrote -

VB.NET:
 P.Properties.Add(New PropertySpec("Test", GetType(Ind), "C", "D", "", GetType(EnumEditor), GetType(EnumConverter)))

Here Ind is my Enum. But inspite of running the code I see a listbox and not checkedlistbox. Somehow the control is not going into the EnumEditor Class.

(I was able to fix the error in default property).

Thus as I mentioned two issues stops me, dynamic adding of elements (enum does not enable me to do that) and control not going in enumeditor so listbox appears rather than checked one.

If you can assist me it will be great.

Thanks a lot for your cooperation so far. I really appreciate it John. :)
 
But inspite of running the code I see a listbox and not checkedlistbox.
EnumEditor displays a Listbox for regular enums and a CheckedListBox for flags enums. I updated the link for help to flags enum above, a newer document has a VB.Net code sample you can copy.
- The items in checkedlistbox will change dynamically.
What a bomb, why doesn't this surprise me? :rolleyes: Which basically means you can forget about using an enum, and brings up support rule #1 "present the problem properly", this is such a fundamental requirement that if you had explained this in your first post I would never even have thought about suggesting to use an enum, and neither of us would have wasted all this time.

So what is it you are trying to do, now having no defined type, no defined properties, and no defined options for your undefined properties? One can only wonder...

With the all new requirements I will propose a new possible solution, your property now needs both to be able to add values and maintain the checked state for each, this is typically a list of a custom type (that has these two properties; value, checked) or a dictionary that has a key/value pair. For the latter define your property as type Dictionary(Of String, Boolean) for starters. Apply a UITypeEditor that pass these options and check state between the dictionary and the CheckedListBox. Since this is way over your knowledge level I will provide a "OptionsEditor" you can use:
VB.NET:
Imports System.Windows.Forms.Design
Imports System.Drawing.Design
Imports System.ComponentModel
Public Class OptionsEditor
    Inherits 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
        ' Check if the required objects exist
        If context Is Nothing OrElse context.Instance Is Nothing OrElse provider Is Nothing Then Return value
        Dim editorService As IWindowsFormsEditorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
        If editorService Is Nothing Then Return value
        'create the checklist
        Dim chkList As New CheckedListBox
        Dim options As Dictionary(Of String, Boolean) = CType(value, Dictionary(Of String, Boolean))
        For Each key As String In options.Keys
            chkList.Items.Add(key, options(key))
        Next
        With chkList
            .BorderStyle = BorderStyle.None
            .CheckOnClick = True
            If .Height > (.Items.Count * .ItemHeight) Then
                ' Adjust the height of the list
                .Height = .Items.Count * .ItemHeight
            End If
        End With
        ' Display the drop down list
        editorService.DropDownControl(chkList)

        'get checked states
        For i As Integer = 0 To chkList.Items.Count - 1
            Dim key As String = CStr(chkList.Items(i))
            options(key) = chkList.GetItemChecked(i)
        Next
        chkList.Dispose()

        Return value
    End Function
End Class
 
oops

Thanks for your support John. I know you are pissed off because I did not mention that the items in the checkedlistbox would change dynamically.

See when I started this, I primarily wanted to see a checkedlistbox and I thought since items in the propgrid are being added dynamically, I would also be able to add items to that chklistbox dynamically. But because I was not able to get that to work in the first place, this thing didnt strike me. After all I was trying to get this to work but inspite of spending two days or more I am still struggling.

See I am aware of all intermediate stuff in .net and my programming is sound but I accept .net is vast and topics like UIEditor etc is new to me.

Well to the main topic again, I wanted to add items to the propertygrid dynamically and I came across a class which I told you about (propertybag). Using that class I was able to do so and most of my requirements was taken care of. Issues started just for checkedlistbox stuff as I was not able to do that.

Now I tried a lot but couldnt get things to work, even the class (whose link you gave) was tried but somehow propertybag does not call it or so thus checkedlistbox never came and all I could see was a combo/listbox.

Right, now I know you have given me code that enabled me to add items dynamically to checkedlistbox but I am still in a spot of bother.
Propertybag is the only class I know that makes it easy to add items to propertygrid dynamically and it isnt somehow not helping my cause. Therefore your code maynot work even with this bag and I will still be stuck.

I know you have help me a lot and I would be grateful if you can write a small code that does what I want. This would finish the nightmare once & for all.

The url for propertybag: CodeProject: Bending the .NET PropertyGrid to Your Will. Free source code and programming help

Believe me I tried a lot but couldnt get this thing to work.

I also saw some samples whereby if a user click on combobox then a form in displayed and that has checkedlistbox with values populated. If thats also possible I would be happy.

Thanks a lot.
 
somehow propertybag does not call it or so thus checkedlistbox never came and all I could see was a combo/listbox.
It is called, but as I said you haven't defined a flags enum yet.
if you can write a small code that does what I want
Yes, I can see now that in this case you wouldn't find the TypeConverter, actually it is not needed for when using the default. I will give a working sample with the PropertyTable of the article you linked in previous post, it's easier to manage.
VB.NET:
Dim options As New Dictionary(Of String, Boolean)
options.Add("A", True)
options.Add("B", False)
options.Add("C", False)

Dim spec As New PropertySpec("Options", options.GetType, "Appearance", "Sets the options.", Nothing, GetType(OptionsEditor), "")

Dim table As New PropertyTable
table.Properties.Add(spec)
table("Options") = options
Me.PropertyGrid1.SelectedObject = table
 
oh

I think there is some problem with class propertybag. Inspite of writing the code as you showed and making sure everything is okay, the control never goes to the class optionseditor. And thus the checkedlistbox never gets displayed.

Can you check propertybag class and make necessary changes. This will ensure that things work as we want and we will finally be thru with this.
 
Back
Top