[RESOLVED] [2008] Setting parameters of Panels in an array?
Hey everyone,
I'm exceptionally new to VB.Net and Windows forms, and this has got me stumped.
I have created a tiny little app which has 4 panels.
By default, all panels are invisible.
The list of panel names is stored in an array.
Code:
Dim aryPanels() As System.Windows.Forms.Panel = {Panel1, Panel2, Panel3, Panel4}
From a sub I try to set the parameter 'Visible = True' like so...
Code:
aryPanels(UBound(aryPanels)).Visible = True
or
Code:
aryPanels(1).Visible = True
which in my mind should be the equivalent of this:
Code:
Panel2.Visible = True
The latter works from the sub when it's hardcoded. The first, which is what I need to do, does not.
I'd like to attach the project but can't see where, but if anyone can tell me how I can set parameters of Panels in an array, I'd be hugely grateful! :afrog:
Thanks,
Scott
Re: [2008] Setting parameters of Panels in an array?
First up, the Panel names are not stored in that array. The Panels themselves are the elements of that array, not their names.
Secondly, I'd suggest calling the GetUpperBound method of the array itself rather than the UBound function.
Finally, where exactly is that array declared? There are two possibilities from what you've posted. Either it's declared inside a method, in which case it doesn't exist in any other method, or it's declared at the class level, in which case all the elements will be null references.
That code will work as is, as long as you've created the array properly. You haven't provided enough content for us to know whether that's the case.
Re: [2008] Setting parameters of Panels in an array?
Quote:
First up, the Panel names are not stored in that array. The Panels themselves are the elements of that array, not their names.
Correct you are.
Quote:
Secondly, I'd suggest calling the GetUpperBound method of the array itself rather than the UBound function.
I'd love to! I knew UBound is oldschool but wasn't 100% sure on this new fangle dangle method. I'll work on it. Think I've got that sorted though the old method seems to make more sense to me (at the minute)
Quote:
Finally, where exactly is that array declared? There are two possibilities from what you've posted. Either it's declared inside a method, in which case it doesn't exist in any other method, or it's declared at the class level, in which case all the elements will be null references.
The array is declared at the class level. I think you're right, the elements are null references. I don't know of any other way to do it at the minute!
I've zipped up the project, it's only tiny, you can check it out here:
http://www.box.net/shared/w8pxv846c0
Once you open it up, you'll see that it's a beginnings of a UI for a touchscreen. When you hover your cursor over one corner then over another corner (as though you've swiped your finger from the top right hand corner to the bottom right hand corner, for instance)...it should, in this proprietary stage, make one of the 4 invisible panels, visible. It'll all make sense once you've seen it first hand.
I appreciate your help jmcilhinney. Glad to see someone knows what they're on about, especially an Australian (I'm in Australia too). Cheers, Scott.
Re: [2008] Setting parameters of Panels in an array?
All elements of that array are null references because when that code is executed the panels themselves haven't been created. Controls added to the form designer are created in the InitializeComponent method, which is called from the constructor. That means that you cannot create your array until AFTER the InitializeComponent method has been called. As such you would need to create your array at the end of the constructor or, more likely, in the Load event handler. You still declare the array variable at the class level but you don't create the array object and assign it to the variable until later.
Also, I suggest that you don't use Dim on member variables. You should always specify an access modifier explicitly. If you want it to be private then use Private. If you want it to be public then use Public.
Re: [RESOLVED] [2008] Setting parameters of Panels in an array?