dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] Create PictureBox when search term met

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Location
    Dundee, Scotland
    Posts
    6

    Resolved [RESOLVED] Create PictureBox when search term met

    Hello again. Im having yet more trouble with my program. I am a novice programmer.

    What I have is a 4x8 2D array filled with the strings "block" or "space" e.g

    Array(0, 0) = "Block"
    Array(0, 1) = "Space" etc

    What I want to do is, if the arrays cell contains an instance of "block" , then create a new picturebox using the 2d arrays row and column numbers for a reference for the picture boxes position

    I also want to associate the picturebox to either the specific cell of the 2d array or a new entry in a 1D array (since I cant add it to a control array as far as Im aware because there are no instances of a picture box on the form at design time.) or a collection. To be truthful im not sure which is best because bearing in mind this will be part of a breakout style game, im not sure what to use for storing the blocks since ill have to deal with remove or replacing instances of a block when the said block is hit by a ball.

    Then this will repeat until all instances of block are found


    I hope this is possible and that i've not lost anyone... This makes so much more sense in my head


    Here is my code. The commented out part should help clarify what I want to achieve.

    Code:
    Option Explicit
    Dim InputText As String
    
    Private Sub CmdLoadFile_Click()
    
    'Open "path to file" For Input As #1
    
    InputText = Input(LOF(1), #1)
    InputText = Replace(InputText, Chr(0), "")
    InputText = Replace(InputText, vbCrLf, "")
    
    Close #1
    Text1.Text = InputText
    Call CreateGameArea
    
    End Sub
    
    
    Function CreateGameArea()
    
    Dim ArraySplit1D() As String
    Dim ArraySplit2D(3, 7) As String
    Dim TotalBlocks As Long
    Dim int2X As Integer
    Dim int2Y As Integer
    
    ArraySplit1D = Split(InputText, ",")
    
    ReDim Preserve ArraySplit1D(32) As String
    
    TotalBlocks = 0
    
    For int2X = 0 To 3
        For int2Y = 0 To 7
            ArraySplit2D(int2X, int2Y) = ArraySplit1D(int2Y + (8 * int2X))
                Print "Item("; int2X; int2Y; "): "; ArraySplit2D(int2X, int2Y)
                    If ArraySplit2D(int2X, int2Y) = "block" Then
                        
                    ' note height of block will be 20px, width of block will be 50 px
                    ' Create newblock
                    ' with newblock
                    ' set .Top = (int2x *(height of block))
                    ' set .left = (int2y *(width of block))
                    ' set .width = width of block
                    ' set .height = height of block
                    ' set up remaining picturebox elements
                    ' some how assign newblock a position in this array, another array or collection
                    ' end with
                        
                       
                        TotalBlocks = TotalBlocks + 1
                    End If
        Next int2Y
    Next int2X
    Print
    Print ; TotalBlocks;
    
    End Function
    Thank you for your time!
    Last edited by jh622; Nov 1st, 2009 at 09:18 AM.

  2. #2
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Create PictureBox when search term met

    you can create controls dynamically, by adding them to the form's Controls collection.

    Code:
      Dim pctDynPicBox As PictureBox 'declare an object for the picturebox control
    
      Set pctDynPicBox = Me.Controls.Add("VB.PictureBox", "PictureBox" & CStr(int2X) & "-" & CStr(int2Y2)) 'assign the new control to this dynamically created picturebox.
    
      pctDynPicBox.Top = (int2x *(height of block)) 'you can then use it as a control.

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,126

    Re: Create PictureBox when search term met

    You have lost me but perhaps I can help anyhow because you are mistaken when you say "since I cant add it to a control array as far as Im aware because there are no instances of a picture box on the form at design time". What most people do is to add a picturebox at design time and set its Index property to 0 and its Visible property to False giving you a hidden control array. Then when you want to use the control array you determine the value of Picture1.Count and if it's 1 you make Picture1(0) Visible and otherwise use Load to create more members of the control array.

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Location
    Dundee, Scotland
    Posts
    6

    Re: Create PictureBox when search term met

    Thank you both for your time and help. I have made a little progress but I am still a little confused. I am encountering some errors and am unaware as to how to correct them.

    Jim thank you for your help. By using your code
    Code:
     
    Set pctDynPicBox = Me.Controls.Add("VB.PictureBox", "PictureBox" & CStr(int2X) & "-" & CStr(int2Y2)) 'assign the new control to this dynamically created picturebox.
    I am encountering a Not Legal Object Name error, however by removing

    Code:
    & CStr(int2X) & "-" & CStr(int2Y2))
    It is allowing me to place one instance of a picture box however I run into run time error 727 "there is already a control with the name "PictureBox." when it reaches the next string instance of "block"

    Martin thanks for that information and Im sorry I haven't explained my problem clear enough. I have attached an image that I quickly made in paint that I hope should allow you to understand my problem.

    If I choose to create a hidden picturebox at design time would it be possible to a) move this picture box to the position of picbox 1 on the attached image, and b) when adding new pictureboxes to that control array move them to the positions of picbox 2 and 3 on the attached image.

    Name:  problem.JPG
Views: 78
Size:  21.7 KB

  5. #5

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Location
    Dundee, Scotland
    Posts
    6

    Re: Create PictureBox when search term met

    Sorry instead of linked I should have put "all PicBoxes are members of an array"

    Since this is a game of breakout when a block is hit by the ball I need to identify which block is being hit and call the collision code when the block is hit

    My group has not created the collision code yet, however I thought that by having all the blocks in an array it would reduce the amount of code required and make it easier for the member of my team who is dealing with the collisions by allowing him to apply the collision code to the array of blocks rather than each individual block

    For example

    If collisionPicBox(i) = true then do something to PicBox(i)


    I hope this has cleared things up a bit more however I fear I may have this completely wrong and that this has made it worse. Sorry for being a pain

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Location
    Dundee, Scotland
    Posts
    6

    Re: Create PictureBox when search term met

    Sorry for the double post.

    Worked it out. Thank you both for your assistance and time!

    For anyones future reference here is the code I have used. The form contains a picture box called "block"

    Code:
    Option Explicit
    Dim InputText As String
    
    Private Sub CmdLoadFile_Click()
    
    'Open "PATH TO YOUR TEXT.TXT FILE" For Input As #1
    Me.ScaleMode = 3
    
    InputText = Input(LOF(1), #1)
    InputText = Replace(InputText, Chr(0), "")
    InputText = Replace(InputText, vbCrLf, "")
    
    Close #1
    Call CreateGameArea
    
    End Sub
    
    Function CreateGameArea()
    
    Dim ArraySplit1D() As String
    Dim ArraySplit2D(3, 7) As String
    Dim TotalBlocks As Long
    Dim int2X As Integer
    Dim int2Y As Integer
    Dim i As Integer
    
    
    ArraySplit1D = Split(InputText, ",")
    ReDim Preserve ArraySplit1D(32) As String
    
    TotalBlocks = 0
    
    For int2X = 0 To 3
        For int2Y = 0 To 7
            ArraySplit2D(int2X, int2Y) = ArraySplit1D(int2Y + (8 * int2X))
                    If ArraySplit2D(int2X, int2Y) = "block" Then
    i = i + 1
    Load Block(i)
    Block(i).Visible = True
    Block(i).Top = (5 + (int2X * 21))
    Block(i).Left = (5 + (int2Y * 51))
    Block(i).Width = 50
    Block(i).Height = 20
    'Block(i).Picture = LoadPicture("PATH TO YOUR IMAGE")
    Block(i).ScaleMode = 3
    Block(i).BorderStyle = 0
    Block(i).Appearance = 0
    
                        TotalBlocks = TotalBlocks + 1
                    End If
        Next int2Y
    Next int2X
    
    Print
    Print
    Print
    Print
    Print
    Print
    Print
    Print
    Print
    Print
    Print ; TotalBlocks;
    
    End Function
    Last edited by jh622; Nov 1st, 2009 at 02:47 PM.

  8. #8
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: [RESOLVED] Create PictureBox when search term met

    I am encountering a Not Legal Object Name error, however by removing
    yes i made a mistake, the "-" is not a legal name. i meant as a "_" character. however the control array you are using is definitely the best way, so nevermind.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width