Search This Blog

Monday, July 6, 2009

Add a Control array at run time using vb.net e.g picturebox

Public PRINT_GRAPH_NOS As Integer = 12
Public PB(14) As System.Windows.Forms.PictureBox

Public Sub create_graph_image()
PB = New System.Windows.Forms.PictureBox(14) {}

For i As Integer = 0 To PRINT_GRAPH_NOS - 1
Me.Controls.Add(PB(i))
PB(i) = New PictureBox
With PB(i)
.Size = New Size(5, 5)
.BackColor = Color.Red
.Visible = True
.BringToFront()
.Show()
PB(i).Tag = "filename"
AddHandler PB(i).Paint, AddressOf PictureboxAdd
End With

Next
end sub

Public Sub PictureboxAdd(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
Dim p As Windows.Forms.PictureBox = DirectCast(sender, Windows.Forms.PictureBox)
Dim fnt As New Font("Times New Roman", 10, FontStyle.Regular)
e.Graphics.DrawString(p.Tag, fnt, Brushes.Black, 20, 20)
End Sub

No comments:

Post a Comment