How To Get Icons For Pictures Files in vb.net - vb.net

I Tried To Get Icons Of files system and display it in my list Box
But i got an empty icon For Picture Files
This Is My Code:
Public Sub ExtractAssociatedIconEx()
' Initialize the ListView, ImageList and Form.
listView1 = New ListView()
imageList1 = New ImageList()
listView1.Location = New Point(37, 12)
listView1.Size = New Size(161, 242)
listView1.SmallImageList = imageList1
listView1.View = View.SmallIcon
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.listView1)
Me.Text = "Form1"
' Get the c:\ directory.
Dim dir As New System.IO.DirectoryInfo(TextBox1.Text)
Dim item As ListViewItem
listView1.BeginUpdate()
Dim file As System.IO.FileInfo
For Each file In dir.GetFiles()
' Set a default icon for the file.
Dim iconForFile As Icon = SystemIcons.WinLogo
item = New ListViewItem(file.Name, 1)
' Check to see if the image collection contains an image
' for this extension, using the extension as a key.
If Not (imageList1.Images.ContainsKey(file.Extension)) Then
' If not, add the image to the image list.
iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName)
imageList1.Images.Add(file.Extension, iconForFile)
End If
item.ImageKey = file.Extension
listView1.Items.Add(item)
Next file
listView1.EndUpdate()
End Sub
How I Can Get Icons For picture Files ?
Thanks

Related

How to display a single Windows Explorer folder (that cannot be navigated away from) within a userform?

I would like to have a box within the userform display the contents of a single folder.
I would like the folder to display icons similar to how Windows Explorer does, and I would also like users to be able to drag icons from other windows into it (just like a real explorer window).
I decided to use a listview, and this will populate it with a bunch of lines of text for each file, but clicking on them does nothing and I cannot drag anything in. Also they don't have icons.
Any ideas?
Dim fileEntries As String() = Directory.GetFiles("C:\Windows\")
For Each fileName As String In fileEntries
ListView1.Items.Add(fileName)
Next
This MSDN article describes exactly how to do this. it even gets the icons, as you requested! It also uses a ListView, too! Enjoy!
As Jeremy said, you will still need to hook up event, if you want it to respond to click (or drag) events.
Private listView1 As ListView
Private imageList1 As ImageList
Public Sub ExtractAssociatedIconEx()
' Initialize the ListView, ImageList and Form.
listView1 = New ListView()
imageList1 = New ImageList()
listView1.Location = New Point(37, 12)
listView1.Size = New Size(161, 242)
listView1.SmallImageList = imageList1
listView1.View = View.SmallIcon
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.listView1)
Me.Text = "Form1"
' Get the c:\ directory.
Dim dir As New System.IO.DirectoryInfo("c:\")
Dim item As ListViewItem
listView1.BeginUpdate()
Dim file As System.IO.FileInfo
For Each file In dir.GetFiles()
' Set a default icon for the file.
Dim iconForFile As Icon = SystemIcons.WinLogo
item = New ListViewItem(file.Name, 1)
' Check to see if the image collection contains an image
' for this extension, using the extension as a key.
If Not (imageList1.Images.ContainsKey(file.Extension)) Then
' If not, add the image to the image list.
iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName)
imageList1.Images.Add(file.Extension, iconForFile)
End If
item.ImageKey = file.Extension
listView1.Items.Add(item)
Next file
listView1.EndUpdate()
End Sub

How to create .key file with specific text

I've been trying to make a program that is able to create a file with a .key extension, which contains a 5 line text.
It is fundamental to have 5 lines, otherwise it won't work.
I use
Dim filepath As String = TextBox1.Text + "\\rarreg.key"
Dim rarreg As New IO.StreamWriter(filepath, True)
rarreg.Write(String.Join(Environment.NewLine, hiddenTxt))
The hiddenTxt contains all the text needed and it's multilined.
However, when I click on the button to call this functions, it succesfully creates the file, but it comes empty.
Try this
' Add any initialization after the InitializeComponent() call.
Dim filepath As String = ""
Dim dialog As New SaveFileDialog
dialog.DefaultExt = "key"
dialog.FileName = "rarreg.key"
dialog.InitialDirectory = "c:\temp"
Dim results As DialogResult = dialog.ShowDialog()
If results <> Windows.Forms.DialogResult.Cancel Then
filepath = dialog.FileName
End If​

Displaying Application Icons (and names) in ListView Object

I,
I'm creating a VB Form with a ListView object containing Images of Excel, Word or Pdf Icons, with a link to the displayed document (to be made)
When compilating, the names of the documents are displayed in the listView but not the icons. Do you know what is missing in my code?
As far as I understand, the "ExtractAssociatedIcon" method needs the full path to the file, but it seems that no Icon is collected here.
Thanks
Imports System
Imports System.IO
Imports System.Drawing
[...]
Dim dirInfo As DirectoryInfo
Dim fileInfo As FileInfo
Dim exePath As String
Dim exeIcon As Drawing.Icon
dirInfo = New DirectoryInfo("G:\XXX\XXX\XXX\XXX\XXX")
'We use this For...Each to iterate over the collection of files in the folder
For Each fileInfo In dirInfo.GetFiles
'We can only find associated exes by extension, so don't show any files that have no extension
If fileInfo.Extension = String.Empty Then
Else
'Use the function to get the path to the executable for the file
exePath = fileInfo.FullName
'Use ExtractAssociatedIcon to get an icon from the path
exeIcon = Drawing.Icon.ExtractAssociatedIcon(exePath)
'Add the icon if we haven't got it already, with the executable path as the key
If ImageList1.Images.ContainsKey(exePath) Then
Else
ImageList1.Images.Add(exePath, exeIcon)
End If
'Add the file to the ListView, with the executable path as the key to the ImageList's image
ListView1.View = View.LargeIcon
ListView1.Items.Add(fileInfo.Name, exePath)
End If
Next
1) You need to set the SmallImageList and/or the LargeImageList property of the ListView:
ListView1.LargeImageList = ImageList1
ListView1.SmallImageList = ImageList1
2) Put this at the top of your code. (Not in the For Each loop)
ListView1.View = View.LargeIcon
3) Also, be sure that you don't add an empty icon, nor sets an invalid image key:
If (ImageList1.Images.ContainsKey(exePath)) Then
ListView1.Items.Add(fileInfo.Name, exePath)
ElseIf (Not exeIcon Is Nothing) Then
ImageList1.Images.Add(exePath, exeIcon)
ListView1.Items.Add(fileInfo.Name, exePath)
Else
ListView1.Items.Add(fileInfo.Name)
End If
Example
The following code is tested and works fine:
ListView1.LargeImageList = ImageList1
ListView1.SmallImageList = ImageList1
ListView1.View = View.LargeIcon
Dim dirInfo As DirectoryInfo
Dim fileInfo As FileInfo
Dim exeIcon As System.Drawing.Icon
dirInfo = New DirectoryInfo("...")
For Each fileInfo In dirInfo.GetFiles
If (Not String.IsNullOrEmpty(fileInfo.Extension)) Then
exeIcon = System.Drawing.Icon.ExtractAssociatedIcon(fileInfo.FullName)
If (ImageList1.Images.ContainsKey(fileInfo.FullName)) Then
ListView1.Items.Add(fileInfo.Name, fileInfo.FullName)
ElseIf (Not exeIcon Is Nothing) Then
ImageList1.Images.Add(fileInfo.FullName, exeIcon)
ListView1.Items.Add(fileInfo.Name, fileInfo.FullName)
Else
ListView1.Items.Add(fileInfo.Name)
End If
End If
Next

Listview. Load image from file

Is it possible to load image in first column (detail view) Listview from a file instead from ImageList's and how?
Dim lvi As ListViewItem = New ListViewItem("Row1")
With .Items.Add("0") '' PICTURE HERE IF POSSIBLE
.SubItems.Add("User name")
.SubItems.Add("Unactive")
End With
Thanks.
No, you would have to make a imagelist "on the fly".
Something on the lines of:-
Dim ImageList As New ImageList
Dim Lvi As New ListViewItem()
ImageList.Images.Add(PictureBox1.Image, Color.White)
With Lvi
.Text = "Blah..."
.ImageIndex = 0
End With
ListView1.Items.Add(Lvi)
(NOTE: Didnt test the code, just wrote it off the top of my head.)

space between tabs in tabcontrol Dotnetbar VB.NET

i am trying to make a new tab and my new tabs always have a space between them
im new to this site so i cant post pictures so heres a link to my issue http://i43.tinypic.com/rbbndk.png
heres my new tab code
Private Sub NewPageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewPageToolStripMenuItem.Click
Dim newpage As TabItem = TabControl1.CreateTab("newpage")
Dim textb As New RichTextBox
Dim filename As New TextBox
Dim sw As New StreamWriter(siteloc & filename.Text & ".hsp")
Dim createb As New Button
'Set the contents of the page
textb.ReadOnly = False
textb.Multiline = True
textb.Dock() = DockStyle.Top
textb.Height = 338
newpage.Text = "NewPage"
filename.Text = "Untitled"
filename.Location = New Point(5, 342)
filename.Width = 220
createb.Location = New Point(232, 342)
createb.Text = "Submit page"
'Add the tab to the page
newpage.AttachedControl.Controls.Add(filename)
newpage.AttachedControl.Controls.Add(textb)
newpage.AttachedControl.Controls.Add(createb)
TabControl1.Tabs.Add(newpage)
'select the tab
TabControl1.SelectedTab = newpage
'add button handlers
AddHandler createb.Click, AddressOf createb_Click
End Sub
i looked all over for answers and found nothing. i havent changed anything in the propertys and i just installed dotnetbar.
CreateTab method already adds the newly created tab to Tabs collection (see docs for the method) so you are adding it twice. Remove this code: TabControl1.Tabs.Add(newpage)
Hope this helps.