• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 71
  • Last Modified:

Sharing Events?

I have attached a screenshot that shows a TabControl along with several groupbox controls. Each groupbox is comprised of multiple controls. These controls make up a UserControl. Each of the tabpages, excluding the "Equipment Selection" tab are laid out exactly the same. Each control is named according to the tabpage it resides on; example - usrChannelAngleLoadTest, usrChannelAngleRepeatabilityTest, usrChannelAngleCreepTest, etc.

Each of these controls have public events defined in the UC code-behind. Here is an example of these events.

    Private Sub usrLoadSettingsCreep_GetUnitType() Handles usrLoadSettingsCreep.GetUnitType
        Try
            EH.ErrorMessage = String.Empty

            If usrLoadSettingsCreep.cmbLoadUnitType.SelectedIndex <> -1 Then
                If gblnLoadUnitTypeSelectedCreep Then
                    gblnLoadUnitTypeSelectedCreep = False

                    RemoveHandler usrLoadSettingsCreep.cmbLoadUnit.SelectedIndexChanged, AddressOf usrLoadSettingsCreep.cmbLoadUnit_SelectedIndexChanged
                    LoadCombo_LoadUnits(usrLoadSettingsCreep.cmbLoadUnitType.SelectedValue, usrLoadSettingsCreep.cmbLoadUnit)
                    AddHandler usrLoadSettingsCreep.cmbLoadUnit.SelectedIndexChanged, AddressOf usrLoadSettingsCreep.cmbLoadUnit_SelectedIndexChanged
                End If
            End If

        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/usrLoadSettingsCreep_GetUnitType() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub
    '
    '
    '
    Private Sub usrLoadSettingsRepeat_GetUnitType() Handles usrLoadSettingsRepeat.GetUnitType
        Try
            EH.ErrorMessage = String.Empty

            If usrLoadSettingsRepeat.cmbLoadUnitType.SelectedIndex <> -1 Then
                If gblnLoadUnitTypeSelectedRepeat Then
                    gblnLoadUnitTypeSelectedRepeat = False

                    RemoveHandler usrLoadSettingsRepeat.cmbLoadUnit.SelectedIndexChanged, AddressOf usrLoadSettingsRepeat.cmbLoadUnit_SelectedIndexChanged
                    LoadCombo_LoadUnits(usrLoadSettingsRepeat.cmbLoadUnitType.SelectedValue, usrLoadSettingsRepeat.cmbLoadUnit)
                    AddHandler usrLoadSettingsRepeat.cmbLoadUnit.SelectedIndexChanged, AddressOf usrLoadSettingsRepeat.cmbLoadUnit_SelectedIndexChanged
                End If
            End If

        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/usrLoadSettingsRepeat_GetUnitType() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub
    '
    '
    '
    Private Sub usrLoadSettingsLoad_GetUnitType() Handles usrLoadSettingsLoad.GetUnitType
        Try
            EH.ErrorMessage = String.Empty

            If usrLoadSettingsLoad.cmbLoadUnitType.SelectedIndex <> -1 Then
                If gblnLoadUnitTypeSelectedLoad Then
                    gblnLoadUnitTypeSelectedLoad = False

                    RemoveHandler usrLoadSettingsLoad.cmbLoadUnit.SelectedIndexChanged, AddressOf usrLoadSettingsLoad.cmbLoadUnit_SelectedIndexChanged
                    LoadCombo_LoadUnits(usrLoadSettingsLoad.cmbLoadUnitType.SelectedValue, usrLoadSettingsLoad.cmbLoadUnit)
                    AddHandler usrLoadSettingsLoad.cmbLoadUnit.SelectedIndexChanged, AddressOf usrLoadSettingsLoad.cmbLoadUnit_SelectedIndexChanged
                End If
            End If

        Catch ex As Exception

        End Try
    End Sub

Open in new window


My question is this!

Is there any way of consolidating these three events into one event where I can just pass in the UC depending on which one is being called at the time? Basically having all three events share one event!

Thanks
Screenshot.jpg
0
BlakeMcKenna
Asked:
BlakeMcKenna
  • 3
  • 2
1 Solution
 
Fernando SotoRetiredCommented:
Change the Handles on one of the events to this

Handles usrLoadSettingsCreep.GetUnitType, usrLoadSettingsRepeat.GetUnitType, ...

Then delete the other events. Inside the event that remains you need to find out which event triggered the event.
0
 
BlakeMcKennaAuthor Commented:
Fernando,

Using your idea, how can pass a string variable into that event that is specific to the "Load", "Creep" or "Repeat"? What needs to be modified in all the UC's?

Thanks!
0
 
Fernando SotoRetiredCommented:
Hi BlakeMcKenna;

Not sure if these are regular event handlers but that are more likely home grown because they have no parameters. So if that is the case please have a look at this web page Handling and Raising Events. When raising the event you apply two parameters the Me object and a EventArgs. This way when the event is handled by your code you get a sender and a event. Then you can cast the sender to a control and you will be able to tell who raised the event.
0
 
BlakeMcKennaAuthor Commented:
Thanks Fernando...that helped!
0
 
Fernando SotoRetiredCommented:
Not a problem BlakeMcKenna, glad I was able to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now