[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:
<Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", GetType(Drawing.Design.UITypeEditor)),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
ReadOnly Property Items As System.Windows.Forms.ListBox.ObjectCollection
Get
Return DirectCast(lb.Items, ListBox.ObjectCollection)
End Get
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?
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
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?
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:
Imports System.ComponentModel
Public Class SpecialList
Public Sub New()
' This call is required by the designer.
InitializeComponent()
Panel1.Dock = DockStyle.Fill
' Add any initialization after the InitializeComponent() call.
End Sub
<Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", GetType(Drawing.Design.UITypeEditor)),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
ReadOnly Property Items As System.Windows.Forms.ListBox.ObjectCollection
Get
Return DirectCast(ListBox1.Items, ListBox.ObjectCollection)
End Get
End Property
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 ?
Re: How would one get items to appear in a ListBox contained in a UserControl?
Quote:
Originally Posted by
Troy Lundin
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...:confused:
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:
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.
Quote:
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?
Re: How would one get items to appear in a ListBox contained in a UserControl?
Quote:
Originally Posted by
Troy Lundin
I am setting these styles in the UserControl:
vb.net Code:
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.
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. :D
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.