Application Auto-closes after dialog box - vb.net

New to VB.NET but pretty stumpped at the moment.
My Windows Form application launches 'Form1' which then starts a dialog box using:
Dim dialogResult As Boolean = configWizard.ShowDialog()
The ConfigWizard then writes some data to the registry, pops up with the new registry values and closes at which point the rest for Form1 loads.
This all works fine.. When debugging from Visual Studio 2015.
The problem I'm facing is when I build an installer for this program using the in-built InstallShield. The installer set the registry values on install (which works perfectly) then the dialog box opens, sets new values and pops up with the new values it's written. This all works fine. However, the Form1 closes straight away as soon as I press 'OK' on the dialog box.
It's supposed to pop up with a message box saying 'True' but the entire program closes.
Upon constant running of the program it did appear that the Form1 did flash up for milliseconds before disappearing. Does seem like the program is just closing for some unknown reason. I'm pretty stumped as to how to stop the Form1 from closing. Any light of questions to help out would be must appreciated.
I've omitted some code that is irrelivent.
I managed to quick take a screenshot when the Form1 did flash up (verifying both Form1 and the messagebox saying 'True' ARE loading after the dialogbox is closing.. albeit only for a split second)
Code:
Dialog Box:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
myValue1= Me.myValue1.Text
myValue2= Me.myValue2.Text
Dim regKey As RegistryKey
regKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\removedSoftware", True)
regKey.SetValue("value1", myValue1)
regKey.SetValue("value2", myValue2)
MsgBox(myValue1 & " + " & myValue2)
MsgBox("Registry: " & regKey.GetValue("value1") & " data: " & regKey.GetValue("value2"))
regKey.Close()
Me.Close()
End Sub
Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dialogResult As Boolean = configWizard.ShowDialog()
Try
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
MsgBox(dialogResult)
End Sub

Use OnShown event (Form1_Shown) instead of OnLoad to display the dialog box.

Related

CrystalReportViewer.RefreshReport hangs when running from BackgroundWorker

I'm trying to "enhance" my reporting code by adding a loading screen while the Crystal Report is being prepared/loaded. Before I started trying to add the loading screen, all of my reports would come up just fine, but the cursor change just wasn't "enough" of an indication that the application was still working on pulling the report - some of them can take a while - so I wanted to provide a more "obvious" visual cue.
In order to accomplish this, I've put the report creation method calls into a BackgroundWorker that exists in the loading screen itself (I haven't gotten around to learning how to use Async/Await well enough yet to feel comfortable using that instead). The loading screen comes up correctly and everything appears to work as expected until it actually attempts to display the report on screen. At that point, the "Please wait while the document is processing." box comes up (in the CrystalReportViewer control in the form used to display reports), but it just sits there, not even spinning. Eventually, my IDE throws an error about receiving a ContextSwitchDeadlock and I pretty much just have to cancel execution.
Here's my dlgReportLoading "splash screen" with a PictureBox control that contains an animated GIF:
Imports System.Windows.Forms
Public Class dlgReportLoading
Private DisplayReport As Common.CRReport
Private WithEvents LoadReportWorker As System.ComponentModel.BackgroundWorker
Public Sub New(ByRef Report As Common.CRReport)
InitializeComponent()
DisplayReport = Report
End Sub
Private Sub dlgReportLoading_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Cursor = Cursors.WaitCursor
Me.TopMost = True
Me.TopMost = False
LoadReportWorker = New System.ComponentModel.BackgroundWorker
LoadReportWorker.RunWorkerAsync()
End Sub
Private Sub dlgReportLoading_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
Me.Cursor = Cursors.Default
End Sub
Private Sub LoadReport_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles LoadReportWorker.DoWork
If Not DisplayReport.ReportOption = Common.CRReport.GenerateReportOption.None Then
Select Case DisplayReport.ReportOption
Case Common.CRReport.GenerateReportOption.DisplayOnScreen
'-- This is the method I'm currently testing
DisplayReport.ShowReport()
Case Common.CRReport.GenerateReportOption.SendToPrinter
DisplayReport.PrintReport()
Case Common.CRReport.GenerateReportOption.ExportToFile
DisplayReport.ExportReport()
End Select
End If
DisplayReport.ReportOption = Common.CRReport.GenerateReportOption.None
'--
'-- This code was in use before trying to generate the reports in the background
'If Not DisplayReport.CrystalReport Is Nothing Then
' DisplayReport.CrystalReport.Dispose()
'End If
'--
End Sub
Private Sub LoadReport_Complete(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles LoadReportWorker.RunWorkerCompleted
Me.DialogResult = DialogResult.OK
Me.Close()
End Sub
End Class
As noted in the code above, I'm currently testing the ShowReport() method as defined here:
Protected Friend Sub ShowReport()
Dim ReportViewer As frmReportPreview
Me.PrepareReport()
ReportViewer = New frmReportPreview(Me)
With ReportViewer
.WindowState = FormWindowState.Maximized
.Show()
End With
End Sub
And the frmReportPreview is this:
Imports System.ComponentModel
Public Class frmReportPreview
Private DisplayReport As Common.CRReport
Private ReportToDisplay As CrystalDecisions.CrystalReports.Engine.ReportDocument
Public Sub New(ByRef Report As Common.CRReport)
InitializeComponent()
DisplayReport = Report
PrepareReportForDisplay()
Me.rptViewer.ReportSource = Nothing
Me.rptViewer.ReportSource = ReportToDisplay
' SET ZOOM LEVEL FOR DISPLAY:
' 1 = Page Width
' 2 = Whole Page
' 25-100 = zoom %
Me.rptViewer.Zoom(1)
Me.rptViewer.Show()
End Sub
Private Sub frmReportPreview_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'-- HANGS HERE
Me.rptViewer.RefreshReport()
End Sub
Private Sub frmReportPreview_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
ReportToDisplay.Dispose()
Me.rptViewer.ReportSource = Nothing
End Sub
'...CODE FOR PREPARING THE REPORT TO BE DISPLAYED
End Class
The dlgReportLoading form pops up correctly and the animation plays until the frmReportPreview pops up in front of it (it doesn't close). The little box that has what is normally an animated spinning circle indicating the report data is being loaded appears, but almost immediately freezes in place.
I have a breakpoint in the LoadReport_DoWork() method of my dlgReportLoading form after the call to the ShowReport() method, but it never gets to that point. I also have one in the LoadReport_Complete() method of that form that it never hits either and that dialog never actually closes.
I put another breakpoint at the end of the frmReportPreview_Shown method, right after the Me.rptViewer.RefreshReport() call, but it never hits that either, so it seems clear that this is where things are getting stuck, but only when the report is being generated through the BackgroundWorker. If I just call the ShowReport() method without sending it through the "splash screen" and BackgroundWorker, everything generates and displays normally.
I've tried putting the RefreshReport() method into its own BackgroundWorker with no change in the behavior. I've tried making the frmReportPreview object display modally with ShowDialog() instead of just Show(). None of this seems to help the issue.
I have a feeling something is being disposed of too early somewhere, but I can't figure out what that would be. I can provide the rest of the report preparation code from frmReportPreview if required, but that all seems to be working without error, as far as I can tell. I'm not averse to trying alternate methods of accomplishing my goal of showing the user a loading screen while all the report preparation is taking place - e.g., Async/Await or other multi-threading methods - so any suggestions are welcome. Please let me know if any additional clarification is needed.
ENVIRONMENT
Microsoft Windows 10 Pro 21H1 (OS build 19043.1348)
Microsoft Visual Studio Community 2017 (v15.9.38)
Crystal Reports for .NET Framework v13.0.3500.0 (Runtime version 2.0.50727)
EDIT: I forgot to mention that this whole mess is being called from a GenerateReport() method in my CRReport class defined as:
Public Sub GenerateReport(ByVal ReportGeneration As GenerateReportOption)
Me.ReportOption = ReportGeneration
If Me.ReportOption = GenerateReportOption.None Then
'...CODE FOR REQUESTING A GENERATION OPTION FROM THE USER
End If
Dim ReportLoadingScreen As New dlgReportLoading(Me)
ReportLoadingScreen.ShowDialog()
End Sub
Which, in turn, is being called from my main form like this:
Private Sub PrintMyXMLReport(ByVal XMLFile As IO.FileInfo)
Dim MyXMLReport As New IO.FileInfo("\\SERVER\Applications\Reports\MyXMLReport.rpt")
Dim Report As New Common.CRReport(MyXMLReport, XMLFile)
Report.GenerateReport(Common.CRReport.GenerateReportOption.DisplayOnScreen)
End Sub
You should separate the heavy lifting and UI operations into distinct methods in order to put them into the appropriate BackgroundWorker events:
Protected Friend Sub PrepareReport()
' perform long-running background work
End Sub
Protected Friend Sub ShowReport()
Dim ReportViewer = New frmReportPreview(Me) With {.WindowState = FormWindowState.Maximized}
ReportViewer.Show()
End Sub
Private DisplayReport As Common.CRReport
Private Sub LoadReport_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles LoadReportWorker.DoWork
DisplayReport.PrepareReport()
End Sub
Private Sub LoadReport_Complete(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles LoadReportWorker.RunWorkerCompleted
DisplayReport.ShowReport()
Me.DialogResult = DialogResult.OK
Me.Close()
End Sub
because LoadReport_DoWork actually runs on a new non-UI thread, and LoadReport_Complete runs on the caller thread, which is a UI thread. Only there can you interact with the UI and show Forms etc.

How to close CefSharp Session winforms vb.net

I've been struggling to close the current session when the form is closed and re opened
https://www.facebook.com
What I expect to happen:
Open form
log in in: https://www.facebook.com
close form
reopen form
session is closed and I can log in with different credentials
What actually happens:
Open form
log in in: https://www.facebook.com
close form
reopen form
session is still open with previous credentials
Imports CefSharp.WinForms
Imports CefSharp
Public Class MasOrdenAPP
Private Sub MasOrdenAPP_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.FormBorderStyle = 0
End Sub
Private WithEvents browser As ChromiumWebBrowser
Public Sub New()
CefSharp.Cef.Shutdown()
InitializeComponent()
Dim settings As New CefSettings()
CefSharp.Cef.Initialize(settings)
CefSharp.Cef.GetGlobalCookieManager.DeleteCookies("", "")
browser = New ChromiumWebBrowser("https://www.facebook.com/") With {.Dock = DockStyle.Fill}
browser.BrowserSettings.ApplicationCache = CefState.Disabled
Panel1.Controls.Add(browser)
End Sub
Private Sub bCerrar_Click(sender As Object, e As EventArgs) Handles bCerrar.Click
CefSharp.Cef.GetGlobalCookieManager.DeleteCookies("", "")
Me.Close()
Home.Activate()
End Sub
Private Sub MasOrdenAPP_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
'CefSharp.Cef.GetGlobalCookieManager.DeleteCookies("", "")
End Sub
End Class
I tried CefSharp.Cef.GetGlobalCookieManager.DeleteCookies("", "")
but only works with Facebook and reddit, not with the site I actually need it to work.
I tried using browser.dispose() but it disables the browser and when I try to reopen the form it doesn't work anymore
I tried:
settings.CachePath = ""
settings.CefCommandLineArgs.Add ("disable-application-cache", "1")
settings.CefCommandLineArgs.Add("disable-session-storage", "1")
It doesn't seem to do anything at all.
If this isn't possible in CEF, is there any way to reset or dispose the CEF browser every time the form is closed so every time the form opens it ask you for your credentials like the first time?

Form2 won't close and if I close it form1 will close as well. vb.net

So I made this game in vb.net, and when you run it, it will ask you for a name, that's form2. The thing is, when you put a name, form2 will not close/disappear, and if you close it the whole game will close.
This is the code for form2:
Public Class Form2
Public Shared myMoney As Long
Public Shared welcome As String
Private Sub PositronButton1_Click(sender As Object, e As EventArgs) Handles PositronButton1.Click
Form1.welcome = txtName.Text
Form1.lblWelkom.Text = "Welcome," & " " & Form1.welcome
MsgBox("Welcome," & " " & Form1.welcome & "." & "You recieved 500 money.")
Form1.myMoney = 500
Form1.lblMoney.Text = Form1.myMoney
Form1.Show()
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TopMost = True
End Sub
End Class
It seems you have wrong settings in your project.
Go to "Project", "Settings" and then have a look after "Shutdown mode".
Yours is probably set to "When last form closes". But you have to set "start form".
Also do not use Form1.Show because this is wrong, create an instance of it, then call it.
Dim frm As New Form1
frm.Show()
Also use ShowDialog for showing the Form2, it returns a DialogResult, and if it is "OK", you can close the form.
Firstly, instead of setting Me.TopMost on the load event, you should call Form2.Focus() when you first open the Form in your Form1 code.
Secondly I am not sure how you are opening Form2, I assume you are using ShowDialog() In that case, in order to close Form2, you should call Me.DialogResult = DialogResult.OK
Hope it Helps

Next button in VB.net

how do i close my form after opening the next form (VB.net Windows form) like the Next button
I tried Form2.show() it shows form2 but does not close the form1 and if i type me.close(), the entire project stops
If you just want Form2 to be Visible you can hide Form1 when you show Form2, then show it Form1 again when you close Form2. What is happening is that once you close Form1 your program will exit see below edit.
Something like this.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
AddHandler frm2.FormClosed, AddressOf Form2Closing
frm2.Show()
Me.Hide()
End Sub
Private Sub Form2Closing(sender As Object, e As FormClosedEventArgs)
Me.Show()
RemoveHandler DirectCast(sender, Form2).FormClosed, AddressOf Form2Closing
End Sub
If you just are wanting to Close Form1 and not go back to it once Form2 is open, you can change your project settings from When startup form closes(which is the default) to When last form closes then you can close your first form without closing your application.
You have to specify what to close:
Form1.Close()
You better not close your form after opening another one from it, unless you want that other one also closed. Otherwise it will cause ownership and visibility side effects, which you really don't want to deal with in the long run.
This is exactly why me.close() on your main form stops your project. You just have consider paradigms Microsoft put into a Winforms application. If you don't, you are guaranteed to get in trouble.
Instead, a wizard control is what you are probably looking for.

VB app not ending

I am using Visual Basic 2010 and I am makeing a simple login app it prompts you for a username and password if it gets it right I want to open a form then close the previus one but when I do me.close it ends the program and I can't go on
I am positive its working because I get the right password and username ut I can't close the first windows
I tried to hide it but when I do I cant close it and the program goes on without quiting and with a hidden form
I heard rumers that there is Sub that can control the x in the corner
if there is one that would probably solve my problem but I can't find it heres the code for the login form
Public Class Main_Login
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "My Name" And TextBox2.Text = "mypassword" Then
Contentsish.Show()
Me.Hide()
Label3.Hide()
Else
Label3.Show()
End If
End Sub
End Class
Then the Form if you enter the right login info
Public Class Contentsish
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Hide()
Timer1.Stop()
End Sub
End Class
how can I fix this problem?
Go into your project settings and set the application to close when the last form closes and not when the startup form closes.
edit: I just checked some of my VB.Net 2k8 code. What I do is create a new instance of the "child" form, do any initialization that I need, call .Show() on it, and then call .Close() on the current form (Me.Close()). Probably not the best way to do things, but it works for me. This is with the project setting I described earlier set. This allows me to exit from the child form or "logout" if needed.
Two other ways you can do this, in addition to Crags:
Load the main form first, then load the password from the main form.
You can use a separate vb module and use the Sub Main for the startup (you specify this in Project Properties, Application, Startup Object), then load the two forms from Sub Main.
So what happens if the login is incorrect? Sounds like you might want to show the login form using ShowDialog, maybe in your Main() before calling Application.Run(new FormMain());