Grid of controls

T

Tom

I need to make up a 'grid' of controls at run time. The controls (of which I
wrote) I will instantiate, then I need to arrange these in a row/column
layout. For example, let's say I instantiate 20 of the controls; then I want
to position them in a grid-like display (within a Windows Form) as 5 rows of
4 controls each. If the 'grid' doesn't fit within the frame of where it is
being generated then scroll bars should appear so that the user can 'scroll'
up and down or sideways to see the controls.

Has anyone done this kind of thing, and has some example code? Yes, I can
write all the code myself, but it is going to be complicated and I don't
really want to reinvent the wheel. This would be REALLY easy if somehow I
could use the grid control itself, and place each of my run-time generated
controls within each cell of the grid (thereby using the grid's row/column
format and scrolling capabilities); however, I have tried this and it
doesn't work as expected. Moreover, I've been told that it can't do this.

Any ideas? Thanks in advance.

Tom
 
F

Fergus Cooney

Hi Tom,

Grids are <much> easier than you imagine.
This is roughly what you want.

Have your controls in an array or ArrayList.

NumRows = <some number>
NumCols = Array.length / NumRows (or ArrayList.count)
dX = <some distance apart>
dY = <some distance apart>
StartX = <some position>
Y = <some position>
For RowNum = 0 to NumRows - 1
X = StartX
For ColNum = 0 To NumCols - 1
P As Point = X, Y
I = RowNum * NumCols + ColNum
ControlArray (I).Location = P
X = X + dX
Next
Y = Y + dY
Next

Set the Form's AutoScroll Property to True

Regards
Fergus
 
T

Tom

Fergus: Hmm.... this sure may work. My question is: What is the
'ControlArray (I).Location'? Is this the actual array element that contains
the control (I am storing mine in an ArrayList)? And once the controls are
in an array list, how to you get them to 'show' on the screen/form?
 
F

Fergus Cooney

Hi Tom.

Every Control has a Location. Stick a Control into an array and it <still>
has a Location. That Location can be accessed by indexing the array (as can
all the other properties).

Eg.
oListBox = make a new new list box.

Dim alArrayList As New ArrayList.
alArrayList.Add (oListBox)

alArrayList (0).Location 'Same as oListBox.Location

When you create a Control programmatically, you must place it on the Form.
If you are unsure of how that works, the best way to learn is to create a
blank Form, drop some Controls on it and examine all the code in the Form. You
must then recreate that behaviour for your own Controls.

Regards,
Fergus
 
T

Tom

Fergus: Thanks... I do understand that (I've been doing VB programming for
8+ years). What was throwing me was your use of the word 'ControlArray'...
plus the fact that when I referenced my control in the ArrayList it wasn't
showing up any intellisense - all I got was 'GetType' and that was it. So
what I did was (in pseudocode):

Dim X as MyControl

Dim al as New ArrayList
al.Add(x)

dim Z as MyControl
z=al(0)
z.Location=...
z.Text=... etc

If you just put in al(0) you get no intellisense - sure, you can still use
Location, but it doesn't prompt you anymore. And since my control (in this
case MyControl) is an internally developed control, it has a lot of 'extra'
parameters and it's too hard remembering what those are without the
intellisense.

Anyway, I think if I now work on the placement code I can figure this out.
Thanks a bunch for your help! :)

Tom
 
F

Fergus Cooney

Hi Tom,

|| I've been doing VB programming for 8+ years

Lol, oops, I got a bit too simplified there!! But I'm with you now - it's
the .NET stuff that's throwing you.

An ArrayList contains items of type Object. So when you access an
ArrayList item, intellisense says "it's an Object - what can I show for
Objects? - which is not a lot". To use an ArrayList item you must cast it into
what <you> already know it is, but VB doesn't. It's a bit of a pain in the bum
sometimes but necessary.

Your solution of using an intermediate variable is better anyway, for not
only do you get the intellisense back, you also don't have to keep referring
to the ArrayList for each of the properties thatt you're setting.

Regards,
Fergus
 
T

Tom

Fergus: Yep, it now works like a champ.... Well, almost. Basically, I put a
panel in my form, then dynamically instantiate the controls and place them
into a 'grid' format on that panel. I have the AutoScroll turned on and the
scroll works great - except for the 'grid' column/row headings. I ended up
putting them (the column/row heading labels) in panels also within the
scrollable panel. That works, except THEY scroll as well - off the screen.
For instance, I would like to have the row (left) headings stay on the
screen even if you scroll all the way to the right; and the top (column)
headings to 'stay' even if you scroll all the way to the bottom - i.e.
LOCKED column and row headings.

I suppose I could put them above and to the left of the panel, but then I
would have to somehow 'detect' that a scroll has occured and then scroll
those headings myself. Except I don't see any event anywhere in the panel
that gets fired when an autoscroll occurs.

I just wish there were an easy way to get these controls into a 'real'
grid - again, it would make this kind of thing a lot easier to handle since
the headings could be locked.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top