I have a vb.net desktop project where I plan on navigating between multiple pages. Each page switch is centered on returning to a Dashboard page as a base page where the other satelite pages can then be individually accessed. I use the mybase.Appear handler on the satelite pages to refresh the data in text boxes from a database upon each page switch. This requires that any data is saved before returning to the dashboard page or it is lost. To avoid unintended loss of changes on the satelite pages, each text box has an event handler which changes a page variable “Changed” to True.
Private Sub txtClientName_TextChanged(sender As Object, e As EventArgs) Handles txtClientName.TextChanged
If StopFunction = False Then
Changed = True
End If
End Sub
The StopFunction page variable avoids firing the handler code when the page is initially updated from the database in the MyBase.Appear handler.
The cmdDashboard button to navigate back to the dashboard offers to save the page if Changed = True:
Private Sub cmdDashboard_Click(sender As Object, e As EventArgs) Handles cmdDashboard.Click
If Changed = True Then
Changed = False
Dim ans = MessageBox.Show(“Do you wish to save your changes?”, “Changes Made”, MessageBoxButtons.YesNoCancel)
Select Case ans
Case DialogResult.Yes
SaveChanges()
Navigation.PreviousPage.Show()
Case DialogResult.No
Navigation.PreviousPage.Show()
Case DialogResult.Cancel
End Select
Else
Navigation.PreviousPage.Show()
End If
End Sub
If a text box is changed on the initial page switch to the satellite page, the cmddashboard button handler works correctly identifying and saving the change. Upon returning to the satellite page a second time, I subsequently edit a text box and click the cmdDashboard button. At that point it crashes, I can’t select any text boxes and the cmdDashboard button does not fire its click event.
The cmdDashboard event works fine consistently if no textbox is changed. I’ve investigated if the Changed variable is being changed somewhere else but the issue is that the cmdDashboard event doesn’t even fire and I can’t select any other control.
I’ve used this strategy on many regular desktop programs and its always performed perfectly. I’m not sure what I’m missing. I’d upload the project but at this point it’s too large.
Also, I use Navigation.Navigate(GetType(satellitepage)) on the Dashboard page to switch to the satellite page, from Wisej examples.
Thanks!
Gerry
My test case used both my “work around” for page switching and the Navigation class. Both work fine without the Messagebox being fired. How can I send that to you?
So, I did some further testing. It turns out the Messagebox causes the lockup. It’s fired if the Changed variable is true. I’m not sure what I’m doing wrong formatting the MessageBox, but that’s where I need the help I believe. I tried to upload a test case as a separate project where the problem was reproduced but the zip file was marginally too large. I’m not sure how to make it smaller, there’s only two sparse pages. Here’s the Messagebox code:
Dim ans = MessageBox.Show(“Do you wish to save your changes?”, “Changes Made”, MessageBoxButtons.YesNoCancel)
I also tried
Dim ans = MessageBox.Show(“Do you wish to save your changes?”, “Changes Made”, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, True, False, True, Wisej.Web.RightToLeft.Inherit)
Thanks!
Gerry
This becomes a vb.net question if you can help. Calling page.Show() or Application.MainPage = page in vb is not as straightforward as in C#. In order to compile I’ve found assigning the Application.MainPage needs to be Application.MainPage = New Page1. Also Page1.Show() won’t compile, can you help with a vb fix for this?
I will create a new project and try to reproduce the error as I have tried using Application.OpenPages(“Page1”).Show() and it causes the error.
Thanks Again!
Gerry
I just tried our Navigation sample but cannot reproduce the issue. If you have steps to reproduce please send.
To answer the other questions:
A good approach is something like this:
var page1 = Application.OpenPages["page1"] ?? new Page { Name = "page1" };
Retrieves the existing “page1” and if null creates a new one.
HTH
Hi Luca,
I wasn’t sure if I provided the information you needed in my comments to your comment. The problem is reproducible. Do you need me to send you a test case?
Thanks much!
Gerry
Thanks for your response! I’m glad to build a smaller project to replicate the issue but before I do this I want to explore a more basic explanation. Also, the app does not “crash” per se, it just locks up. I believe your explanation of using disposed pages is correct. FYI, I can eliminate the problem by refreshing the page in the browser.
I’m not sure of the best way to handle switching pages. I suspect my limited understanding might be creating the issue. In a desktop app, closing a form disposed of all its components. In my app I used the WiseJ example code for the class Navigation from https://github.com/iceteagroup/wisej-examples/tree/2.2/PageNavigation/PageNavigation. The function below seemed to offer a way to load a new page or reload it if it already existed. This sped up the page transition for already loaded pages which seemed like a good option:
Public Shared Function Navigate(pageType As Type, ParamArray args As Object()) As Page
PreviousPage = Application.MainPage
Dim page As Page = Application.OpenPages(pageType.Name)
If page Is Nothing Then page = CType(Activator.CreateInstance(pageType, args), Page)
page.Show()
Return page
End Function
I used this to switch to a satellite page then simply used the PreviousPage variable in the Navigation class to return to the Dashboard page: Navigation.PreviousPage.Show(). I can dispose of the satellite page to solve the issue but then I have the delay in reloading it to switch back.
My solution may need be to dispose of any open pages and reload the next but this creates a delay in opening the page. I can live with that but first I wanted to know if you have a recommendation on how to switch pages. I’ve seen a few suggestions in these support questions but I’m not sure what best suits my needs. The code below works without any issue switching pages (problem solved), but I was hoping to reuse open pages to speed up the transition as its possible for users to want to switch back and forth.
Application.MainPage = New SatellitePage
Me.Dispose()
Do you have a better recommendation -or- a way to implement the function I pasted above without the error ?
Thanks very much for your time!
Gerry
Crashes how? If you get an exception pls send the stack trace. You can also enable VS exceptions to catch it when thrown.
Looks to me that you end up with disposed pages and controls. Hit refresh on the browser to refresh the state on the browser so you can see exactly what is on the server.
What is Navigation? It seems like a module in VB.NET which is shared and you may end up with disposed objects.
It’s unclear how we can help without some kind of reproducible. You don’t have to upload the entire application, just build a small simple case showing the same architecture in the code snippet.
Please login first to submit.