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

x
?
Solved

Array Of PictureBoxes

Posted on 2011-03-08
7
Medium Priority
?
444 Views
Last Modified: 2012-06-27
I''m new to VB.NET and i'm trying to create a array of PictureBoxes that I will need the events and handle of.

I goto the code below off EE but i get the following error
Array cannont be declared with NEW

I suspect i'm missing a
Imports System ??????

Any help would be appreciated.

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
0
Comment
Question by:PhilChapmanJr
  • 3
  • 2
  • 2
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35069247
You made an array of Lists!

Remove the parentheses from your declaration:

Dim newPB As New List(Of PictureBox) 

Open in new window

0
 
LVL 13

Expert Comment

by:LIONKING
ID: 35069264
Try: Dim myPB() As List(Of PictureBox)  = New List(Of PictureBox)
and let us know...
0
 
LVL 13

Expert Comment

by:LIONKING
ID: 35069271
Yeah... kaufmed is correct... Remove the parenthesis...
0
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!

 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 35069641
Removeing the parenthese fixed the Array cannont be declared with NEW

But when I try to get he handle
Example:
 Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim i As Integer
        For i = 0 To 2
            MsgBox(newPB(i).Handle)
        Next
End Sub

Index was out of range.  Must be non-negative and less thatn the sized of the collection Parameters



0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 35070813
Let's rework your code a bit  : )

In your OP, you have two declarations of newPB--one at the form level, and one inside of a method. The one inside of the method hides the form-level due to scope. Inside of your method, you add new PictureBoxes to your method-level variable, but the form-level variable is never touched, hence the exception: you never added anything to the form-level variable (i.e. it's empty). Let's try this modification:

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

            For i = 0 To 2  
                Me.newPB.Add(New PictureBox()) ' Must call "Add()" to insert elements to the list
                
                With Me.newPB(i)  
                    .Top = 230  
                    .Width = 230  
                    If i > 0 Then  
                        .Left = Me.newPB(i - 1).Left + Me.newPB(i - 1).Width + 6  
                    Else  
                        .Left = 3  
                    End If  
                    .Height = 200  
                    .Parent = Me  
                    .BorderStyle = BorderStyle.Fixed3D  
                    .Visible = True  
                End With  
            Next  
    End Sub
    
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Dim i As Integer
            For i = 0 To 2
                MsgBox(Me.newPB(i).Handle)
            Next
    End Sub
End Class

Open in new window


Now, in your Button1_Click_1 handler, you are working with the form-level variable itself. A practice I have adopted is to always preface form-level (class-level) variable with "Me.", so I know I'm working with the form-level variable. Then, when you start with "Me." you'll quickly see variables available to you via Intellisense, and should not have to worry (as much) about variable hiding.
0
 
LVL 2

Author Closing Comment

by:PhilChapmanJr
ID: 35118860
I thought I had already closed this issue.  Thanks for the explination.
.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35122959
NP. Glad to help  :  )
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

916 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