|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Event handler delegate as variableI have a program which creates buttons on a form on loading from data in a database table and connects them to an event handler as follows... btn = New Button btn.Text = dr("buttontext").ToString btn.Visible = True btn.Dock = DockStyle.Fill btn.BackColor = Color.FromKnownColor(KnownColor.InactiveCaptionText) btn.Name = dr("buttonname").ToString If dr("AddressOf") = "historyclick" Then AddHandler btn.Click, AddressOf historyClick ElseIf dr("AddressOf") = "pricelevelclick" Then AddHandler btn.Click, AddressOf pricelevelClick ElseIf dr("AddressOf") = "keyboardsclick" Then AddHandler btn.Click, AddressOf keyboardsClick end if Is there any way to use a variable to link to the event handler delegate instead of comparing the db field value as in the above code e.g AddHandler btn.Click, AddressOf dr("AddressOf") (This doesn't work obviously) Regards Steve Yes, it's possible using reflection, but IMHO far too compex for
something trivial like this. You can use a Select Case statement to make your code a bit more managable. Something like this: Select Case dr("AddressOf") Case "historyclick": AddHandler btn.Click, AddressOf historyClick Case "pricelevelclick": AddHandler btn.Click, AddressOf pricelevelClick Case "keyboardsclick": AddHandler btn.Click, AddressOf keyboardsClick End Select steve wrote: Show quoteHide quote > Hi All > > I have a program which creates buttons on a form on loading from data in a > database table and connects them to an event handler as follows... > > btn = New Button > > btn.Text = dr("buttontext").ToString > > btn.Visible = True > > btn.Dock = DockStyle.Fill > > btn.BackColor = Color.FromKnownColor(KnownColor.InactiveCaptionText) > > btn.Name = dr("buttonname").ToString > > If dr("AddressOf") = "historyclick" Then > > AddHandler btn.Click, AddressOf historyClick > > ElseIf dr("AddressOf") = "pricelevelclick" Then > > AddHandler btn.Click, AddressOf pricelevelClick > > ElseIf dr("AddressOf") = "keyboardsclick" Then > > AddHandler btn.Click, AddressOf keyboardsClick > > end if > > Is there any way to use a variable to link to the event handler delegate > instead of comparing the db field value as in the above code > > e.g AddHandler btn.Click, AddressOf dr("AddressOf") (This doesn't work > obviously) > > > > Regards > > Steve > >
VB2005 Pro Edition--Data Sources window is disabled when viewing forms
Adding a Web Reference changes DataTypes ??? Multithreading Problem Traversing a Collection Regular Expression Accessing one dimension of a multidimensional array Desperately needing direction with EncoderParameters and saving images under different compression/q Maximum Form Height? problem opening Excel file with Shell Displaying copied file names in textbox |
|||||||||||||||||||||||