Loading the ImageList to ListView doesn't work as intended in vb.net 2010 - vb.net

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

Related

Rows Added to a Datagridview not being captured in Screen Capture

I am running into an issue where I am attempting to add some blank rows to a datagridview before printing the form for use.
I am having an issue trying to understand where I need to put the code to add the rows to the data gridview so they are added and captured before the user hits print. My concern is that I am not adding the rows in the right spot before the screen is captured however the code to add the rows is run before the code to hide the buttons is implemented however the buttons are removed and the rows are not added in the actual printed image.
If that is not clear hopefully these images will make more sense. At this point I am not sure why the buttons being hidden are being captured but the added rows are not when based on the code it looks like the rows are being added before the buttons are hidden.
Thank you.
Here is the Image before the user hits print
Here is the Image after the user hits Print
And here is what actually printed
And here is the code in question that captures the screen
Private Sub CaptureScreen()
Dim myGraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
If dgvReceive.Rows.Count < 27 Then
Dim rowstoadd As Integer = 0
rowstoadd = 27 - dgvReceive.Rows.Count
dgvReceive.Rows.Add(rowstoadd)
End If
If MemoryImage IsNot Nothing Then
MemoryImage.Dispose()
End If
MemoryImage = New Bitmap(s.Width, s.Height, myGraphics)
For Each btn As Button In Me.Controls.OfType(Of Button)
btn.Visible = False
Next
dgvReceive.ClearSelection()
lblTitle.Select()
Me.FormBorderStyle = FormBorderStyle.None
Dim memoryGraphics As Graphics = Graphics.FromImage(MemoryImage)
memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
Me.FormBorderStyle = FormBorderStyle.Sizable
For Each btn As Button In Me.Controls.OfType(Of Button)
btn.Visible = True
Next
End Sub

How to access cells from programmatically created datagridview in VB.net?

When creating a datagridview in VB.net I cannot get at the cells from anywhere except the Sub where it was made.
I tried placing the code in Form_Load and tried make the Sub Public.
I use Controls.Find, which finds the datagridview, but I cannot access any rows or cells. I can create textbox and label controls and access their text content from anywhere, but not so with the datagridview. This is odd to me.
Using and learning Visual Basic in Visual Studio 2017, for use in my own home business utility.
Dim dgv As New DataGridView
dgv.Location = New Point(2, 200)
dgv.Size = New Size(300, 50)
dgv.Name = "NewDGV"
Dim ColumnTitles() As String = {"ProdId", "Price"}
Dim ColumnWidths() As Integer = {50, 50)
For i = 0 To UBound(ColumnTitles)
Dim col As New DataGridViewTextBoxColumn
col.DataPropertyName = ColumnTitles(i)
col.HeaderText = ColumnTitles(i)
col.Name = ColumnTitles(i)
col.Width = ColumnWidths(i)
dgv.Columns.Add(col)
Next
Controls.Add(dgv)
‘the following sees the datagridview just fine
‘within the same sub
Dim x = dgv.Rows(0).Cells(0).Value
This is how I am creating this control. Perhaps I am Adding to Controls in the wrong way? I am open to better ways to do this.
Dim dgv As DataGridView = CType(Controls("NewDGV"), DataGridView)
dgv.Rows(0).Cells(0).Value = "I found you!"
What this does is find the first Control in the Controls collection, coerces it into a DataGridView type, and assigns it to the variable dgv. You can than manipulate dgv. In this case I put some text in the first cell on the first row.
You could ask the control collection for the instance of your datagridview
Dim MyDgv As DataGridView = Controls.Cast(Of DataGridView).First(Function(dgv) dgv.Name = "NewDGV")
Then work with "MyDGV" properties, methods, and/or events

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

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.

Dynamically load Tabpages and data into those tabpages

I ma new Learner. I am creating an application in Visual Basic and I need help to load dynamically data from database into my tabpages.
I want TabPages names to be "Category Names" (From Tbl_Category) and in each Tabpages i want buttons to be created and these buttons are each record from "Tbl_ItemDetails.
button name will be from "ItemSKU" column. and button image will be from "Item_Image" column.
i have searched a lot but couldn't do it. Please help me on this.
if you guys know any other way of doing it (without tabpages) please share. Thanks!
here are some codes which i tried.
1. image code is not working.
2. if i have more items on tabpages it does not show any scroll bar. so i can see only few button not all.
![enter image description here][1]
Private Sub AddProductsToTabbedPanel()
Dim i As Integer = 1
For Each tp As TabPage In TabControl1.TabPages
Dim filteredProduct As ObjectQuery(Of Tbl_ItemDetails) = New ObjectQuery(Of Tbl_ItemDetails)(("SELECT VALUE P FROM Tbl_ItemDetails AS P WHERE P.CatID = " + i.ToString), cse)
Dim flp As FlowLayoutPanel = New FlowLayoutPanel
flp.Dock = DockStyle.Fill
For Each tprod As Tbl_ItemDetails In filteredProduct
Dim b As Button = New Button
b.Size = New Size(100, 100)
b.Text = tprod.ItemSKU
' ''how can i get image on button. bellow code is not working
'b.BackgroundImage = tprod.Item_Image
'b.Image = tprod.Item_Image
b.Tag = tprod
AddHandler b.Click, AddressOf Me.UpdateProductList
flp.Controls.Add(b)
Next
tp.Controls.Add(flp)
i = (i + 1)
Next
End Sub
1: How the Image will work
a) If you only provide a pathname to an existing file
button.Image = New Bitmap("C:\temp\Info.bmp")
b) If you get a bytearray from your DB containing the bitmap
Dim byteArrayFromDB As Byte()
Dim memStream As System.IO.MemoryStream
Dim img As Image
memStream = New System.IO.MemoryStream(byteArrayFromDB)
img = Image.FromStream(memStream)
2: Scrollable FlowLayoutPanel
flp.AutoScroll = True