How do I set images to listview items when they contain certain text? - vb.net

Does anyone know how I set images to listview items when they contain certain text? For instance if an items' text is something with ".png" I want to give that item (or those items) an image which I've added to an imagelist.
Here is the code I use to populate the listview with folders and files:
Dim FilePath As String = "C:\"
ControlListView.Items.Clear()
Dim DirInfo() As DirectoryInfo
DirInfo = New DirectoryInfo(FilePath).GetDirectories
For Each DirInfoFolder In DirInfo
ControlListView.Items.Add(DirInfoFolder.Name)
Next
Dim FilePathFiles As New IO.DirectoryInfo(FilePath)
For Each FileInfoFolder In FilePathFiles.GetFiles
ControlListView.Items.Add(FileInfoFolder.Name)
Next
Any help would be appriciated. Thanks in advance :)

Instead of using the default ListView.Add(string), you need to construct your own ListViewItem and then add it to the ListView after setting the correct index for the image in the image list. (My VB.Net is rusty so please verify the syntax)
For Each FileInfoFolder In FilePathFiles.GetFiles
Dim lvi as New ListViewItem(FileInfoFolder.Name)
If FileInfoFolder.Name.EndsWith(".png")
lvi.ImageIndex = pngImageIndex
End If
ControlListView.Items.Add(lvi)
Next

Related

How to keep the content of ListBox in Vb.net

If i want to keep the content of a textbox i do this
TextBox1.Text = TextBox1.Text & Something
Is there a way to do the same thing for the content of Items of a Listbox?
In my RichTextBox3 i have the list of files in the C:\Work directory
I Tried this code but it's giving me The content of the last line (It's not adding the lines before)
Do Until number = RichTextBox3.Lines.Length
Dim directory = "C:\Work\" & RichTextBox3.Lines(number)
Dim files() As System.IO.FileInfo
Dim dirinfo As New System.IO.DirectoryInfo(directory)
files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
For Each file In files
ListBox1.Items.Add(file)
Next
number = number + 1
Loop
Help is appreciated
Thanks to all of you
I'm not sure that this will address your stated problem but there's a serious issue with that code and I need to provide a long code snippet to address it and that won't be readable in a comment.
The Lines property of a TextBox or RichTextBox is not "live" data, i.e. it doesn't refer to an array stored within the object. Each time you get the property, a new array is created. You are getting RichTextBox3.Lines twice for every iteration of that loop, so that's obviously wrong. You also should not be adding items to the ListBox one by one like that. You should be creating a list of all the items first, then adding them all with a single call to AddRange:
Dim files As New List(Of FileInfo)
For Each line In RichTextBox3.Lines
Dim folderPath = Path.Combine("C:\Work", line)
Dim folder As New DirectoryInfo(folderPath)
files.AddRange(folder.GetFiles("*", SearchOption.AllDirectories))
Next
ListBox1.Items.AddRange(files.ToArray())
If that code doesn't work as expected, you can debug it and view the contents of files at various stages to make sure that you are getting the files you expect. It might also be worth testing folder.Exists before calling GetFiles, unless you're absolutely sure that each line in the RichTextBox represents an existing folder.
This will do what you want.
number = 0
ListBox1.items.clear()
Do Until number = RichTextBox3.Lines.Length
Dim directory = "C:\Work\" & RichTextBox3.Lines(number)
Dim files() As System.IO.FileInfo
Dim dirinfo As New System.IO.DirectoryInfo(directory)
files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
For Each file In files
ListBox1.Items.Add(file)
Next
number = number + 1
Loop

Using Listview to insert an image in one column and write in another in vb

I have a listview with three column named: picture, time, description.
I need to add an image to the first column and at then add time and description to the other but in the same row.
I had a look on the internet and managed to find a code that adds the images to the listview. This is shown below:
Dim imagelist As ImageList = New ImageList()
imagelist.ImageSize = New Size(10, 10)
imagelist.Images.Add(Bitmap.FromFile("0.png"))
imagelist.Images.Add(Bitmap.FromFile("1.png"))
imagelist.Images.Add(Bitmap.FromFile("2.png"))
imagelist.Images.Add(Bitmap.FromFile("3.png"))
imagelist.Images.Add(Bitmap.FromFile("4.png"))
imagelist.Images.Add(Bitmap.FromFile("5.png"))
LV_Log.Items.Add("", 5)
LV_Log.SmallImageList = imagelist
Now I have a code that added text to the columns.
Dim lvi As New ListViewItem
lvi.Text = "image"
lvi.SubItems.Add(Now())
lvi.SubItems.Add(message)
LV_Log.Items.Add(lvi)
Here I want to add an image where it says "image".
Assuming the ListView's imageList property is set to the correct image list you can do something like this.
Dim newItem As ListViewItem = New ListViewItem
newItem.ImageIndex = 0 'or look for correct one from image list
newItem.SelectedImageIndex = 0 'if the image should change
newItem.Text = Now()
newItem.SubItems.Add("description") 'may need to play here, haven't done this in a while

VB-write selected item from combo box into a text file

I have a list of items in my combo box. I need to save the selected item only into a text file. My idea is something like this.
Dim stream As New System.IO.FileStream("C:\CONFIG\PA.txt", IO.FileMode.Append, IO.FileAccess.Write)
Dim write As New System.IO.StreamWriter(stream)
Dim PA As String = (ComboBox1.SelectedItem)
write.WriteLine(PA)
But I couldn't figure out the correct way to do it. Can anyone help?
Thank you in advance.
It would be good to check for a selected item before proceeding.
Then write it like this:
If Not ComboBox1.SelectedItem Is Nothing Then
File.WriteAllText("c:\config\pa.txt", ComboBox1.Text)
End If

Saving Columns of listview in VB with separator

So I have a listview which has two columns. the listview view is details.
I have successfully imported a file into the list view with correct splits. The code i used is,
Using sr As StreamReader = File.OpenText( file path )
While (-1 < sr.Peek())
Dim line As String = sr.ReadLine()
Dim item As New ListViewItem(line.Split(":"c))
ListView1.Items.Add(item)
End While
sr.Close()
End Using
So this imports the lines from my file to the program into correct columns with : as split.
Now I also have a option for users to add data from my program to the file the same way, I used this code,
Using sw As StreamWriter = File.AppendText(file path)
For Each item As ListViewItem in ListView1
Dim line As String = Nothing
For Each entry As String in item.SubItems
line.Append(entry & ":")
Next For
sw.WriteLine(line)
Next For
sw.Close()
End Using
Taken from : Separating text from .txt into colums in listview (VB.net mobile)
But my bad, vb gives this error,
Error 1 Expression is of type 'System.Windows.Forms.ListView', which is not a collection type. C:\Users\xxxx\documents\visual studio 2012\xxxxx\Form1.vb 97
I am not sure why i am getting this error, is it because of my list views properties ?
I want to be able to save the data to the text file when user click a button.
This line:
For Each item As ListViewItem in ListView1
should be this:
For Each item As ListViewItem in ListView1.Items
and this line:
For Each entry As String in item.SubItems
should be this:
For Each entry As ListViewItem.ListViewSubItem in item.SubItems
You then get a String from the Text property of the subitem.

VB.NET - Adding items to a listview as well as checkboxes and tag property

I've got this code which basically loops through a set of folders and subfolders and finds specific file types. And then lists these in a listview. Now, it's intended to list exe and msi files. And I've made it so that these icons can be doubleclicked after they have been listed. I do this by adding the path to the file in it's tag property.
But, my superiors want a checkbox next to each item. So that they can check each item they want installed. And then have a button which runs the path in each tag property one at a time. It's basically the part where I fill the listview with the checkbox, filename of the exe or msi file, tag and the icon that I'm wondering about.
This is the existing code. This includes just a Tile view of the listview.
Public Sub getDirectories(ByVal strFilepath As String, ByVal strFileExtension As String, ByVal objControl As Object)
'Load first files from the root folder. Then loop each subfolder
Dim di As New DirectoryInfo(strFilepath)
Dim aryFi As IO.FileInfo() = di.GetFiles(strFileExtension, SearchOption.AllDirectories)
Dim filePath As String
Dim fileIcon As Icon
' For each file in the root folder
For Each file In aryFi
If file.Extension = String.Empty Then
Else
filePath = GetAssociatedProgram(file.Extension)
On Error Resume Next
'Extract icon
fileIcon = Drawing.Icon.ExtractAssociatedIcon(filePath)
'Add the icon if we haven't got it already
objControl.StateImageList = Form1.iconList
If Form1.iconList.Images.ContainsKey(filePath) Then
Else
Form1.iconList.Images.Add(filePath, fileIcon)
End If
'Add item to list
objControl.items.add(file.Name, filePath).Tag = file.DirectoryName
End If
Next
End Sub
Basically I call this sub in this way:
getDirectories(strProgramLocation, "*.exe", Form1.listViewSupSoftware)
And I've found that I can add items to a listview which also contains columns:
Dim tempstr(2) As String
tempstr(0) = "Name of item"
tempstr(1) = "Target folder of item"
Dim tempNode As ListViewItem
tempNode = New ListViewItem(tempstr)
Form1.listViewItem.Items.Add(tempNode)
But there should be a way of combining these two right? I'm not sure how I can add a checkbox in the first column of the listview? I've already set the Checkbox property of the listview to True. But I could use some pointers here if anyone's got any. :)
Mmm, I don't understand quite well your question. If you already have the listbox you just need to use a CheckedListBox and loop thru all the checked items executing the executable stored into the tag property ...
As I said I'm not sure If I get what you mean ...