Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create an User Control very similar to ComboBox Control. My control is also having a Property 'Items' as String Collection. I am using string collection editor which is working fine. But I don't know, how to use the methods 'Clear', Add, Remove etc.

Can any one provide me the source code for property as collection or atleast guide me.

What I have tried:

I have tried the followings,

VB
Private P1Value As New System.Collections.Specialized.StringCollection

<designerserializationvisibility(designerserializationvisibility.content), category("data"),="" description("items="" in="" the="" combo="" box.")=""> _
    <editor("system.windows.forms.design.stringcollectioneditor, system.design",="" "system.drawing.design.uitypeeditor,="" system.drawing")=""> _
    Public Property Items() As System.Collections.Specialized.StringCollection
        Get
            Return P1Value
        End Get
        Set

        End Set
    End Property
Posted
Updated 31-Jan-21 6:57am
v2
Comments
[no name] 31-Jan-21 12:01pm    
If it's "similar" to a CB, maybe you should inherit from a CB. Better yet, stick with the CB. There's nothing to indicate you need something else.
Member 11700078 1-Feb-21 0:35am    
Dear Gerry Schmitz, Thanks for the suggestion. I have not inherited CB, I wanted to create it from scratch. For learning purpose only. New, Please help me if you can.
My user control is having a property Items() as System.Collections.Specialized.StringCollection. This collection is having methods like Add(), Clear() etc. If I add a string the Count property increases by one, if I use Clear(), count become zero. How my control will come to know that count has changed. Can I add a new Event CountChanged() . Please help me in this. Regards.

1 solution

Quote:
But I don't know, how to use the methods 'Clear', Add, Remove etc.


Can't you use methods exposed by StringCollection[^]?
See:
StringCollection.Add(String) Method (System.Collections.Specialized) | Microsoft Docs[^]
StringCollection.Remove(String) Method (System.Collections.Specialized) | Microsoft Docs[^]

So...
VB
YourUserControlInstance.Items.Add("new Item")
YourUserControlInstance.Items.Remove("old Item")
 
Share this answer
 
v2
Comments
Member 11700078 31-Jan-21 16:55pm    
Thanks a lot Mr. Maciej Los for the guidance.

I have some more doubts.

I have added the user control on a form. In the design mode of the form, the user control is having the property 'Items' in the property grid. on clicking the eclipse the collection editor pops up, I added three lines. During run mode of the Form my user control shows the three lines (Items). I have added on Button on the form, behind the Button I wrote "MyControl.Items.Clear()". this should clear all the three items. but this is not happening. This will not happen because I have not written any code for this to happen. I don't know what to write and where to write. Similarly I tried to add new item, it is not adding. You have suggested "YourUserControlInstance.Items.Add("new Item")" when is mycontrol's instance, in the load event of the user control or at the Form level ? I am confused, please guide me.
Methods of String Collection Like 'Clear()', 'Add()' works automatically ? Please see that the Property is "Public ReadOnly Property Items() As System.Collections.Specialized.StringCollection"
Regards.
Maciej Los 1-Feb-21 3:17am    
I do NOT have an access to your project, so i can't tell you what's wrong with your code.
I'd suggest to read this: Develop Custom Controls - Windows Forms .NET Framework | Microsoft Docs[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900