Solved

Array Of PictureBoxes

Posted on 2011-03-08
7
440 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
[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
  • 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
Industry Leaders: 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 500 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

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

627 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