# Visual Basic > Universal Windows Platform and Modern Windows Experience >  UWP - Back Button Problems with Pivot Tables

## QuattroDave

Afternoon all,

I've got a bit of an odd problem that I can't seem to get to the bottom off.  The back button works perfectly unless I'm in a Pivot table, where it will jump clean out of the pivot tabel and back to the previous page.  What I would like to happen is to go back to the previous page or previous pivot table page... Any Idea how I might be able to achieve this...?

Thanks

Dave

----------


## Poppa Mintin

Afternoon Dave,

I reckon you're going to have to show us what you're doing before we can offer any help.

We need to at least see the code where the problem is.


Poppa.

----------


## QuattroDave

I thought you might ask that...

This code makes the back button work:

App.xaml.vb


```
'The Event Handler:
AddHandler SystemNavigationManager.GetForCurrentView().BackRequested, AddressOf OnBackRequested

'The Procedure that does the work:
Private Sub OnBackRequested(sender As Object, e As BackRequestedEventArgs)
        Dim rootFrame As Frame = TryCast(Window.Current.Content, Frame)
        If rootFrame.CanGoBack Then
            e.Handled = True
            rootFrame.GoBack()
        End If
End Sub
```

This is the code that goes to a previous pivot page

BookingPage.XAML.VB



```
Private Sub PreviousPivotPage()
        If rootpivot.SelectedIndex >= 1 And rootpivot.SelectedIndex <= rootpivot.Items.Count Then
            rootpivot.SelectedIndex = rootpivot.SelectedIndex - 1
        End If
End Sub
```

Edit: Just noticed I posted in the wrong forum, I think it should be in 'Modern Windows Experience', if you want to move it pls feel free...

----------


## si_the_geek

I think this will do what you want (assuming rootpivot is in scope):


```
Private Sub OnBackRequested(sender As Object, e As BackRequestedEventArgs)
        If rootpivot.SelectedIndex >= 1 And rootpivot.SelectedIndex <= rootpivot.Items.Count Then
            rootpivot.SelectedIndex = rootpivot.SelectedIndex - 1
        Else
            Dim rootFrame As Frame = TryCast(Window.Current.Content, Frame)
            If rootFrame.CanGoBack Then
                e.Handled = True
                rootFrame.GoBack()
            End If
        End If
End Sub
```





> Edit: Just noticed I posted in the wrong forum, I think it should be in 'Modern Windows Experience', if you want to move it pls feel free...


Moved  :Smilie:

----------


## QuattroDave

Hi Si,

Thanks for the reply & thanks for moving the post.  Thats the problem, the pivot table isn't in the scope of 'App.xaml.vb' really not sure how to get around this.  Can I expand the scope or am I able to override the handeling of the back button...?

Thanks

Dave

----------


## si_the_geek

I've only briefly looked into UWP, so I'm not sure of how it all works.

Assuming that the pivot table is an object on the page, you might be able to declare a public variable of the same data type, then inside the page with the pivot table assign that pivot table to the variable - which you can then use in the Back routine.

----------


## QuattroDave

Thanks for your reply,

I'm sturggeling to understand how that would work and how I'd code it, have you got any examples you can point me to?

I also came accross this in C# a little earlier but don't seem to be able to translate it to VB, any ideas?
https://stackoverflow.com/questions/...set-in-appx-cs

Many thanks

Dave

----------


## QuattroDave

After a bit more reading and head scratching I think this is exactly what I need:

https://stackoverflow.com/questions/...set-in-appx-cs
In C#

App.xaml.cs:



```
public event EventHandler<BackRequestedEventArgs> BackRequested;

private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
// Raise child event
var eventHandler = this.BackRequested;

if (eventHandler != null)
{
eventHandler(sender, e);
}

if (!e.Handled)
{
if (_rootFrame != null && _rootFrame.CanGoBack)
{
e.Handled = true;
_rootFrame.GoBack();

}
}
}
```

Then in your page, subscribe to the child event instead of the main one:



```
((App)(App.Current)).BackRequested += OnBackRequested;
```

Make sure to unsubscribe to the event when leaving the page, or you may end up with a memory leak.


I've managed to convert most of it to VB:



```
Public Event BackRequested As EventHandler(Of BackRequestedEventArgs)

Private Sub OnBackRequested(sender As Object, e As BackRequestedEventArgs)
	' Raise child event

	RaiseEvent BackRequested(sender, e)

	If Not e.Handled Then
		If _rootFrame IsNot Nothing AndAlso _rootFrame.CanGoBack Then
			e.Handled = True

			_rootFrame.GoBack()
		End If
	End If
End Sub
```

But I'm really struggeling with this line:



```
((App)(App.Current)).BackRequested += OnBackRequested;
```

Any pointers as to how it should be in VB?

Thanks

Dave

----------


## si_the_geek

I think it should be this:


```
AddHandler App.Current.BackRequested, AddressOf OnBackRequested
```

----------


## QuattroDave

Thanks for the reply Si,

Hmmm it doesn't recognise 'App' so I'm going to assume 'Application' would be the correct choice. but then on this line:



```
AddHandler Application.Current.BackRequested, AddressOf OnBackRequested
```

I get the error 'BackRequested' is not a member of 'Application' when its blatently declaired there... 

Why is it the small issues that cause all the problems  :Roll Eyes (Sarcastic): 

Thanks

Dave

----------

