davearia Posted May 13, 2006 Posted May 13, 2006 Hi All, Here is a snippet of some code I have been playing around with this morning. 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. :D :D :D Quote
Cags Posted May 13, 2006 Posted May 13, 2006 You can't dynamically create a method for each one. What you should be doing is creating a generic sub that will deal with all buttons, you will then attach that sub as the event for each one. The assuming you use an overload similar to the standard .Net Framework ones, you can find out exactly which button has called the event, and then filter what todo for that button using either a switch statement or whatever. Quote Anybody looking for a graduate programmer (Midlands, England)?
davearia Posted May 13, 2006 Author Posted May 13, 2006 Thanks for the reply Cags. I understand the logic of what you are saying. But what is the syntax of the solution you described. Could you be so kind as to give me some code as an example? Thanks Dave. :D :D :D Quote
Cags Posted May 13, 2006 Posted May 13, 2006 Well it's very difficult to provide an example of the code without actually knowing what exactly you want the event todo. I can give you an example of attaching an event (in case you don't know how todo this). ' create an event like so Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) ' stuff todo when event fired End Sub ' in your code you attach the event with AddHandler Button1.Click, AddressOf Button1_Click I think it's right, I'm a C# person myself though. Basically what the code does is attaches the sub Button1_Click to the Click event of an object called Button1. Quote Anybody looking for a graduate programmer (Midlands, England)?
davearia Posted May 13, 2006 Author Posted May 13, 2006 Thanks mate, Think you solution is little more straightb forward but I got a fix as well. I got round it another way: I created a userControl that inherited the class Button. I just added the following the new class. Public Sub mySub(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click MessageBox.Show(Me.Name) End Sub It fired ok. Tried your code and that is good too. So as usual there is always more than one way of doing this. Thanks again, Dave. :D :D :D Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.