473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

creating an owner drawn listbox that includes a filetype icon and a filespec

I now know how to gather the file type icons and I'm able to use them in a
listview. But a listbox does not have a .smallimagelist or .largeimagelist
member, so I don't know how to translate that so that I have the icon + the
string in a listbox. Is this possible, and if so, can you give me some idea
how to go about it.

Thanks for any help.

Bernie Yaeger
Jul 21 '05 #1
4 1743
"Bernie Yaeger" <be*****@cherwellinc.com> schrieb:
I now know how to gather the file type icons and I'm able to use them in a
listview. But a listbox does not have a .smallimagelist or .largeimagelist
member, so I don't know how to translate that so that I have the icon + the
string in a listbox. Is this possible, and if so, can you give me some
idea how to go about it.


I am just curious why a listview control is not sufficient...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jul 21 '05 #2
Hi,
Try something like this.

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

Dim arIcons As New ArrayList

Dim clsIco As New clsGetIcon

Dim diRoot As New IO.DirectoryInfo("C:\")

For Each fiCur As IO.FileInfo In diRoot.GetFiles

Dim c As New FileHelper

c.FileName = fiCur.Name

c.FileIcon = clsIco.getIcon(fiCur.FullName)

arIcons.Add(c)

Next

ListBox1.DrawMode = DrawMode.OwnerDrawFixed

ListBox1.DataSource = arIcons

ListBox1.DisplayMember = "FileName"

End Sub


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

Dim g As Graphics = e.Graphics

Dim br As SolidBrush

Dim s As String

Dim c As FileHelper

Try

c = DirectCast(ListBox1.Items.Item(e.Index), FileHelper)

s = c.FileName

Catch ex As Exception

Trace.WriteLine(ex.ToString)

End Try

g.FillRectangle(Brushes.White, e.Bounds)

If CBool(e.State And DrawItemState.Selected) Then

g.FillRectangle(Brushes.LightBlue, e.Bounds)

End If

br = New SolidBrush(Color.Black)

g.DrawIcon(c.FileIcon, e.Bounds.Left, e.Bounds.Top)

g.DrawString(s, ListBox1.Font, br, e.Bounds.Left + 25, e.Bounds.Top)

br.Dispose()

End Sub

Helper classes

Public Class FileHelper

Dim ico As Icon

Dim strFile As String

Public Property FileIcon() As Icon

Get

Return ico

End Get

Set(ByVal Value As Icon)

ico = Value

End Set

End Property

Public Property FileName() As String

Get

Return strFile

End Get

Set(ByVal Value As String)

strFile = Value

End Set

End Property

End Class

Imports System.Runtime.InteropServices

Public Class clsGetIcon

Public Structure SHFILEINFO

Dim hIcon As IntPtr

Dim iIcon As Integer

Dim dwAttributes As Integer

<VBFixedString(260), System.Runtime.InteropServices.MarshalAs( _

System.Runtime.InteropServices.UnmanagedType.ByVal TStr, _

SizeConst:=260)> Public szDisplayName As String

'String that contains the name of the file as it appears in the Microsoft®
Windows® Shell, or the path and file name of the file that contains the icon
representing the file.

<VBFixedString(80), System.Runtime.InteropServices.MarshalAs( _

System.Runtime.InteropServices.UnmanagedType.ByVal TStr, _

SizeConst:=80)> Public szTypeName As String

End Structure

Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As
String, _

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _

ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Private Const SHGFI_ICON As Integer = &H100

Private Const SHGFI_SMALLICON As Integer = &H1 'Small icon

Private Const SHGFI_TYPENAME As Integer = &H400 ' get type name

Public Function getIcon(ByVal szFilename As String) As Icon

Try

Dim aSHFileInfo As New SHFILEINFO

Dim cbFileInfo As Integer = Marshal.SizeOf(aSHFileInfo)

Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)

Dim myIcon As Icon

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

aSHFileInfo.szTypeName = Space(255)

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, SHGFI_TYPENAME)

Trace.WriteLine(aSHFileInfo.szTypeName)

Return myIcon

Catch ex As Exception

Debug.WriteLine(ex.ToString)

Return Nothing

End Try

End Function

End Class

Ken

---------------------

"Bernie Yaeger" <be*****@cherwellinc.com> wrote in message
news:Of**************@TK2MSFTNGP15.phx.gbl...
I now know how to gather the file type icons and I'm able to use them in a
listview. But a listbox does not have a .smallimagelist or .largeimagelist
member, so I don't know how to translate that so that I have the icon + the
string in a listbox. Is this possible, and if so, can you give me some idea
how to go about it.

Thanks for any help.

Bernie Yaeger

Jul 21 '05 #3
Hi Herfried, Cor;

There are several reasons why the listview is inadequate.

I am trying to replicate the funtionality and display of the Outlook Express
attachment box. The listview does not give me a horizontal scrollbar until
there is a second column displayed, but the path string sometimes runs
beyond the bounds of the listview. In addition, I can't get a vertical
scrollbar in it. Third, I do not want multi cols. Fourth, only 'list' view
can do what I need done - even smallicon view is inadequate.

The listbox has most of what I need intrinsically.

Thanks to both of you for your responses, as always.

Bernie Yaeger

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Os**************@TK2MSFTNGP10.phx.gbl...
"Bernie Yaeger" <be*****@cherwellinc.com> schrieb:
I now know how to gather the file type icons and I'm able to use them in a
listview. But a listbox does not have a .smallimagelist or
.largeimagelist member, so I don't know how to translate that so that I
have the icon + the string in a listbox. Is this possible, and if so, can
you give me some idea how to go about it.


I am just curious why a listview control is not sufficient...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jul 21 '05 #4
Hi Ken,

Thanks, as always; I will try your suggestion.

Bernie

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OX****************@tk2msftngp13.phx.gbl...
Hi,
Try something like this.

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

Dim arIcons As New ArrayList

Dim clsIco As New clsGetIcon

Dim diRoot As New IO.DirectoryInfo("C:\")

For Each fiCur As IO.FileInfo In diRoot.GetFiles

Dim c As New FileHelper

c.FileName = fiCur.Name

c.FileIcon = clsIco.getIcon(fiCur.FullName)

arIcons.Add(c)

Next

ListBox1.DrawMode = DrawMode.OwnerDrawFixed

ListBox1.DataSource = arIcons

ListBox1.DisplayMember = "FileName"

End Sub


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

Dim g As Graphics = e.Graphics

Dim br As SolidBrush

Dim s As String

Dim c As FileHelper

Try

c = DirectCast(ListBox1.Items.Item(e.Index), FileHelper)

s = c.FileName

Catch ex As Exception

Trace.WriteLine(ex.ToString)

End Try

g.FillRectangle(Brushes.White, e.Bounds)

If CBool(e.State And DrawItemState.Selected) Then

g.FillRectangle(Brushes.LightBlue, e.Bounds)

End If

br = New SolidBrush(Color.Black)

g.DrawIcon(c.FileIcon, e.Bounds.Left, e.Bounds.Top)

g.DrawString(s, ListBox1.Font, br, e.Bounds.Left + 25, e.Bounds.Top)

br.Dispose()

End Sub

Helper classes

Public Class FileHelper

Dim ico As Icon

Dim strFile As String

Public Property FileIcon() As Icon

Get

Return ico

End Get

Set(ByVal Value As Icon)

ico = Value

End Set

End Property

Public Property FileName() As String

Get

Return strFile

End Get

Set(ByVal Value As String)

strFile = Value

End Set

End Property

End Class

Imports System.Runtime.InteropServices

Public Class clsGetIcon

Public Structure SHFILEINFO

Dim hIcon As IntPtr

Dim iIcon As Integer

Dim dwAttributes As Integer

<VBFixedString(260), System.Runtime.InteropServices.MarshalAs( _

System.Runtime.InteropServices.UnmanagedType.ByVal TStr, _

SizeConst:=260)> Public szDisplayName As String

'String that contains the name of the file as it appears in the Microsoft®
Windows® Shell, or the path and file name of the file that contains the
icon
representing the file.

<VBFixedString(80), System.Runtime.InteropServices.MarshalAs( _

System.Runtime.InteropServices.UnmanagedType.ByVal TStr, _

SizeConst:=80)> Public szTypeName As String

End Structure

Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath
As
String, _

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _

ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Private Const SHGFI_ICON As Integer = &H100

Private Const SHGFI_SMALLICON As Integer = &H1 'Small icon

Private Const SHGFI_TYPENAME As Integer = &H400 ' get type name

Public Function getIcon(ByVal szFilename As String) As Icon

Try

Dim aSHFileInfo As New SHFILEINFO

Dim cbFileInfo As Integer = Marshal.SizeOf(aSHFileInfo)

Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)

Dim myIcon As Icon

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

aSHFileInfo.szTypeName = Space(255)

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, SHGFI_TYPENAME)

Trace.WriteLine(aSHFileInfo.szTypeName)

Return myIcon

Catch ex As Exception

Debug.WriteLine(ex.ToString)

Return Nothing

End Try

End Function

End Class

Ken

---------------------

"Bernie Yaeger" <be*****@cherwellinc.com> wrote in message
news:Of**************@TK2MSFTNGP15.phx.gbl...
I now know how to gather the file type icons and I'm able to use them in a
listview. But a listbox does not have a .smallimagelist or
.largeimagelist
member, so I don't know how to translate that so that I have the icon +
the
string in a listbox. Is this possible, and if so, can you give me some
idea
how to go about it.

Thanks for any help.

Bernie Yaeger

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eric | last post by:
I implemented owner drawing on the main and context menus of my main form. It works fine. There is also a notify icon control that has a context menu. When the icon is in the status bar, it...
1
by: Patty O'Dors | last post by:
Hi I have some code to create an ownerdrawn listbox (derived), and when I add an item to it, the bold text of the first item (the title, 'Collections and Maturities') mysteriously seems to get...
5
by: Bernie Yaeger | last post by:
I now know how to gather the file type icons and I'm able to use them in a listview. But a listbox does not have a .smallimagelist or .largeimagelist member, so I don't know how to translate that...
0
by: LostInMd | last post by:
Hi All, I've got an owner drawn listBox where I draw and measure the items that I add to the listBox. For example, I have a listBox that can only display 10 characters on each horizontal line. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.