0

I'm new to VB.net so please understand. I have a MainWindow, Frame1, Frame2 (AdFrame1), Page1.

MainWindow loads Page1 from Frame1 on startup. In MainWindow.vb class I have a Public Sub that makes Frame2 visible.

How would I share that Public Sub so I could use Page_Mouseup Event to show Frame2 (AdFrame1)

I tried Public Shared Sub, but I get this error: "Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class"

Code in MainWindow.vb:

Private Sub MainWindow2_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseUp


        AdClick()

    End Sub



    Public Sub AdClick()



        Try


            clicks += 1

            If clicks >= 10 Then

                'After 5 seconds Adframe CloseLink and TextBlock1 will hide.
                AdFrame1.Visibility = Windows.Visibility.Visible
                CloseMainButton.Visibility = Windows.Visibility.Visible
                InitializeComponent()
                dpTimer = New DispatcherTimer
                dpTimer.Interval = TimeSpan.FromMilliseconds(10000)
                AddHandler dpTimer.Tick, AddressOf TickMe
                dpTimer.Start()

                clicks = 0

            End If
        Catch ex As Exception
            MessageBox.Show("Oops! Error X0123A1. Please contact us with error ASAP!", "Error!", MessageBoxButton.OK)
        End Try
    End Sub


    'After 10 sesonds auto close
    Private Sub TickMe()

        AdFrame1.Visibility = Windows.Visibility.Hidden

    End Sub

Code Page1.vb

  Private Sub Page1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseUp


            AdClick() (inaccessible due to protection)

        End Sub

So how would share AdClick() with Page1

J...
  • 30,968
  • 6
  • 66
  • 143
Benjamin Jones
  • 987
  • 4
  • 22
  • 50

1 Answers1

3

First, note that the Shared keyword is not related to visibility - members can be Public Shared or Private Shared, for example. A Shared member is the same as a static member in C# - it is common to all instances of a class (see : What is the use of a shared variable in VB.NET?)

For the actual question, I'd suggest just hooking up the event that is already there :

Class MainWindow2
    Private WithEvents _Page1 as New Page1

    Private Sub MainWindow2_MouseUp(sender As Object, e As MouseEventArgs) _
                                            Handles Me.MouseUp, _Page1.MouseUp 
        AdClick()
    End Sub

    Private Sub AdClick()
         'Do something
    End Sub
End Class

Exposing methods is usually the wrong approach - if you want MainWindow to do something it is far better that it remains responsible for doing it itself, not having some other class trying to do it elsewhere. With events, Page1 is only responsible for letting MainWindow know that it has been clicked; this leaves it up to MainWindow to decide what to do about it and Page1 needs know nothing about the methods that are contained in MainWindow.

You can even add custom events - if you had a button on Page1, for example :

Class Page1
    'make your own event
    Public Event Button1Click(sender As Object, e As RoutedEventArgs)

    Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) _
                                                         Handles Button1.Click
        RaiseEvent Button1Click(sender, e)
    End Sub
End Class

and then in MainWindow :

Private Sub ClickHandle(sender As Object, e As RoutedEventArgs) _
                                                   Handles _Page1.Button1Click
    AdClick()
End Sub

This would let a button click inside the page be propagated up to the main window.

If your pages are created at runtime you cannot use WithEvents and Handles but you can do :

Dim newPage as New Page1
AddHandler newPage.MouseUp, AddressOf MainWindow2_MouseUp

Just make sure to remove the handler before the page goes out of scope when you are finished with it (Handlers keep a strong reference so newPage will never get garbage collected with a handler still attached! --> memory leak).

RemoveHandler newPage.MouseUp, AddressOf MainWindow2_MouseUp
Community
  • 1
  • 1
J...
  • 30,968
  • 6
  • 66
  • 143