Results 1 to 9 of 9

Thread: [RESOLVED] How would one get items to appear in a ListBox contained in a UserControl?

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Resolved [RESOLVED] How would one get items to appear in a ListBox contained in a UserControl?

    I created a UserControl. This UserControl contains a Panel which contains a ListBox. This is so I can draw a custom border for the ListBox. I am exposing the ListBox's Items property like so:

    vb.net Code:
    1. <Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", GetType(Drawing.Design.UITypeEditor)),
    2.     DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    3.     ReadOnly Property Items As System.Windows.Forms.ListBox.ObjectCollection
    4.         Get
    5.             Return DirectCast(lb.Items, ListBox.ObjectCollection)
    6.         End Get
    7.     End Property

    This is how it is declared in Reflector. However, whenever I edit the collection using the designer's property editor, the items do not show up in the ListBox. Please note that I am able to add items and make them appear properly at runtime.

    How would I get the items to appear in the ListBox at design time?
    Prefix has no suffix, but suffix has a prefix.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How would one get items to appear in a ListBox contained in a UserControl?

    if you don't need a custom editor for your items, you can just expose the entire listbox via a property:

    Code:
    Imports System.ComponentModel
    
    Public Class UserControl1
    
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public ReadOnly Property embeddedListBox() As ListBox
            Get
                Return ListBox1
            End Get
        End Property
    
    End Class

  3. #3

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: How would one get items to appear in a ListBox contained in a UserControl?

    That's not quite the issue I have. Let me use some bullets.

    - If I add items to the UserControl at runtime, using myUserControl.Items.Add, the items will display in the ListBox properly.
    - If I add items to the UserControl at design-time, using the property grid of the designer, the items will display in the ListBox properly at runtime.
    - If I add items to the UserControl at design-time, using the property grid of the designer, the items will not display in the ListBox at all in the designer.

    Though the items are properly in the Items property, the do not display in the ListBox in the designer. Is that a bit clearer?
    Prefix has no suffix, but suffix has a prefix.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: How would one get items to appear in a ListBox contained in a UserControl?

    I tried reproducing your problem using this UserControl:-
    vbnet Code:
    1. Imports System.ComponentModel
    2.  
    3. Public Class SpecialList
    4.  
    5.     Public Sub New()
    6.  
    7.         ' This call is required by the designer.
    8.         InitializeComponent()
    9.  
    10.         Panel1.Dock = DockStyle.Fill
    11.         ' Add any initialization after the InitializeComponent() call.
    12.     End Sub
    13.     <Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", GetType(Drawing.Design.UITypeEditor)),
    14.     DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    15.     ReadOnly Property Items As System.Windows.Forms.ListBox.ObjectCollection
    16.         Get
    17.             Return DirectCast(ListBox1.Items, ListBox.ObjectCollection)
    18.         End Get
    19.     End Property
    20.  
    21. End Class

    It works fine for me. Items added at design-time show up at design time. Is there any notable difference between the code above and your UserControl ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How would one get items to appear in a ListBox contained in a UserControl?

    Quote Originally Posted by Troy Lundin View Post
    That's not quite the issue I have. Let me use some bullets.

    - If I add items to the UserControl at runtime, using myUserControl.Items.Add, the items will display in the ListBox properly.
    - If I add items to the UserControl at design-time, using the property grid of the designer, the items will display in the ListBox properly at runtime.
    - If I add items to the UserControl at design-time, using the property grid of the designer, the items will not display in the ListBox at all in the designer.

    Though the items are properly in the Items property, the do not display in the ListBox in the designer. Is that a bit clearer?
    I understood your question the first time + gave you a working solution...

  6. #6

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: How would one get items to appear in a ListBox contained in a UserControl?

    Honestly, I have no idea what's wrong then.

    I am setting these styles in the UserControl:
    vb.net Code:
    1. SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)

    I changed the BackColor of the ListBox and when I add items in the Designer I can see them being added to the ListBox, but only the item's BackColor is being drawn. The ForeColor isn't. I am making use of the DrawItem event of the ListBox, if that matters.

    I understood your question the first time + gave you a working solution...
    Exposing the ListBox is a bit overkill. Though, after exposing it and setting it's items in the designer, I get the same result as above: BackColor without ForeColor.

    The weirdest part is it works at runtime. Does DrawItem not get called at all at design time?
    Prefix has no suffix, but suffix has a prefix.

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: How would one get items to appear in a ListBox contained in a UserControl?

    Quote Originally Posted by Troy Lundin View Post
    I am setting these styles in the UserControl:
    vb.net Code:
    1. SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
    Try it without setting the styles of either the UserControl or the ListBox.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: How would one get items to appear in a ListBox contained in a UserControl?

    Got rid of the styles and no changes.

    Edit: I figured it out. It was totally an oversight. In my DrawItem event for the ListBox, I was evaluating a property that was Nothing until runtime, so it was preventing the strings from being written until runtime. That's embarrassing. Thanks for the help.
    Last edited by Troy Lundin; Aug 30th, 2013 at 09:00 AM. Reason: Derp Moment Discovered
    Prefix has no suffix, but suffix has a prefix.

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: [RESOLVED] How would one get items to appear in a ListBox contained in a UserCont

    Why is it that you guys always leave out such critical information when asking a question. You said nothing about overriding DrawItem.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width