Home > Blockchain >  How to use properties of dynamically created picturebox in vb.net
How to use properties of dynamically created picturebox in vb.net

Time:12-19

I was trying to remove this "brickn" when ball intersect with this brick but i am facing problem that "brickn" is not declared any helps?

there is code

        Dim brickWidth as Integer = 0
    Public Function CreateBrick() As PictureBox
        Dim Brickn As New PictureBox
        
        Me.Controls.Add(Brickn)
        Brickn.Size = Brick.Size
        Brickn.Left = BrickWidth  
        Brickn.Top = 0
        Brickn.Image = Brick.Image
        Brickn.SizeMode = PictureBoxSizeMode.StretchImage
        Brickn.BackColor = Color.Black

        Return Brickn


    End Function
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

             For i= -75 To Me.Width 

            CreateBrick()
            BrickWidth  = 170   ' increasing brick.left on every new brick is created 
            i  = 170          ' increasing looop count according to brick needed

        Next

    Private Sub Boll_control_Tick(sender As Object, e As EventArgs) Handles Boll_control.Tick

        If Ball.Bounds.IntersectsWith(brickn.Bounds) Then
            Me.Controls.Remove(brickn)
        End If

    End Sub

why this "brickn" is not saying not declared in "boll control tick " timer

CodePudding user response:

You are instantiating brickn withing CreateBrick. You are returning it as the result of that function, but not assigning it to anything. So it's scope is limited to the CreateBrick fuction only, which is why it's not accessible from Boll_control_Tick.

Also you are then trying to create multiple instances of it with using a single object.

This code will allow you to create one or more. You will then need to rework your Boll_control_Tick to work out whether it intersects with any. You may want to create a list or array of PictureBox objects as Brickn instead of one.

Dim brickWidth as Integer = 0
Dim Brickn As PictureBox  ' This may be better as a list or an array

Public Function CreateBrick() As PictureBox
    Dim myBrickn As New PictureBox
    
    'Note - no idea what Brick is here or where it comes from
    Brickn.Size = Brick.Size
    Brickn.Left = BrickWidth  
    Brickn.Top = 0
    Brickn.Image = Brick.Image
    Brickn.SizeMode = PictureBoxSizeMode.StretchImage
    Brickn.BackColor = Color.Black

    Return myBrickn 

End Function

and then in your Form_Load method:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   For i= -75 To Me.Width 
       Brickn  = CreateBrick()  ' or add to your List/Array here
       Me.Controls.Add(Brickn)
       BrickWidth  = 170   ' increasing brick.left on every new brick is created 
       i  = 170          ' increasing looop count according to brick needed    
    Next

That will add multiple picture boxes to your controls and, if you want, create a list or array of them for easy access. You will need to loop through those in Boll_control_Tick to see if ball bounds intersects with any and then remove that specific one only

  • Related