# How to write event handler in VB?

**URL:** <https://tech-community.robotics.abb.com/t/how-to-write-event-handler-in-vb/3218>\
**Category:** RobotStudio\
**Created:** [November 27, 2009, 1:51pm UTC](https://tech-community.robotics.abb.com/t/how-to-write-event-handler-in-vb/3218 "2009-11-27T13:51:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@Kevin](https://tech-community.robotics.abb.com/u/Kevin)\
**Post date:** [November 27, 2009, 1:51pm UTC](https://tech-community.robotics.abb.com/t/how-to-write-event-handler-in-vb/3218/1 "2009-11-27T13:51:30Z")

</div>

My VB add-in needs to detect when a new selection is made in the graphics window.

I think I have to link my subroutine to to the GraphicPicker.GraphicPick event, but cannot work out how to do this from the API Help.

Would somebody please give me some example code.

Thanks,

Kevin

---

<div class="post-metadata">

**Author:** ![Henrik\_Berlin](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/henrik_berlin/32/1011_2.png) [@Henrik\_Berlin](https://tech-community.robotics.abb.com/u/Henrik_Berlin)\
**Post date:** [November 27, 2009, 4:34pm UTC](https://tech-community.robotics.abb.com/t/how-to-write-event-handler-in-vb/3218/2 "2009-11-27T16:34:06Z")

</div>

The following topic discusses event handling in C#. Perhaps that can give some guidance.

[http://forums.robotstudio.com/forum\_posts.asp?TID=3475](http://forums.robotstudio.com/forum_posts.asp?TID=3475)

---

<div class="post-metadata">

**Author:** ![Henrik\_Berlin](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/henrik_berlin/32/1011_2.png) [@Henrik\_Berlin](https://tech-community.robotics.abb.com/u/Henrik_Berlin)\
**Post date:** [November 27, 2009, 4:35pm UTC](https://tech-community.robotics.abb.com/t/how-to-write-event-handler-in-vb/3218/3 "2009-11-27T16:35:00Z")

</div>

… btw … It think this is the event to use

```auto
Public Event SelectionChanged As [EventHandler](http://msdn2.microsoft.com/en-us/library/xhb70ccc)

```

---

<div class="post-metadata">

**Author:** ![Kevin](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@Kevin](https://tech-community.robotics.abb.com/u/Kevin)\
**Post date:** [December 1, 2009, 5:32pm UTC](https://tech-community.robotics.abb.com/t/how-to-write-event-handler-in-vb/3218/4 "2009-12-01T17:32:55Z")

</div>

Thanks for your reply Henrik,

The API Help shows ‘SelectionChanged’ associated only with ‘ToolControlBase’, ‘CommandBarComboBox’ and ‘ReferenceComboBox’ but states that the ‘GraphicPicker.GraphicPick’ event … “occurs when the user picks an object in a graphic view”, which I think is what I want.

I have converted the C# example from your link to VB, but after much experimenting I still have not managed to write a working handler and link it to my required event.

I would be very grateful if someone would give a detailed example of how to do this in VB.

Thanks,

Kevin

---

<div class="post-metadata">

**Author:** ![Kevin](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@Kevin](https://tech-community.robotics.abb.com/u/Kevin)\
**Post date:** [December 2, 2009, 1:43pm UTC](https://tech-community.robotics.abb.com/t/how-to-write-event-handler-in-vb/3218/5 "2009-12-02T13:43:26Z")

</div>

OK, I sorted it. For the benefit of anyone else who is struggling with event handlers in VB, here is an example which displays a message box whenever a face or body are selected.

Add a reference to ‘ABB.Robotics.RobotStudio.Stations.Forms’ in the form or module properties then include this line at the start of the form/module code:

```auto
Imports ABB.Robotics.RobotStudio.Stations.Forms
```

Add this code so that it executes before a graphic selection will be made:

```auto
AddHandler GraphicPicker.GraphicPick, AddressOf MySub_SelectionChanged
```

Then add the handler subroutine:

` Public Sub MySub_SelectionChanged(ByVal sender As Object, ByVal e As GraphicPickEventArgs)
If e.PickedObject IsNot Nothing Then
If e.PickedObject.TypeDisplayName = "Face" Then
MsgBox("Face selected")
ElseIf e.PickedObject.TypeDisplayName = "Body" Then
MsgBox("Body selected")
End If
End If
End Sub
`

Kevin
