Create control at runtime [duplicate] - vb.net

This question already has answers here:
How to programmatically add controls to a form in VB.NET
(5 answers)
Closed 8 years ago.
I need to make a picturebox in VB.NET using code not the toolbox so it is not on my form just i draw it when I want it in code. Also No I don't want to use the: visible = false or true
I want to get a picturebox shown on a special position/x,y. Then I need it to execute a command once clicked. I am making a vb game and that is going to be pretty much the graphics layout.
If it may help i was pretty much thinking of it displaying a panel which is going to be made by using the draw features etc...

This code should help you to
Create a PictureBox programmatically
Position it on the form
Set an image
Add a click event handler
Add it to the form
Make a new WinForms project and paste this code. No need to add any controls
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
addPictureBoxToForm()
End Sub
Private Sub addPictureBoxToForm()
Dim pb As New PictureBox
pb.Location = New Point(0, 0)
pb.Image = Bitmap.FromFile("C:\test.png")
AddHandler pb.Click, AddressOf PictureBox_Click
Me.Controls.Add(pb)
End Sub
Private Sub PictureBox_Click(sender As Object, e As EventArgs)
MessageBox.Show("The picture box was clicked")
End Sub

Related

How do I make controls inside of a FlowLayoutPanel do things?

I'm adding controls to a FlowLayoutPanel like this:
Dim box As New PictureBox
(I'm making the controls using code, then I'm using FlowPanelLayout1.Controls.Add(box)).
How do I make these controls do things? In my code I'm using For Each, so multiple are made using this, my goal is to make each one be able to do what I want and the code for each would be different. How can I achieve this goal?
I believe what you're after is AddHandler. AddHandler allows you to programmatically assign code to an event at runtime. So you can create a Sub specifically to handle events from the picture boxes you create.
You can also retrieve the control that raised the event. This allows you to modify it, or access it's tag, allowing you to attach data to the PictureBox when you add it to the FlowLayoutPanel.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Create a new picturebox
Dim PB As New PictureBox With {.Size = New Drawing.Size(50, 50), .SizeMode = PictureBoxSizeMode.Normal}
PB.Image = Image.FromFile("res.png")
'Add it to the FlowLayoutPanel
FlowLayoutPanel1.Controls.Add(PB)
'Assign HandlePictureboxClick() to handle the PictureBox's DoubleClick event.
AddHandler PB.DoubleClick, AddressOf HandlePictureboxClick
End Sub
Public Sub HandlePictureboxClick(sender As Object, e As EventArgs)
'Retrive the control that raised the event (In this case, the one you double-clicked)
Dim Picturebox As PictureBox = CType(sender, PictureBox)
'Do whatever you want to do with it
Picturebox.SizeMode = PictureBoxSizeMode.Zoom
End Sub
The example above will change the PictureBox's size mode to zoom when the user double clicks on it, without modifying the other PictureBoxes in the FlowLayoutPanel

How can I center form when opening it in MDI Parent form?

I've been trying to solve a problem that occured on my program. All the forms I open from the menu refuse to center based in MDIParent form. Since I'm using 2 panels to design a custom toolbar (containing the Close and Minimize buttons) and another panel regarding the menu.
Here is an image to explain my struggle on fixing my problem.
Image regarding my problem
Things I've tried:
Private Sub RegistarDevoluçãoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RegistarProjetoToolStripMenuItem.Click
Dim janela As New frmRegProjeto
janela.MdiParent = Me
janela.StartPosition = FormStartPosition.CenterParent
janela.Show()
End Sub
Answer by Hans Passant:
Since it would not work on the specific case of an MDI child window CenterScreen object should get the job done:
Private Sub RegistarDevoluçãoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RegistarProjetoToolStripMenuItem.Click
Dim janela As New frmRegProjeto
janela.MdiParent = Me
janela.StartPosition = FormStartPosition.CenterScreen
janela.Show()
End Sub

Create a Picturebox when a button was press

I am creating an OS system called KernalOS With full functioning desktop But my problem is I want to be able to add an Icon when I click a button. I want the Icon to detect if mouse button was down / clicked.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim newPictureBox As New PictureBox
newPictureBox.Visible = True
newPictureBox.Top = 20
newPictureBox.Width = 100
newPictureBox.Height = 50
newPictureBox.Left = 10
'add control to form
Controls.Add(newPictureBox)
newPictureBox.Location = New Point(100, 100)
End Sub
This code isn't working right so Idk what going on or why its not working. Please ask question I will fill you in with details. thanks Guys
Use the AddHandler Event:
newPictureBox.AddHandler, AddressOf newPictureBox_Click
newPictureBox.AddHandler, AddressOf newPictureBox_MouseDown
Now I'm not sure about how precise that is; you now have to write a
Private Sub newPictureBox_Click.
This is probably not the entire correct syntax, but you get the idea.

How to Handle events from a control array VB.net

To start, hi to everyone im new to stackoverflow, and also new to programing (on 1º year).
I've been searching but ive found nothing that answer my question, or maybe im just to newbie to understand the answers, so im sorry if its too simple, i cant see it!
/* my native lenguage is not english*/
Here is my problem, i'm making a VB form whit 200 pictureboxes that have to change or interact on click
i've made a control array whit all of them, like this:
Dim control(199) As PictureBox = Controles(control, 0)
Function Controles(ByRef control As Array, ByVal cont As Integer)
For Each pic As PictureBox In Me.Controls
control(cont) = pic
cont += 1
Next
Return control
End Function
this should asociate each picturebox to an array position, my problem now is how i can set the event handler to watch at control().click so no matter what box you click the event onclick will proc.
the only way i know is to create a click handler for each box manually.
hope i can find some answers
Using the Addhandler statement you can wire them all to the same routine. Then cast the sender object to interact with the PB that was clicked. OfType function.
Private Sub LoadME() Handles Me.Load
For Each pb As PictureBox In Me.Controls.OfType(Of PictureBox)()
'add all PB click events to a event sub
AddHandler pb.Click, AddressOf pb_Click
Next
End Sub
Private Sub pb_Click(sender As Object, e As EventArgs)
Dim pb = DirectCast(sender, PictureBox)
'this is the PB that was clicked
End Sub

add on click event to picturebox vb.net

I have a flowLayoutPanel which I am programatically adding new panelLayouts to. Each panelLayout has a pictureBox within it. It's all working nicely, but I need to detect when that picture box is clicked on. How do I add an event to the picture? I seem to only be able to find c# examples....
my code to add the image is as follows...
' add pic to the little panel container
Dim pic As New PictureBox()
pic.Size = New Size(cover_width, cover_height)
pic.Location = New Point(10, 0)
pic.Image = Image.FromFile("c:/test.jpg")
panel.Controls.Add(pic)
'add pic and other labels (hidden in this example) to the big panel flow
albumFlow.Controls.Add(panel)
So I assume somewhere when I'm creating the image I add an onclick event. I need to get the index for it also if that is possible! Thanks for any help!
Use the AddHandler statement to subscribe to the Click event:
AddHandler pic.Click, AddressOf pic_Click
The sender argument of the pic_Click() method gives you a reference to the picture box back:
Private Sub pic_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim pic As PictureBox = DirectCast(sender, PictureBox)
' etc...
End Sub
If you need additional info about the specific control, like an index, then you can use the Tag property.
Substitute PictureBox1 with the name of your control.
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
'executes when PictureBox1 is clicked
End Sub