Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Array of PictureBox

Posted on 2011-03-07
4
Medium Priority
?
861 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

885 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