Solved

Array of PictureBox

Posted on 2011-03-07
4
815 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now