Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, June 8, 2008 5:54 AM | 1 vote
I want to add controls to a form in code. I was hoping I could set up the Table Layout Panel with three columns. The hope would be to have a form that looks like this below. I know I can set my GrowthStyle to "AddRows" so that my controls are added to new rows. How do my controls know what columns to be added to when using the Add Method? The idea being that I would add controls to column1 first then columns2, and then finally column 3.
Column1 Column2 Column3
Label #1 Combobox#1 Button
Label #2 Combobox#1
Label #3 Combobox#1 TextBox
Label #4 Combobox#1
Label #5 Combobox#1
Thanks
EM
All replies (5)
Sunday, June 8, 2008 11:04 AM âś…Answered | 2 votes
Dim tlp As New TableLayoutPanel |
tlp.ColumnCount = 3 |
tlp.RowCount = 5 |
tlp.AutoSize = True |
Dim labels(4) As Label |
For i As Byte = 0 To 4 |
labels(i) = New Label |
labels(i).Text = "Label number " & i |
tlp.Controls.Add(labels(i), 0, i) |
Next i |
Dim comboboxes(4) As ComboBox |
For i As Byte = 0 To 4 |
comboboxes(i) = New ComboBox |
comboboxes(i).Items.AddRange _ |
(New String() {"first item", "second item", "item number 3"}) |
tlp.Controls.Add(comboboxes(i), 1, i) |
Next i |
Dim firstRowButton As New Button |
firstRowButton.Text = "New button!" |
tlp.Controls.Add(firstRowButton, 2, 0) |
Dim thirdRowTextBox As New TextBox |
tlp.Controls.Add(thirdRowTextBox, 2, 2) |
Me.Controls.Add(tlp) |
Saturday, June 14, 2008 3:26 AM
So just out of curiosity if I wanted to add these controls to a control collection I am assuming that I can do the following:
Public CntrlCollection As Collection
Dim labels(4) As Label
For i As Byte = 0 To 4
labels(i) = New Label
labels(i).Text = "Label number " & i
tlp.Controls.Add(labels(i), 0, i)
CntrlCollection.Add(labels(i))
Next i
However when I try this I get an Null Reference Exception ("Object reference not set to an instance of an object.") on the line of code tha says "CntrlCollection.Add(labels(i))"
Lastly my goal is to use the .Show method on the collection so that I can show all the controls. However I the intellisense does not automatically pull up a .Show Method after CntrlCollection.
Do I have to dimension my collection differently?
Thanks
EM
Saturday, June 14, 2008 3:34 AM
The Null Reference is referring to CntrlCollection. You probably need to declare it
*Public CntrlCollection As New Collection
*What isn't clear to me is the purpose of CntrlCollection. When you add the labels to the TLP, they are already becoming members of the collection TLP.Controls.
Adding them to the TLP also displays them, so you don't need to call .Show on anything.
Saturday, June 14, 2008 1:56 PM
Yes I see your point. What I want to do is create a group of controls which I can turn on and off in a Row Enter event in a DataGridView. The code above would be an example of one group of controls. The way the code was originally written, the controls all show. But I want to be able to add mutitple groups of controls to the same table layout panel and turn them on and off. I was hoping to add them to the control collection and the use a Show Method on the collection. The controls currently show when the form loads. But I cannot get the controls to show again when I choose a different row and then come back to the row in question in the DataGridView (i.e. DataGridView1_RowEnter event).
Public CntrlCollection As New Collection
Dim labels(4) As Label
For i As Byte = 0 To 4
labels(i) = New Label
labels(i).Text = "Label number " & i
tlp.Controls.Add(labels(i), 0, i)
CntrlCollection.Add(labels(i))
Next i
Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
Dim SelectedRow As DataGridViewRow
Dim cntrlcounter As Double
SelectedRow = GetGridRow(e.RowIndex, DataGridView1)
'Clear existing controls on tablelayoutpanel
TableLayoutPanel1.Controls.Clear()
Select Case SelectedRow.Index
Case Is = 0
For cntrlcounter = 1 To CntrlCollection.Count
Audit1CntrlCollection(cntrlcounter).Show()
Next
Case Is = 1
'Do nothing
End Select
Thanks
EM
Monday, September 15, 2008 3:37 PM
ExcelMonkey said:
Yes I see your point. What I want to do is create a group of controls which I can turn on and off in a Row Enter event in a DataGridView. The code above would be an example of one group of controls. The way the code was originally written, the controls all show. But I want to be able to add mutitple groups of controls to the same table layout panel and turn them on and off. I was hoping to add them to the control collection and the use a Show Method on the collection. The controls currently show when the form loads. But I cannot get the controls to show again when I choose a different row and then come back to the row in question in the DataGridView (i.e. DataGridView1_RowEnter event).
To add multiple groups of controls, I suggest that you create a userControl and add it to the row/column you want your group of control to be in.
Just in case, you don't know what's a userControl, it will allow you to create an object (or group if you prefer) with more than one control in it. Then, after you build your project, you should see the userControl appears in your toolbox. Pick it from there and add it in the tableLayoutPanel row/column you want your "group of controls" to be in.
Hope it will helps you.