Windows Develop Bookmark and Share   
 index > Windows Forms General > Listbox with icons
 

Listbox with icons

Hi,

I am willing to have an icon will will display infront of each listbox item so that the users can distinguish between "groups" and "users". So basically I am willing 2 add a listbox with listboxitems each of which has either the groups icons or the users icon. Unfortunately, I couldn't find any useful article or sample fully functional code online. Can anyone help by giving an example?. I found this example but it didnt work out w/me http://www.codeproject.com/cs/combobox/glistbox.asp?print=true

THANKS

perpetual_dream  Tuesday, July 03, 2007 12:51 PM
Set the ListBox' DrawMode property to OwnerDrawFixed/Variable so you can do your own drawing and paint both the icon and the text. There's a good example in the MSDN library topic.
nobugz  Tuesday, July 03, 2007 1:22 PM
Use Graphics.DrawIcon(). I can't think of an interesting example.
nobugz  Wednesday, July 04, 2007 10:47 AM

Thank u. I found this on http://homepage1.nifty.com/rucio/main/dotnet/Samples/Sample112ListBoxWithIcon.htm

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

ListBox1.DrawMode = DrawMode.OwnerDrawFixed
ListBox1.ItemHeight = 16

End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

'▼項目がない場合は何もしない

If e.Index = -1 Then
Exit Sub
End If

'▼アイコンとブラシの用意

Dim myBrush As Brush = New SolidBrush(ListBox1.ForeColor)
Dim Icon As Drawing.Icon

'描画するアイコンを3つごとに適当に決?br> '実際にはスピード向上のためアイコンを先に読み込んでおくことが望ましい?br> Select Case e.Index Mod 3
Case 0
Icon =
New Drawing.Icon(Application.StartupPath & "\Icon1.ico")
Case 1
Icon =
New Drawing.Icon(Application.StartupPath & "\Icon2.ico")
Case 2
Icon =
New Drawing.Icon(Application.StartupPath & "\Icon3.ico")
End
Select

'▼描画実?/p>

e.DrawBackground()
e.Graphics.DrawIcon(Icon,
New Rectangle(e.Bounds.X, e.Bounds.Y, 16, 16))
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, myBrush,
New RectangleF(e.Bounds.X + 16, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()

End Sub

perpetual_dream  Saturday, July 07, 2007 6:54 AM
Set the ListBox' DrawMode property to OwnerDrawFixed/Variable so you can do your own drawing and paint both the icon and the text. There's a good example in the MSDN library topic.
nobugz  Tuesday, July 03, 2007 1:22 PM
Would u mind showing me a clean and clear example to draw icons?
perpetual_dream  Wednesday, July 04, 2007 10:18 AM
Use Graphics.DrawIcon(). I can't think of an interesting example.
nobugz  Wednesday, July 04, 2007 10:47 AM

Thank u. I found this on http://homepage1.nifty.com/rucio/main/dotnet/Samples/Sample112ListBoxWithIcon.htm

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

ListBox1.DrawMode = DrawMode.OwnerDrawFixed
ListBox1.ItemHeight = 16

End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

'▼項目がない場合は何もしない

If e.Index = -1 Then
Exit Sub
End If

'▼アイコンとブラシの用意

Dim myBrush As Brush = New SolidBrush(ListBox1.ForeColor)
Dim Icon As Drawing.Icon

'描画するアイコンを3つごとに適当に決?br> '実際にはスピード向上のためアイコンを先に読み込んでおくことが望ましい?br> Select Case e.Index Mod 3
Case 0
Icon =
New Drawing.Icon(Application.StartupPath & "\Icon1.ico")
Case 1
Icon =
New Drawing.Icon(Application.StartupPath & "\Icon2.ico")
Case 2
Icon =
New Drawing.Icon(Application.StartupPath & "\Icon3.ico")
End
Select

'▼描画実?/p>

e.DrawBackground()
e.Graphics.DrawIcon(Icon,
New Rectangle(e.Bounds.X, e.Bounds.Y, 16, 16))
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, myBrush,
New RectangleF(e.Bounds.X + 16, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()

End Sub

perpetual_dream  Saturday, July 07, 2007 6:54 AM

You can use google to search for other answers

Custom Search

More Threads

• Where are the Hints?
• .NET Framework Error Message Box
• creating xls spread sheet from gridview. problem with formating/sorting in xls
• ContextMenuStrip Event Handler question
• Separate opacities in MSVC++.NET
• checkbox checkedchanged event
• SelectedText but not Property!
• Problem with Working with Datagrid with custom fucntions
• Problem using ActiveX of Acrobat Reader(8.0) in form
• Need Help with SqlDataAdapter