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.)
Related
I'm doing a deck builder project through a card database and so far when I click a row (using datagridview), the value contained in the "image_url" column is printed into an invisible textbox, which is then used to download that image and show it in a picturebox.
Now that all works fine, but decks go up to 60 cards so I'm going to use 60 pictureboxes to print the user's selected cards. What I'm been trying to do is set up like a picturecount and when they select a column the number is increased by one like this:
picturebox(picturecount) = textbox4.text
but I've run into too many errors. Would you know a way to display the user's selected card in the next available picturebox? For example if they select "Dark Magician" three times, then the image of the "Dark Magician" is printed in the first available three pictureboxes
VB.NET:
Private Async Sub PictureLoader()
Dim imageURL As String
If TextBox4.Text = "" Then
imageURL = dataSet.Tables("YGO cards").Rows(row_count).Item(7)
Else
imageURL = TextBox4.Text
End If
Dim client As Net.WebClient = New Net.WebClient()
Dim ms As MemoryStream = New MemoryStream(Await client.DownloadDataTaskAsync(New Uri(imageURL)))
Using image As Image = Image.FromStream(ms)
PictureBox1.Image?.Dispose()
PictureBox1.Image = DirectCast(image.Clone(), Image)
End Using
ms.Dispose()
client.Dispose()
End Sub
and this is the event when a column is selected in the datagrid!
Dim index As Integer
index = e.RowIndex
Dim selectedrow As DataGridViewRow
selectedrow = DataGridView1.Rows(index)
' selectedrow.Cells(1) is the image_Url column
TextBox4.Text = selectedrow.Cells(1).Value.ToString()
If TextBox4.Text = "" Then
PictureBox1.Image = Nothing
' imageURL = dataSet.Tables("YGO cards").Rows(row_count).Item(7)
Else
PictureLoader()
End If
Ignore for a moment the specifics of your particular problem and break this down into a generic statement. What you're saying is that you have a collection and that the size of the collection can grow or shrink based on user input. This is an ideal case for a List(Of T) where you declare the List by specifying the data type of the items it will hold and then add items as needed. Because you are storing the URL of the card, you would create a new List(Of String):
Dim cards As List(Of String) = New List(Of String)
Now whenever you needed to add URLs to your list you would call the Add method if it is a single URL or AddRange if it is multiple URLs:
cards.Add(TextBox1.Text)
'Or
cards.AddRange({TextBox1.Text, TextBox2.Text, TextBox3.Text})
As far as displaying the image in the PictureBox, there's really no need to create a MemoryStream and clone an Image considering that the PictureBox class has the Load and LoadAsync (which it looks like you want asynchronous capabilities) methods. But if you wanted to create a PictureBox for every item in your collection, you will need to iterate through the collection, create a new PictureBox, call the Load or Load Async method on the currently iterated URL, and then add it to the Form (or a container in general). This can be done using a traditional For/Each loop:
'Create a placeholder variable
Dim cardPictureBox As PictureBox
'Loop through every selected card URL
For Each url As String In Cards
'Create a new PictureBox
cardPictureBox = New PictureBox() With {
.Size = New Size(100, 100)
.SizeMode = PictureBoxSizeMode.CenterImage
.WaitOnLoad = False
}
'Add the PictureBox to the Form
Me.Controls.Add(cardPictureBox)
'Load the image asynchronously
cardPictureBox.LoadAsync(url)
Next
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
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
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
I try to load images from ImageList to ListView and it works great... but when the form is load, the last image of ImageList doesn't appear.... I will call this procedure at the combobox selectedIndexChanged event and at the form load... when the form load, the last image from ImageList doesn't appear and when I click the combobox for several times, all of the images appear as intended... Any help?
Here is the code..
Private Sub LoadMenuItem()
lvItem.Clear()
Dim adptItem As New Restaurant_Management_SystemDataSetTableAdapters.ItemTableAdapter
Dim categoryID As Integer = cboccategory.SelectedValue
Dim dtItem As Restaurant_Management_SystemDataSet.ItemDataTable = adptItem.GetDataByCategoryID(categoryID)
lvItem.BeginUpdate()
For Each drItem As Restaurant_Management_SystemDataSet.ItemRow In dtItem.Rows
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(drItem.ImageForItem)
Dim img As Image = System.Drawing.Image.FromStream(ms)
ImageList1.Images.Add(drItem.ItemID, img)
lvItem.Items.Add(drItem.ItemName.Trim, drItem.ItemID)
Next
lvItem.EndUpdate()
cboItem.ValueMember = "ItemID"
cboItem.DisplayMember = "ItemName"
cboItem.DataSource = dtItem
lvItem.Refresh
End Sub