Hi All,
Here is a snippet of some code I have been playing around with this morning.
Basically this code is part of a user control. Based on what the dataset dsAnswers contains each tab page will dynamically create a label, a comboBox and a button for each row in the dataset. All of which works perfectly.
The problem I have is creating/accessing events. For example where I have put the comment 'How do I create a event handler for the click event of this button in the code, how would I create this event for each button?
Any help is greatly appreciated.
Thanks, Dave.
Here is a snippet of some code I have been playing around with this morning.
Visual Basic:
Private Sub designIndividualTabPages(ByRef TabPage1 As TabPage, ByVal dsQuestions As DataSet, ByVal dsAnswers As DataSet)
Dim counter As Int32
Dim xCoordForLabel As Int32 = 10
Dim yCoord As Int32 = 10
Dim xCoordForComboBox As Int32 = 150
If dsQuestions.Tables.Count > 0 Then
If dsQuestions.Tables(0).Rows.Count > 0 Then
Try
For counter = 0 To (Convert.ToInt32(dsQuestions.Tables(0).Rows.Count - 1))
Dim lbl1 As New System.Windows.Forms.Label
lbl1.Location = New System.Drawing.Point(xCoordForLabel, yCoord)
lbl1.TabIndex = counter
lbl1.Text = Convert.ToString(dsQuestions.Tables(0).Rows(counter).Item(0))
lbl1.Name = "label" + Convert.ToString(counter)
TabPage1.Controls.Add(lbl1)
Dim ComboBox1 As New System.Windows.Forms.ComboBox
ComboBox1.Location = New System.Drawing.Point(xCoordForComboBox, yCoord)
ComboBox1.TabIndex = counter
ComboBox1.Name = "cbo" + Convert.ToString(counter)
ComboBox1.Size = New System.Drawing.Size(121, 21)
populateComboBox(ComboBox1, dsAnswers)
TabPage1.Controls.Add(ComboBox1)
yCoord += 30
Next
Dim Button1 As New System.Windows.Forms.Button
Button1.Name = "btn" + Convert.ToString(counter)
Button1.Location = New System.Drawing.Point(100, 140)
Button1.Text = "Submit"
'How do I create a event handler for the click event of this button
TabPage1.Controls.Add(Button1)
Catch ex As Exception
MessageBox.Show("Error whilst putting controls on tab page")
End Try
End If
End If
End Sub
Basically this code is part of a user control. Based on what the dataset dsAnswers contains each tab page will dynamically create a label, a comboBox and a button for each row in the dataset. All of which works perfectly.
The problem I have is creating/accessing events. For example where I have put the comment 'How do I create a event handler for the click event of this button in the code, how would I create this event for each button?
Any help is greatly appreciated.
Thanks, Dave.