Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rewriting Code

Status
Not open for further replies.

Jay1b

Programmer
Apr 27, 2004
15
GB
Guys

I was hoping someone could please help me. I have a piece of code which works fine, but looks incredibly messy and ideally i'd like to make it easier to reuse.

Code:
        For Each np As Control In NavBar1.Controls
            If TypeOf np Is NavBarPanel Then
                If np.Name = "NavPanelCalendar" Then
                    Dim nsb As NavBarSubButton = np.Controls("subBtnUpcoming")
                    AddHandler nsb.Click, AddressOf frmUpcoming
                End If
            End If
        Next

NavBar1 is a usercontrol, which has a collection of NavBarPanels on it, these are custom controls inheriting from the Panel control. The NavBarPanels have a number of NavBarSubButton's on it, which are also custom controls inheriting from the button control.

I have tried both of the following, but they both come up with 'Object reference not set to an instace of a object'.

Code:
        'Attempt 1
        Dim nsb2 As NavBarSubButton = NavBar1.Controls("NavPanelCalender").Controls("subBtnUpcoming")
        AddHandler nsb2.Click, AddressOf frmUpcoming

        'Attempt 2
        AddHandler (NavBar1.Controls("NavPanelCalender").Controls("subBtnUpcoming")).Click, AddressOf frmUpcoming

As their are several NavBarPanels, the method i have working looks a little untidy and unwieldy!


Also is there a simple way of using delegates via strings? For example:
Code:
AddHandler nsb2.Click, AddressOf frmUpcoming
I would just like to pass the delegate method name into it via code. The delegate name being stored into a database, ideally I would like to store the entire method in a database, but i'm guessing this would be much more complex!



Thanks for any help you may be able to give.
 
First of all, if you have access to the original control's code, I would add click events and then raise them that way instead of trying to add handler's.

However, the AddressOf is supposed to be a method or function that will handle the click event. You don't want to target the click event to the form itself.

An example of proper click event handling is below.

Code:
Public Class Form1

#Region " Private Variables "
    Private btnOne As Button
#End Region

#Region " Private Events "

#Region " Form Events "
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        btnOne = New Button
        btnOne.Location = New Point(20, 20)
        btnOne.Text = "Click Me"
        Me.Controls.Add(btnOne)
        AddHandler btnOne.Click, AddressOf btnOne_Click
    End Sub
#End Region

#Region " Button Click Events "
    Private Sub btnOne_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        MsgBox("Congratulations. You created a dynamic button!")
    End Sub
#End Region

#End Region

End Class
 
Thanks Qamgine, but it doesnt fit into my control as the control will be used on multiple application.

Misterstick, could you please point me in the direction of what part of ther reflection namespace? Its quite big lol!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top