Solved

Array of PictureBox

Posted on 2011-03-07
4
828 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 48
XML error not understood 5 33
.Net remove carriage returns, line feeds and tabbs 5 37
Adding Rows to Grid 4 34
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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

752 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