Results 1 to 20 of 20

Thread: Array issue

  1. #1

    Thread Starter
    New Member DanMnz82's Avatar
    Join Date
    Dec 2018
    Posts
    5

    Question Array issue

    I'm not sure if it's an "array" but I'm attempting to create multiple pictureboxes on a form that all have the same code. I use to be able to copy/paste a picturebox in vb6 and it would create picturebox(x) for me automatically, but I'm confused how to do that now or if there is a better way to do this.

    For instance, let's say I have 20 picture boxes on my form, I want pictureboxes 1-25 to display a picture I put in the code without having to define each one on my own with saying picturebox1 picturebox2 picturebox3 etc.

    - Thank you

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,313

    Re: Array issue

    To create an array of pictureboxes...

    Code:
    Dim pictureboxes() As PictureBox
    
    Public Sub Form_Load(‘etc
    
        pictureboxes = New PictureBox() {PictureBox1, PictureBox2, PictureBox3}
    
    End Sub

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,313

    Re: Array issue

    To wire them all up to one click handler, for example...

    Code:
    For Each pb As PictureBox in pictureboxes
        AddHandler pb.Click, AddressOf pbs_click
    Next
    Code:
    Public Sub pbs_click(ByVal sender as object, ByVal e as EventArgs)
        Dim pb as PictureBox = DirectCast(sender, PictureBox)
        ‘pb is the picturebox that was clicked
    End Sub

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,313

    Re: Array issue

    The array doesn’t create new PictureBoxes. It refers to existing controls

  5. #5

    Thread Starter
    New Member DanMnz82's Avatar
    Join Date
    Dec 2018
    Posts
    5

    Re: Array issue

    I'm not looking to CREATE new ones, I want to put them on the form myself, then control them during runtime in the code.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,313

    Re: Array issue

    In that case, the array I showed you is the way to go. Declare it at form level, load it in form_load, then use it anywhere in the form code...

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,313

    Re: Array issue

    If you are planning on adding or removing pictureboxes during the lifetime of the form, use a List(of PictureBox) instead of an array. List items are easily added or removed...

  8. #8

    Thread Starter
    New Member DanMnz82's Avatar
    Join Date
    Dec 2018
    Posts
    5

    Re: Array issue

    I'm going to try the list idea. The other post you have I'm very confused with.

    I'm just trying something like when the user presses space, all the pictureboxes backcolor changes. Doing something simple just to make sure I can have all the pictureboxes change with a command.

  9. #9

    Thread Starter
    New Member DanMnz82's Avatar
    Join Date
    Dec 2018
    Posts
    5

    Re: Array issue

    I'm going to try the list idea. The other post you have I'm very confused with.

    I'm just trying something like when the user presses space, all the pictureboxes backcolor changes. Doing something simple just to make sure I can have all the pictureboxes change with a command.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,313

    Re: Array issue

    So you catch the key press, then loop through the array and set the backcolor for each element

  11. #11

    Thread Starter
    New Member DanMnz82's Avatar
    Join Date
    Dec 2018
    Posts
    5

    Re: Array issue

    My problem is trying to get the "Array" for the boxes.

    I want to group 20 pictureboxes and basically define them into 1 group. That way I can just say something like [AllBoxes background color = black] where I'll define AllBoxes to equal picturebox1, picturebox2, picturebox3, etc

    If I try to group them into an array I'm confused with how to make the boxes arrays. To simplify this, I'll use three boxes for now. I'm lost as how these 3 boxes go into the code as one. I can't find a way to make them an array.

    I can say something like [when user clicks button, picbox1 background = black and picbox2 background = black etc] but not sure how to make the number an array variable like picbox(i)

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,313

    Re: Array issue

    The VB6 control arrays that you mentioned don’t exist in VB.Net.
    As I showed you, you can create an array of pictureboxes in code, then set up multi-eventhandlers for each element, also as I showed you. Having an array with a reference to a group of pictureboxes, you can loop through it and apply the same changes to all of the elements...

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,313

    Re: Array issue

    Quote Originally Posted by DanMnz82 View Post
    My problem is trying to get the "Array" for the boxes.

    I want to group 20 pictureboxes and basically define them into 1 group. That way I can just say something like [AllBoxes background color = black] where I'll define AllBoxes to equal picturebox1, picturebox2, picturebox3, etc

    If I try to group them into an array I'm confused with how to make the boxes arrays. To simplify this, I'll use three boxes for now. I'm lost as how these 3 boxes go into the code as one. I can't find a way to make them an array.

    I can say something like [when user clicks button, picbox1 background = black and picbox2 background = black etc] but not sure how to make the number an array variable like picbox(i)

    See post #2

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,313

    Re: Array issue

    Arrays are arrays. They’re used the same as an integer or a string array from VB6. Arrays are zero based in VB.Net

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,148

    Re: Array issue

    You should understand what you can and can't do. Control arrays in VB6 existed for a reason. That reason doesn't exist in VB.NET so neither do VB6-style control arrays. The reason was so that you could handle an event of all the controls with a single procedure. In VB.NET, events are handled differently so control arrays aren't needed, so they don't exist. If you want to have a group of controls that you can access by index they you just create an array in code, just as you would for Integers or Strings or any other type. You've already been shown how to create an array of PictureBoxes so you should stop trying to make VB.NET work the way VB6 did by sheer force of will and just do as you've been shown.

    That said, if it was me, I probably wouldn't bother creating an array at all in this case. You can and it won't hurt - it will even be infinitesimally more efficient at run time - but if what you want is to do the same thing to all PictureBoxes in the same container in one place then you can do that like this:
    vb.net Code:
    1. For Each pb In Me.Controls.OfType(Of PictureBox)()
    2.     'Use pb here.
    3. Next
    That will give you access to each PictureBox added directly to the form. If you want PictureBoxes added to a different container then use the Controls collection of that container. The code can be modified a little to access multiple containers but, if you want a subset of the PictureBoxes in a container, this might not be the best option.

    For the record, if you want to handle the same event for multiple controls in VB.NET then, rather than creating a control array, as you would in VB6, you select all the controls in the designer, open the Properties window, click the Events button and then double-click the desired event. That will generate an event handler and add all the selected controls to the Handles clause. Using the Properties window is far more flexible than the old VB6 way.

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,313

    Re: Array issue

    Quote Originally Posted by jmcilhinney View Post
    For the record, if you want to handle the same event for multiple controls in VB.NET then, rather than creating a control array, as you would in VB6, you select all the controls in the designer, open the Properties window, click the Events button and then double-click the desired event. That will generate an event handler and add all the selected controls to the Handles clause. Using the Properties window is far more flexible than the old VB6 way.
    Or you can do it in code, using AddHandler as i showed you in post #3

  17. #17
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,148

    Re: Array issue

    Quote Originally Posted by .paul. View Post
    Or you can do it in code, using AddHandler as i showed you in post #3
    Yeah, but why would you do that? If you wouldn't use AddHandler rather than double-click a Button or TextBox to auto-generate an event handler, why would you do it in this case? AddHandler must be used with objects created at run time and there are some legitimate reasons to use that rather than WithEvents/Handles for designer-generated controls, e.g. you want to be able to not handle the event at certain times. Other than that though, if you double-click controls to create handlers for default events normally, you should be using the Properties window in the designer or the Navigation Bar in the code window to create handlers for other events.

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,313

    Re: Array issue

    I prefer AddHandler. Just seems neater than a long list in the handles clauses

  19. #19
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,148

    Re: Array issue

    Quote Originally Posted by jmcilhinney View Post
    Yeah, but why would you do that?
    One answer I have heard to that question is that it "automatically" includes any controls you add later in the event handler. I'm not sure who the people are to whom that's an issue but I'm certainly not one of them. If you're creating an array explicitly then you still have to add the new control to the array explicitly, so it's no easier than simply selecting the existing event handler in the Properties window. If you use a method like I suggested, i.e. one that gets all controls in a container, then a new control will be automatically included. I don't really see adding the event handler to the new control to be an onerous task though, so it being done automatically doesn't get me excited. If you're likely to forget to do something like that, I don't trust your software to begin with.

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,313

    Re: Array issue

    Quote Originally Posted by jmcilhinney View Post
    I don't trust your software to begin with.
    You can trust my software 100%...
    Any errors are free of charge

Posting Permissions

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



Click Here to Expand Forum to Full Width