Thursday 14 July 2011

Dynamic Create Controls on Runtime

Here is my quick guide on how to generate ImageHolder Buttons on runtime. This is perfect and works nice with inventory systems or payroll applications.(ID picture of employee)

First of all create a new vb.net project, drag a panel onto your form.
Double click on your form to open up the Form_Load() method and Return to design view.

















After you add the panel, set its Dock property to Fill.
 Right click on form1 and view code, to return to the Form_Load() method.
 In your Form_Load() Method add the following code:

Panel1.Controls.Clear()  ' This Clears the Panel of all controls created on previous runtimes.
Panel1.AutoScroll = True ' Enable this to enable scrollbars on the panel if too many controls is created
Dim pic1 As New PictureBox
       pic1.Width = 223           ' Set the size of your picture buttons
       pic1.Height = 264        
       pic1.SizeMode = PictureBoxSizeMode.StretchImage ' This will stretch the image to fit
       pic1.BorderStyle = BorderStyle.Fixed3D
       pic1.Image = Image.FromFile(("noemployee.jpg")) 'Input pathname of imagefile
       Panel1.Controls.Add(pic1) 'This add the picturebox to the panel
       AddHandler pic1.Click, AddressOf PictureBox_Click 'enable a on click event for the picturebox
                   
Now on runtime you will generate a picturepox within your panel. Linking the ImageFile to a pathname stored in your database for each employee file would enable you to generate one for each employee.


For more help on that email me at fbotes2@gmail.com.

No comments:

Post a Comment