Dynamic PictureBox in Visual Basic - vb.net

I've created a form with a PictureBox on it and would like to dynamically create another PictureBox on the form while the program runs (to the left of the static one). I've written this code:
Dim temp As PictureBox
temp = New PictureBox
temp.Image = StaticPictureBox.Image
temp.Visible = True
temp.Top = StaticPictureBox.Top
temp.Width = StaticPictureBox.Width
temp.Height = StaticPictureBox.Height
temp.Left = StaticPictureBox.Left - 20
temp.BringToFront()
When I run this code I can detect that the temp PictureBox does get created. However, it is not rendered onto the form. It seems like it's there but is invisible.
Does anyone have an idea of what I'm doing wrong?

You need to add it to the form's control collection:
Me.Controls.Add(temp)

Why don't you just remove that code and place a picturebox next to the other one and set:
newpicturebox.visible = false
Then whenever you have the action completed you have it change:
newpicturebox.visible = true

I know this is old but... you got an error here:
temp.Left = StaticPictureBox.Left - 20
should be:
temp.Left = StaticPictureBox.right + 20
or:
temp.Left = StaticPictureBox.right
hope it helped.

Related

How do I display an image from a DataGridView to another PictureBox in a new Windows Form?

What I'm trying yo do is once I clicked on a row in my DataGridView, clicked on the view button, and it opens a new window that displays all the data from the DataGridView to the textboxes. I already did the first part, but I don't know how to do it with the images (I have 2 image columns). Can someone help me out? I'm only starting, sorry. This is my code for the view button that opens up another window. The picture boxes are always blank.
Private Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
If GunaDataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected) > 0 Then
data.txtID.Text = GunaDataGridView1.CurrentRow.Cells(0).Value.ToString
data.txtLN.Text = GunaDataGridView1.CurrentRow.Cells(1).Value.ToString
data.txtFN.Text = GunaDataGridView1.CurrentRow.Cells(2).Value.ToString
data.txtMN.Text = GunaDataGridView1.CurrentRow.Cells(3).Value.ToString
data.txtGen.Text = GunaDataGridView1.CurrentRow.Cells(4).Value.ToString
data.txtNum.Text = GunaDataGridView1.CurrentRow.Cells(5).Value.ToString
data.txtDOB.Text = GunaDataGridView1.CurrentRow.Cells(6).Value.ToString
data.txtAddress.Text = GunaDataGridView1.CurrentRow.Cells(7).Value.ToString
data.txtPlate.Text = GunaDataGridView1.CurrentRow.Cells(8).Value.ToString
data.txtVT.Text = GunaDataGridView1.CurrentRow.Cells(9).Value.ToString
data.txtVB.Text = GunaDataGridView1.CurrentRow.Cells(10).Value.ToString
data.txtVYM.Text = GunaDataGridView1.CurrentRow.Cells(11).Value.ToString
data.txtSP.Text = GunaDataGridView1.CurrentRow.Cells(12).Value.ToString
data.ownerPhoto.Image = GunaDataGridView1.CurrentRow.Cells(13).Value
data.carPhoto.Image = GunaDataGridView1.CurrentRow.Cells(14).Value
data.ShowDialog()
End If
End Sub
I saw another guy asked this question and another guy commented with this code line:
Dim bytes As Byte() = DataGridView1.CurrentRow.Cells(13).Value
Using ms As New MemoryStream(bytes)
ownerPP.Image = Image.FromStream(ms)
End Using
Dim bit As Byte() = DataGridView1.CurrentRow.Cells(14).Value
Using memory As New MemoryStream(bit)
carPhoto.Image = Image.FromStream(memory)
End Using
It works perfectly. I hope this helps someone out too.

Image won't change in picturebox, vb.net

I'm trying to code it so that i can create a picture box from a method in a class. However when my picture box is drawn it doesn't display any image, it only shows a white square of the specified dimensions in the specified location.
Here is the code which i am using to create said picture box:
Public Sub DrawEnemy(ByRef formInstance)
Dim enemypic As New PictureBox
enemypic.Image = Image.FromFile("C:\fboi1\Enemy.Png")
enemypic.Width = 64
enemypic.Height = 64
enemypic.Location = New Point(Me.EnemyPosX, EnemyPosY)
enemypic.Visible = True
formInstance.Controls.Add(enemypic)
End Sub
And here is where i am calling the method from:
Dim Enemy1 As New computerControlled(1, 1)
Enemy1.DrawEnemy(Me)
Please add the following code in your DrawEnemy() method:
enemypic.SizeMode = PictureBoxSizeMode.StretchImage
When i drag the console window suddenly the white box turns into the image i wanted.
Aha! This means the code is not causing the form to be repainted. We can trigger that by calling the Invalidate() function.
Public Sub DrawEnemy(formInstance As Form)
Dim enemypic As New PictureBox
enemypic.Image = Image.FromFile("C:\fboi1\Enemy.Png")
enemypic.Width = 64
enemypic.Height = 64
enemypic.Location = New Point(Me.EnemyPosX, EnemyPosY)
enemypic.Visible = True
formInstance.Controls.Add(enemypic)
formInstance.Invalidate()
End Sub
If you're calling this several times in a loop, you would instead handle this after the loop, where you also block repainting (to prevent flickering) until the loop is finished.
form.SuspendLayout()
For Each enemy In ...
'...
DrawEnemy(form)
Next
form.Invalidate()
form.ResumeLayout()
It's also possible you only need to Invalidate() the picturebox.

Why don't my Labels show in VB.NET Form

After reading this question, I wrote some code to create a label for each attribute of an xml element.
The problem is that when I run the project, my form only displays the first label. I've checked in the immediate window as well as debug window and all of the labels are loaded to the form, but none of them are displayed. Help?
Here's the code that runs when the form loads.
Dim doc As New XmlDocument()
doc.Load("xmlfile")
Dim ability As XmlNode = doc.GetElementsByTagName("ability").Item(0)
Dim numberofLabels = ability.Attributes.Count
ReDim labels(numberofLabels)
For counter As Integer = 0 To numberofLabels - 1
labels(counter) = New Label
labels(counter).Visible = True
labels(counter).Text = ability.Attributes.Item(counter).Name
labels(counter).Location = New System.Drawing.Point(10, 30 + counter * 10)
Me.Controls.Add(labels(counter))
Next
You should be using some layout manager, to help you with control positioning. Doing it manually is not worth the pain. Try using TableLayoutPanel or FlowLayoutPanel. Both can be docked or anchored to a parent control, so everything behaves very smoothly. Otherwise you are looking to write a lot of positioning/resizing code, and then maintaining it later.
Change the value of 10 in the original code line for a new point to a bigger value such as 40, so that new labels could appear separated visually:
labels(counter).Location = New System.Drawing.Point(10 + counter, 30 + counter * 40)

Order of controls being added to panel, control not showing unless docked

I imagine this is probably an easy to answer question but for some reason I can't get it to work
Sub New(ByVal Sess As AudioSessionControl2)
S_Session = Sess
'Create the panel and position it.
S_Panel.BackColor = Color.AliceBlue
S_Panel.Width = 200
S_Panel.Height = 40
Dim Position As New Point(6, 19)
If G_AppSessions.Count > 0 Then
Position = Point.Add(G_AppSessions.Item(G_AppSessions.Count - 1).SessionPanel.Location, New Point(0, 45))
End If
S_Panel.Location = Position
'Create a label which has the name of the process
Dim S_PName As New Label
S_PName.Text = "Test"
S_PName.Dock = DockStyle.Left
S_Panel.Controls.Add(S_PName)
'Create a button to change volume
Dim S_Save As New Button()
S_Save.Text = "Save"
AddHandler S_Save.Click, AddressOf Save_Click
S_Save.Parent = S_Panel
S_Panel.Controls.Add(S_Save)
S_Volume.Parent = S_Panel
S_PName.Parent = S_Panel
MainForm.Controls.Add(S_Panel)
S_Panel.Parent = MainForm.gb_Applications
End Sub
The problem is that, the label will show because its docked, but the button won't. It will only show if its docked as well, and thats just not what I want. This is part of a class for creating a dynamic UI, where I can create a number of this class to create a bunch of panels for various things.
I don't see anywhere where you are setting the label or button position. You probably have them both at 0,0 and the label is on top of the button, obscuring it. Did you try setting the position of both the controls, making sure they don't overlap?

Changing the layout order of vb.net controls

I dynamically add picture boxes to a vb.net form. However, when I add the new picture box, it is always under/below/behind the picture boxes that I previously created. Is it possible to change it so that the newly created picture box would always be in front of the others?
Thanks
Just add this code to the part that creates the pictureboxes
PictureBox.BringToFront()
Dim mypic As PictureBox = New PictureBox
mypic.Height = 13
mypic.Width = 13
mypic.Left = 100
mypic.Top = 100
mypic.Visible = True
mypic.SizeMode = PictureBoxSizeMode.Normal
mypic.Parent = Me
mypic.Image = Image.FromFile("Path of the Image")
Me.Controls.Add(mypic)
mypic.bringtofront()