Solved

Array Of PictureBoxes

Posted on 2011-03-08
7
399 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 74

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 74

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 74

Expert Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 …
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

16 Experts available now in Live!

Get 1:1 Help Now