2

I have an array of picture boxes as so:

    Dim pieces(500) As PictureBox
    pieces(1) = New PictureBox
    With pieces(1)
        .CreateControl()
        .Visible = True
        .BackColor = Color.Red
        .Width = 50
        .Height = 50
        .Left = 50
        .Top = 50
    End With

The program does not crash or anything, but the picturebox is no where to be seen on the form. How to I make this work correctly?

And is 'Control Array' the correct term for this? or something else?

0

2 Answers 2

4

It won't show up until you add those PictureBoxes to a form.

I suppose you already have a Windows Form, so all you have to do is: Window.Controls.Add(PictureBox)

Supposing your form object is called "Window"

You need to add them one by one and they don't need to be on an array, that's why there's a Control collection inside the Windows Form

Control Array is a VB 6 term, is not used in .NET anymore. The programming model between .NET and VB 6 is very different, you should take the time to go through a good tutorial or good book.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked perfectly thanks. As for the term 'Array of Controls' I think what I meant was an 'Array of Objects'. TYVM.
2

You need to add it to the form or panel where you want it/them displayed.

CreateControl only creates children and forces the creation of the control's Handle, but it will not place it onto the a form or parent control (it wouldn't know what to add it too!).

2 Comments

Ah, that makes sense. I remember trying this a while ago and it didn't work for creating an object. Slipped my mind I guess.
It happens to everyone. Good luck :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.