Re: Initialize 40 objects ?
Code:
MsgBox(“here’s your code example”)
:D
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
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.
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.
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.
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.
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
Re: Initialize 40 objects ?
Quote:
Originally Posted by
vbdotnut
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.
Re: Initialize 40 objects ?
Quote:
I would disagree with this from a UX perspective.
either way its 'hackish' clunky smokescreen. Can easily show progress in either method.
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.
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.
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.
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...
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.