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

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

Related

Adding images into next available picturebox

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

autoCompleteSource vb overflow?

I tried to add autoCompleteSource in vb.net textbox so it displays a list of suggestion. But I get this error.
Is it because I try to add more than 300 records in suggestion . If so what can I do to add a scroll bar to display the records in suggestion. I guess I'll be needing to create my own handler if so could you suggest me the properties to consider to add scroll bar to create my own textbox autocomplete.
[UPDATE]
Dim MySource As New AutoCompleteStringCollection()
lst.Add("apple")
lst.Add("applle")
lst.Add("appple")
lst.Add("appplee")
lst.Add("bear")
lst.Add("pear")
'Records binded to the AutocompleteStringCollection.
MySource.AddRange(lst.ToArray)
'My own data source with 300 records
Dim index(cmboList.Items.Count) As String
For i As Integer = 0 To cmboList.Items.Count - 1
If cmboList.Items.Count > 0 Then
index(i) = cmboList.Items(i).ToString()
End If
Next
MySource.AddRange(index.ToArray)
txtExpressionBuilder.AutoCompleteCustomSource = MySource
'Auto complete mode set to suggest append so that it will sugesst one
'or more suggested completion strings it has bith ‘Suggest’ and
'‘Append’ functionality
txtExpressionBuilder.AutoCompleteMode = AutoCompleteMode.Suggest ' SuggestAppend
'Set to Custom source we have filled already
txtExpressionBuilder.AutoCompleteSource = AutoCompleteSource.CustomSource

VB.NET: DataGridViewImageCell - Image from Ressource?

I have added a datagridview on a windows form with the name DataGridView1. The following code adds a row with 2 columns. I want to show an image in the 2nd column.
Dim dt As New DataTable
dt.Columns.Add("TESTROW")
dt.Rows.Add("TESTCONTENT")
DataGridView1.DataSource = dt
Dim colImage As New DataGridViewImageColumn
DataGridView1.Columns.Add(colImage)
For intI As Integer = 0 To dt.Rows.Count
Dim cellImage As New DataGridViewImageCell
' THE FOLLOWING LINE WORKS FINE!!!!
cellImage.Value = Drawing.Image.FromFile("c:\foo\bar.gif")
' BUT WHY NOT THIS?
' cellImage.Value = Properties.Resources.ResourceManager.GetObject("ExistingRessource")
' OR THIS?
' cellImage.Value = CType(Properties.Resources.ResourceManager.GetObject("ExistingRessource"), Image)
cellImage.ImageLayout = DataGridViewImageCellLayout.Zoom
DataGridView1.Rows(intI).Cells(1) = cellImage
Next
It's working fine if I use "fromFile" with the path to the image and the 2nd column shows the gif picture inside the cell.
Unfortunately, my attempt to load an image from the ressource ("GetObject") fails, and the cell shows a page-symbol with a red cross on it.
I got all the images I need inside the ressource.
How can I achieve this?
Thanks in advance.
If you've added an image to the resources, then you can access it with my.resources.ResourceName where ResourceName seems to be "ExistingRessource" in your case
It's possible that your current attempts are failing because the resource isn't actually added properly, or you have got the spelling wrong on the name? Either way if you use my.resources you can see for certain that the resource is added properly.
cellImage.Value = My.Resources.ExistingRessource

How do I set images to listview items when they contain certain text?

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

VB.Net using databindings with a picture box

A row, in a data table, iscalled FirstImage contains a url to an image file on a web server. I am trying to bind the data of this row to the image source of the picture box.
My current code:
For Each row As DataRow In ListData.Rows
Dim ImageDecode = ser.Deserialize(Of PropertyImage())(row("Images"))
row("FirstImage") = "http://rental.joshblease.co.uk/propertyimages/" & ImageDecode(0).Image
'Returns http://rental.joshblease.co.uk/propertyimages/image1.jpg
Next row
TxtListName.DataBindings.Add("Text", ListData, "Name")
TxtListSlug.DataBindings.Add("Text", ListData, "Slug")
TxtListCreated.DataBindings.Add("Text", ListData, "Created")
ImgListItem.DataBindings.Add("Image", ListData, "FirstImage", True)
DataRepeater1.DataSource = ListData
But at the moment, the image is still blank. I have tried entering the location into a hidden textbox and copying the data over, but I can;t figure out how to use the controls in a data repeater.
This was the experimental copy from a hidden text box code:
If Me.DataRepeater1.ItemCount > 0 Then
Dim n As Integer = Me.DataRepeater1.ItemCount
For i As Integer = 1 To n
Me.DataRepeater1.CurrentItemIndex = i - 1
Dim item = Me.DataRepeater1.CurrentItem
item.Controls("ImgListItem").ImageLocation = item.Controls("TxtImageLocation").Text
Next
End If
Simply add Picture Box property ImageLocation
ImgListItem.DataBindings.Add("ImageLocation", ListData, "FirstImage", True)
The databinding for the image expects binary image data and in this case your passing it a string. What we can do is convert the image location into a format the binding can understand. Take a look at this link C# Code Snippet - Download Image from URL. Then once you have the image in memory, you will be able to bind it to your PictureBox.
Also, keep in mind that the simplest way shown in this answer would not work for you since URIs are not supported by the BitMap class.