Solved

Array Of PictureBoxes

Posted on 2011-03-08
7
410 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple file Upload asp.net 2 38
Code enhancement 5 22
Trying to use colormap to replace color in png, with a gradient to transparent 7 31
Variable Event ? 3 22
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 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

19 Experts available now in Live!

Get 1:1 Help Now