Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > custom listbox


Reply
 
Thread Tools Display Modes
  #1  
Old 07-15-2005, 02:40 AM
stones75 stones75 is offline
Newcomer
 
Join Date: Aug 2004
Posts: 13
Default custom listbox


Hi,

I'm trying to have a listbox with text and icons. My listbox isn't displaying anything because it's not going through the OnDrawItem procedure but I don't know how to make it do that. Thanks for any help.

This is what I have in my onload procedure ( have a listbox called lstText):

Dim lstText As CustomListBox = New CustomListBox
lstText.Items.Add(New CustomListBox("Image 1", New Icon("../Images/Icon16.ico")))
lstText.Items.Add(New CustomListBox("Image 2", New Icon("../Images/Icon16.ico")))



This is my CustomListBox class:

Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Reflection
Imports System.Resources
Imports System.Windows.Forms


Public Class CustomListBox
Inherits ListBox

Private m_Icon As Icon

Public Sub New()
MyClass.New("", Nothing)
End Sub

Public Sub New(ByVal text As String, ByVal Icon As Icon)
Me.DrawMode = DrawMode.OwnerDrawFixed
m_Icon = Icon
End Sub

Private Function GetText() As String
Dim s As String
s = Text
return s
End Function

Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
Dim item As String
Dim br As Brush

item = GetText()
'Draw the icon
If Not m_Icon Is Nothing Then
e.Graphics.DrawIcon(m_Icon, e.Bounds.Left + 2, e.Bounds.Top + 2)
End If

'Draw the item
e.Graphics.DrawString(item, e.Font, New SolidBrush(e.ForeColor), e.Bounds.Left + 25, e.Bounds.Top + 2)

MyBase.OnDrawItem(e)
End Sub


Protected Overrides Sub OnMeasureItem(ByVal e As MeasureItemEventArgs)
e.ItemWidth = 300
e.ItemHeight = 100
End Sub


End Class

Last edited by stones75; 07-15-2005 at 06:01 AM.
Reply With Quote
  #2  
Old 07-15-2005, 07:27 AM
shiba126 shiba126 is offline
Centurion
 
Join Date: Jul 2003
Posts: 127
Default

Have you ever thought about using the Listview control? You can add icons etc into it. You can also change the view, whether it be in columns, large icons, small icons or list. I think there are a few more, but ah well.

Chris
__________________
Posting Guidelines
Reply With Quote
  #3  
Old 07-15-2005, 08:08 AM
stones75 stones75 is offline
Newcomer
 
Join Date: Aug 2004
Posts: 13
Default

Quote:
Originally Posted by shiba126
Have you ever thought about using the Listview control? You can add icons etc into it. You can also change the view, whether it be in columns, large icons, small icons or list. I think there are a few more, but ah well.

Chris
Well I have other reasons for wanting to use a listbox
Reply With Quote
  #4  
Old 07-16-2005, 01:51 PM
shiba126 shiba126 is offline
Centurion
 
Join Date: Jul 2003
Posts: 127
Default

And these are....
__________________
Posting Guidelines
Reply With Quote
  #5  
Old 07-18-2005, 07:32 AM
stones75 stones75 is offline
Newcomer
 
Join Date: Aug 2004
Posts: 13
Default

Quote:
Originally Posted by shiba126
And these are....

Dosen't matter.
Reply With Quote
  #6  
Old 07-18-2005, 07:46 AM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

attitude...

Is it going through the "OnMeasureItem". Have you set the OwnerDraw property of the listbox. The DrawItem and MeasureItem events will not fire unless this is set.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #7  
Old 07-19-2005, 09:38 AM
shiba126 shiba126 is offline
Centurion
 
Join Date: Jul 2003
Posts: 127
Default

Quote:
Originally Posted by stones75
Dosen't matter.

Then how can you expect help........

I think Iceplug was right - attitude
__________________
Posting Guidelines
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->