Creating new buttons at runtime and making them v
Hi,
I have a little problem I was hoping someone could help me with. I have a user control that I have created, with a split container with two panels splitting vertically (two panels, one on top, one on bottom).
On the top panel, it is occupied by a DataGridView that is fully docked. On the bottom panel, I have a PictureBox control that is also fully docked too.
At runtime, depending on what items I have populated the DataGridView, I want to loop over them, and then on-the-fly, create a single Button control for each row in the DataGridView.
I have a Sub that creates an array of Button controls on the fly that has the following Dim statement:
Dim cmdLocArray(dgvTree.Rows.Count) As Button
As I loop through each row of the grid, I instantiate a new Button object for each element of the array. Then set its properties (size, location, text, etc.) that I want. At the end, I made sure that I set the button to visible.
Here is the code that I have below:
Public Sub PopulatePlacementView()
Dim intX As Integer
Dim cmdLocArray(dgvTree.Rows.Count) As Button
Dim imgLocIcon As Image
Dim strText As String
Dim sngX, sngY As Single
'Load the image first
imgLocIcon = Image.FromFile(AppInfo.InstallPath + "\Icons & Images\location.gif")
For intX = 0 To dgvTree.Rows.Count - 1
cmdLocArray(intX) = New Button
cmdLocArray(intX).Name = "cmdLoc" + CStr(intX)
cmdLocArray(intX).Parent = SplitContainer1.Panel1
cmdLocArray(intX).Size = New Size(150, 50)
sngX = picPlacement.Location.X + (100 * intX)
sngY = picPlacement.Location.Y + (100 * intX)
cmdLocArray(intX).Location = New Point(sngX, sngY)
cmdLocArray(intX).Image = imgLocIcon
cmdLocArray(intX).ImageAlign = ContentAlignment.TopLeft
cmdLocArray(intX).TextAlign = ContentAlignment.TopLeft
cmdLocArray(intX).TextImageRelation = TextImageRelation.ImageBeforeText
strText = "Location: " + dgvTree.Rows(intX).Cells("colLocation").Value
strText = strText + vbCrLf + "Date / Time: " + dgvTree.Rows(intX).Cells("colTimeStamp").Value
strText = strText + vbCrLf + "Reading: " + Trim(dgvTree.Rows(intX).Cells("colData").Value)
strText = strText + vbCrLf + "Status: " + Trim(dgvTree.Rows(intX).Cells("colStatus").Value)
strText = strText + vbCrLf + "Alarm Status: " + Trim(dgvTree.Rows(intX).Cells("colAlarms").Value)
cmdLocArray(intX).Text = strText
cmdLocArray(intX).FlatStyle = FlatStyle.Flat
cmdLocArray(intX).BringToFront()
cmdLocArray(intX).Visible = True
Me.Controls.Add(cmdLocArray(intX))
Next
End Sub
The odd thing here is that it does not generate any errors, but the buttons do not appear on the form. I thought that they might be hidden below the PictureBox control on the bottom panel, but when I call the .BringToFront() on each button within the For loop, nothing happened either.
I'm not sure how to go about doing this. It used to be a lot easier back in VB6 to create a control array, and also be able to trap their events (MouseMove, MouseOver, etc.) too. Is there a much easier way for me to do this? I do not want to endup having to create say 20 buttons at design time, and then having to loop through and unhide them when I need to. It just seems a bit odd that VB2008 does not allow me to do that easily.
I would really appreciate any help you can provide.
Thank you very much for your help.
Khoi Nguyen
__________________
Khoi Nguyen
|