I am creating lots of PictureBox's on my Form but they will not display. I have tried to debug it to see if its trying to show in the wrong location and stuff like that however everything seems to be correct. I have tried to call Show() but that did nothing either. I used to be experienced with VB quite a while ago but fell out and forget a lot of the stuff.

Yes, I am calling it with createPixels(0)

    Private Sub createPixels(layer As Integer)
        For i As Integer = 0 To 255
            For j As Integer = 0 To 255
                Dim pb As PictureBox = New PictureBox()
                Dim loc As Point = New Point()

                pb.Size = New Size(1, 1)
                pb.BackColor = Color.FromArgb(j, i, layer)

                loc.X = j + (1 + layer)
                loc.Y = 25 + i + (1 + layer)

                pb.Location = loc
                loc = Nothing
            Next
        Next
    End Sub

Recommended Answers

All 8 Replies

It appears to me that, you're creating the pictureboxes but you're not adding them to the Controls collection of the form. Something like this should work:

Private Sub createPixels(layer As Integer)
    For i As Integer = 0 To 255
        For j As Integer = 0 To 255
            Dim pb As PictureBox = New PictureBox()
            Dim loc As Point = New Point()
            pb.Size = New Size(1, 1)
            pb.BackColor = Color.FromArgb(j, i, layer)
            loc.X = j + (1 + layer)
            loc.Y = 25 + i + (1 + layer)
            pb.Location = loc
            loc = Nothing
            Me.Controls.Add(pb)
        Next
    Next
End Sub

Alright that did something, however when I try to run the Application it waits for a while then gives me this Error.

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dll

Additional information: Error creating window handle.

Any ideas?
Heres all my code.

Public Class Form1

    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'For value As Integer = 0 To 255
        'createPixels(value)
        'Next
        createPixels(0)
    End Sub

    Private Sub Btn_Close_MouseClick(sender As Object, e As MouseEventArgs) Handles Btn_Close.MouseClick
        Me.Close()
    End Sub

    Private Sub Btn_Minimize_MouseClick(sender As Object, e As MouseEventArgs) Handles Btn_Minimize.MouseClick

    End Sub

    Private Sub Btn_Close_MouseEnter(sender As Object, e As EventArgs) Handles Btn_Close.MouseEnter
        Btn_Close.Image = My.Resources.close_h
    End Sub

    Private Sub Btn_Close_MouseLeave(sender As Object, e As EventArgs) Handles Btn_Close.MouseLeave
        Btn_Close.Image = My.Resources.close
    End Sub

    Private Sub MenuBar_MouseDown(sender As Object, e As MouseEventArgs) Handles MenuBar.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub MenuBar_MouseUp(sender As Object, e As MouseEventArgs) Handles MenuBar.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub MenuBar_MouseMove(sender As Object, e As MouseEventArgs) Handles MenuBar.MouseMove
        If IsFormBeingDragged = True Then
            Dim tempPoint As Point = New Point()

            tempPoint.X = Me.Location.X + (e.X - MouseDownX)
            tempPoint.Y = Me.Location.Y + (e.Y - MouseDownY)

            Me.Location = tempPoint
            tempPoint = Nothing
        End If
    End Sub

    Private Sub Btn_Minimize_MouseEnter(sender As Object, e As EventArgs) Handles Btn_Minimize.MouseEnter
        Btn_Minimize.Image = My.Resources.minimize_h
    End Sub

    Private Sub Btn_Minimize_MouseLeave(sender As Object, e As EventArgs) Handles Btn_Minimize.MouseLeave
        Btn_Minimize.Image = My.Resources.minimize
    End Sub

    Private Sub createPixels(layer As Integer)
        For i As Integer = 0 To 255
            For j As Integer = 0 To 255
                Dim pb As PictureBox = New PictureBox()
                Dim loc As Point = New Point()

                pb.Size = New Size(1, 1)
                pb.BackColor = Color.FromArgb(j, i, layer)

                loc.X = j + (1 + layer)
                loc.Y = 25 + i + (1 + layer)

                pb.Location = loc
                pb.Show()
                loc = Nothing

                Me.Controls.Add(pb)
            Next
        Next
    End Sub
End Class

You can do your loops like

For i As Integer = 0 To 255
   For j As Integer = 0 To 255
        Dim pb As New PictureBox
        Dim loc As New Point
        Dim sz As New Size(1, 1)

        pb.Size = sz
        pb.BackColor = Color.FromArgb(j, i, layer)
        loc.X = j + (1 + layer)
        loc.Y = 25 + i + (1 + layer)
        pb.Location = loc
        pb.Show()
        loc = Nothing
        Me.Controls.Add(pb)
     Next
     Application.DoEvents()
  Next

But, Application.DoEvents() will be slowed down the loops. If you do not use this you couldn't display the picture boxes until the loops completed.

Your problem might be in how many pictureboxes you're adding. 65,536 pictureboxes is quite a lot. Perhaps you need to look at a different way to accomplish your task.

I am basically trying to draw a grid with spaces in between that I can color each cell individually. Then I need to draw 255 more of them on a sort of z axis.

Probably easier to use Java and Open GL but I have always hated using Open GL and I hate doing graphics with Java.

You should probably submit that as a new question and get some other ideas on that.

Try to convert it the image to byte:

Public Function ConvertImageToByte(ByVal Img As Image) As Byte()

        Dim ms As MemoryStream = New MemoryStream()
        Img.Save(ms, Imaging.ImageFormat.Jpeg)
        Dim data(CInt(ms.Length)) As Byte
        ms.Position = 0

        ms.Read(data, 0, CInt(ms.Length))
        ms.Close()
        ms = Nothing
        ConvertImageToByte = data

End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.