Solved

Array of PictureBox

Posted on 2011-03-07
4
820 Views
Last Modified: 2012-05-11
I'm new to vb.Net and I'm trying to create a array of picture boxes.  That I will need to place an external application in.  I will also need the events of the picture box

I'm using the code below

Public Class Form1
Dim newPB() As PictureBox


Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        i = 2
        Dim newPB(i) As PictureBox
        For i = 0 To 2
            newPB(i) = New PictureBox
            With newPB(i)
                .Top = 230
                .Width = 230
                If i > 0 Then
                    .Left = newPB(i - 1).Left + newPB(i - 1).Width + 6
                Else
                    .Left = 3
                End If
                .Height = 200
                .Parent = Me
                .BorderStyle = BorderStyle.Fixed3D
                .Visible = True
            End With
        Next
    End Sub

When I try to get the
MsgBox(newPB(0).Handle)

I get the following message
Use the "new" keyword to create an object instance

If I am going about it the wrong way please feel free to correct.

Any help would be appreciated.
0
Comment
Question by:PhilChapmanJr
  • 3
4 Comments
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 35063708
Dim newPB() As PictureBox

to

Dim newPB(2) As New PictureBox

0
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 35063709
Change the code

Dim newPB() As PictureBox

to

Dim newPB(2) As New PictureBox

0
 
LVL 8

Accepted Solution

by:
PagodNaUtak earned 500 total points
ID: 35063725
Or you can optimize your code by using generic list instead...

Try something like the code below:


Public Class Form1
Dim newPB() As New List(Of PictureBox)


Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        i = 2
        Dim newPB(i) As PictureBox
        For i = 0 To 2
            newPB(i) = New PictureBox
            With newPB(i)
                .Top = 230
                .Width = 230
                If i > 0 Then
                    .Left = newPB(i - 1).Left + newPB(i - 1).Width + 6
                Else
                    .Left = 3
                End If
                .Height = 200
                .Parent = Me
                .BorderStyle = BorderStyle.Fixed3D
                .Visible = True
            End With
        Next
    End Sub

Open in new window

0
 
LVL 2

Author Closing Comment

by:PhilChapmanJr
ID: 35064124
Thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question