Solved

Array Of PictureBoxes

Posted on 2011-03-08
7
430 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Close form "before" open 3 49
start a process from a service 3 33
export data table to excel in vb.net 10 36
VB.Net - Not Sure if Multi-threading is What I Need 4 30
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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