473,320 Members | 1,930 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,320 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 1738
"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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.