Results 1 to 14 of 14

Thread: Initialize 40 objects ?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2023
    Posts
    28

    Initialize 40 objects ?

    Hello to all.

    I have 20 Goupboxes which have inside 20 PictureBoxes in a Form create within the Designer.

    Which is the quickest way to put a Text in each Groupbox? (The text is inside in myArraylist(1...20) which are variables)
    Also the same is for the Pictureboxes, the images are in the resource file( to reach via my.resouces.image1)

    A little code example will better then words!!!

    Thanks.
    Last edited by marsias; Feb 6th, 2023 at 02:43 AM.

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

    Re: Initialize 40 objects ?

    Code:
    MsgBox(“here’s your code example”)


    To start, why do you have an ArrayList? A Generic List(Of String) is used these days. After that, where do you want to put the text? If you mean the GroupBoxes captions, that’s easy with a loop. Something like this, but I typed it in my phone, so it might need tweaking…

    Code:
    For x As Integer = 0 to 19
        Me.Controls(“GroupBox” & (x+1)).Text = myList(x)
        DirectCast(DirectCast(Me.Controls(“GroupBox” & (x+1)),     GroupBox)).Controls(“PictureBox” & (x+1)), PictureBox).Image = DirectCast(My.Resources.ResourceManager.GetObject(“image” & (x+1)), BitMap)
    Next

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Initialize 40 objects ?

    I don't think there is a 'quickest' way. Or, at least there isn't one that really matters. There may well be a most efficient way, but that may not really solve the problem.

    What is the problem you are having that led you to ask the question? Presumably, you tried something and the performance was not what you hoped for. That would be understandable, considering the number of controls that you are talking about. After all, what you described could be read as 20 PB in each of the 20 GroupBoxes, which would mean 400 PB total, which will bring any system to it's knees. One PB in each of 20 Group Boxes isn't so bad, but loading each with a picture could still be costly.

    If it's the former, then there's a better design. If it's the latter, then there might not be a better design, but there might be a better place to do the setup than how you are doing it. Either way, you might gain more form a different design than from any specific code, but to say anything useful about that, we'd need to know a bit more about how this form gets loaded, and when. For example, you might be better off doing the work in the constructor, but only if certain assumptions are met.
    My usual boring signature: Nothing

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

    Re: Initialize 40 objects ?

    Quote Originally Posted by Shaggy Hiker
    …Presumably, you tried something and the performance was not what you hoped for. costly.
    Looking at the (presumed) level of proficiency of the OP, I think this question was the first port of call

    Quote Originally Posted by Shaggy Hiker
    …One PB in each of 20 Group Boxes isn't so bad, but loading each with a picture could still be costly.
    That’s what I took it to be as there are 40 Controls, 20 of which are GroupBoxes containing PictureBoxes.
    The Form’s constructor would definitely be the place to set the Text of the GroupBoxes and assign My.Resources images to the PictureBoxes.

  5. #5
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: Initialize 40 objects ?

    I have found that you can create 1000's of controls anywhere you want. It is not until you make them visible does it start costing your time.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Initialize 40 objects ?

    That's true. Creating an object costs very little. If those objects have a visible representation...well, then drawing them is going to have a cost. That's especially true for Pictureboxes that are showing images. Lots of drawing going on there.
    My usual boring signature: Nothing

  7. #7
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: Initialize 40 objects ?

    The best tip I can give is to make the Opacity of the source form 0 until the end of your routine and make it 100. It is hands down the easiest thing you can do to prevent the user the spectacle of ugly drawing

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Initialize 40 objects ?

    Quote Originally Posted by vbdotnut View Post
    The best tip I can give is to make the Opacity of the source form 0 until the end of your routine and make it 100. It is hands down the easiest thing you can do to prevent the user the spectacle of ugly drawing
    I would disagree with this from a UX perspective. What I tend to do is create a loading modal to appear on top of the form rendering the controls that need to be created/made visible, this can optionally have a progress bar that gets incremented as the control is created/shown. With the loading modal running, the container that needs to add the controls starts with SuspendLayout, adds the controls, and then finished with ResumeLayout.

    This makes it so that controls are added/shown while giving the user a visual indicator that something is going on behind the scenes.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: Initialize 40 objects ?

    I would disagree with this from a UX perspective.
    either way its 'hackish' clunky smokescreen. Can easily show progress in either method.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Initialize 40 objects ?

    It's certainly hackish, but which is the best approach seems to be situational, in my opinion. Sometimes, it is sufficient to do all the work in the constructor, which works if you can create the form well before you show it. Other times, toggling either the façade screen or the opacity should work, and in one case that I have, the façade screen works...so long as the façade is dropped a second or two AFTER the Shown event of the form.

    So, it's a matter of making it work the way that makes for a good user experience, and that can depend on the situation.
    My usual boring signature: Nothing

  11. #11
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: Initialize 40 objects ?

    The bottom line is this. You can create the controls any way you like. The more narrow you can buffer the display property of the controls is going to make or brake the user experience.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Feb 2023
    Posts
    28

    Re: Initialize 40 objects ?

    Thanks to all,
    First of all : The GroupBoxes of the form are from min 8 to 20 maximum... Total of 40 Controls(with the PictureBoxes inside).
    The amount of the controls "showing" in the Forms is depended of a variable. Maybe i make the not used controls each time not visible

    @.paul

    Thanks for the code, works perfectly( after i corrected the Directcast()... line.
    Last edited by marsias; Feb 7th, 2023 at 04:19 AM.

  13. #13
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,382

    Re: Initialize 40 objects ?

    From the sidelines:
    Each control is an Object/Class, and therefore has a (inherited) constructor.
    Since i'm a Pascal-Guy (and not a Dot-NETer), i'd probably derive my own class(es) for the controls, and override the Constructor, and put the code there.
    No looping through Lists and what not...
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  14. #14
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: Initialize 40 objects ?

    it sounds like you should be creating the controls from the list as they are needed and not referring to existing controls through casting. Going on Pauls example, you should be testing if the object is not nothing before trying to cast it into a gb/pb.

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