Reputation: 189
I'm writing an application in VB.NET and face the following issue. I'm trying to create an interface that will allow the user to select their availability given a range of times and the days of the week. I want to create checkbox controls in a tabular form with two indices that I can use to refer to a certain time range and a day of the week. The table looks something like this:
Sunday Monday Tuesday Wednesday Thursday Friday Saturday 1:00am [x] [x] [x] [x] [x] [x] [x] 2:00am [x] [x] [x] [x] [x] [x] [x] 3:00am [x] [x] [x] [x] [x] [x] [x] 4:00am [x] [x] [x] [x] [x] [x] [x]
... and so forth. So Sunday @ 1:00am would be 0,1 - Monday @ 1:00am would be 0,2 - etc.
I followed the article found here which creates and exposes a control array, however it is not multi-dimensional.
Does anyone know of a similar way to dynamically generate controls and store them in a multi-dimensional array in VB.NET?
Thanks in advance!
Upvotes: 2
Views: 1115
Reputation: 936
You may want to to consider using a TableLayoutPanel in your form.
One method you could use is to create the individual controls, either in the designer if it a fixed size grid, or in the program code for a variable size grid, and place them in (or add them to) the grid cells.
If you need to handle the click events, you can tie the events for all the controls to a single handler (described in the article you linked to). Then, set the tag property of each control to reflect the grid location. For example, if your grid is less than ten by ten, set the tag using an integer with the column in the tens digit and the row in the ones place. In the handler routine, tou can tell which control was clicked using the sender.tag property.
--- After further reading, It appears that you could use the TableLayoutPanel GetPositionFromControl method to get the to determine the position of the control that fired the event ie: tableName.GetPositionFromControl(sender). I'm going to try that in my code in later, maybe I can get rid of the tags.
If you don't need to handle events, you can use the TablelayoutPanel GetControlFromLocation method to access the individual controls.
So, in effect, you can use the TableLayoutPanel as your two dimensional control array without having to code your own array.
Upvotes: 1
Reputation: 941277
A control is an expensive object. You've got too many here, painting the form will start to get noticeably slower. Use a DataGridView instead, add columns of type DataGridViewCheckBoxColumn.
Upvotes: 3