Unexpected Behavior with Event Handlers

0
0

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

  • You must to post comments
0
0

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?

  • You must to post comments
0
0

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

  • Luca (ITG)
    There is nothing wrong in the way you use the message box. Can you try to reproduce with our VB.NET navigation sample?
  • Gerald Lemay
    I should have added that the Messagebox does occasionally work but then subsequently fails. I use Messageboxes that do not have a DialogResult elsewhere in the app without issue.
  • You must to post comments
0
0

We have the same examples in VB.NET here

https://github.com/iceteagroup/wisej-examples-vb

 

  • You must to post comments
0
0

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

  • You must to post comments
0
0

I just tried our Navigation sample but cannot reproduce the issue. If you have steps to reproduce please send.

To answer the other questions:

  • If you call New Page1() it creates a new page instance, then when it’s made visible the first time it’s referenced in the OpenPages collection and stays there until disposed to prevent garbage collection. If you simply create a page instance and never use it, it will get garbage collected at one point or another, it’s up to .NET.
  • When you simply navigate back and forth – calling page.Show() or Application.MainPage = pageX then you are not creating new instances.

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

  • You must to post comments
0
0

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

  • You must to post comments
0
0

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

 

  • Luca (ITG)
    There is no need to dispose the page unless you want to dispose to free resources. Page.Show/Hide automatically set Application.MainPage. Do you get the same issue with out Navigation sample?
  • Gerald Lemay
    The Navigation sample is what I’ve been using and it creates the issue. I used the “Navigation.Navigate” function to initially load the satellite page. Once the Satellite page is loaded, the user returns to the Dashboard page(Navigation.PreviousPage.Show()), To return to the satellite page a second time, the Navigate function uses Application.OpenPages(pageType.Name) to open the satellite page instead of creating a new instance. It is here that the issue presents itself.
  • Gerald Lemay
    My concern about not disposing of pages is resources. If I use Application.MainPage = New SatellitePage to switch pages, I’m thinking it will create multiple instances of the page as one switches back and forth. Is that true? Also, I’m not sure how to implement Page.Open in vb. I only know to use Dim Page as New satellite then Page.Open. Without declaring the New Page variable, vb won’t allow me to enter satellite.Open. Again, this brings up the resources issue. Does Page.Open function to load a page then simply open it if it already exists?
  • Gerald Lemay
    I wrote some code to find if a page was already open by stepping through the items in Application.OpenPages with a For Next loop. If it was not, I loaded it using “Application.MainPage = New satellitepage”. If it was already open I used Application.OpenPages.Item(n).Show(). The original problem was reproduced. Apparently in my app using Application.OpenPages(page) causes the issue.
  • You must to post comments
0
0

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.

 

  • You must to post comments
Showing 8 results
Your Answer

Please first to submit.