1

I am making a ToolBar menu UserControl for my project. The ToolBar will have n quantities of buttons based on each situation it undergoes. So for example in certain windows toolbar will have new, edit, delete buttons, but in other it might have upload, download, and print. This is a simple example, it gets more complicated than that.

Here is what i have so far: In the UserControl (ToolBar)

NEW Button
    Public Event btnNew_Click As btnNew_ClickedEventHandler
    Public Delegate Sub btnNew_ClickedEventHandler(sender As Object)
    Private Sub btnNew_MouseUp(sender As Object, e As MouseButtonEventArgs)
        RaiseEvent btnNew_Click(Me)
    End Sub

This code will make this event visible from the Window in xaml

<toolbar btnNew_Click="Code_for_New_Record"/>

So far so good (everything works). But I want to check if my Event is Attached from the MainWindow and if it is not, to hide it.

So for example, if i had buttons for update, delete, print, etc. And the programer for the MainWindow only coded the New Button, only the new Button should show.

Question: How can I tell if the Event is attached or called? I would like to have some code that says

IF myEvent IsNot attached then button.visibility = collapsed end if

Thanks for all your help in advance!

user3524375
  • 86
  • 1
  • 1
  • 8

1 Answers1

0

To check if the window containing your toolbar control has registered to the btnNew_Click event (meaning if an event handler is attached to it) you can just check it for null:

if (btnNew_Click == null)
    // event not attached
else
    // event attached

Note that this is C# code, in VB this would look similar to the following (can not test it right now):

If btnNew_Click Is Nothing Then
    ' Event not attached
Else
    ' event attached
End If

The question is when to call that code. If you want to hide the respective toolbar button completely, you might want to do the null-check within the UserControl's OnLoaded event and set the button's Visibilityto either Visible or Collapsed. However, you can do this whenever you want, even when the App is already running (I implemented a similar Scenario once that did the null-check within the button's MouseOver event and changed the mouse cursor (to Signal that the button cannot be clicked) if the event was not attached.

andreask
  • 4,248
  • 1
  • 20
  • 25