vb.net formclosing with dialog - vb.net

I can not believe I am asking this question after reading similar questions mostly OLD and here is the frustration I had code that worked just as desired then I changed to Option Strict ON now more issues than I bargained for
Here is the code that works OR I should say worked at one time
In fact it still works on another test project?
The Handles Me.Closing has a RED ERROR squiggle line under it
Private Sub frmTwo_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.Closing
Const message As String = "YES Exit Program" + vbCrLf + vbNewLine + "NO Back to Form Start"
Const caption As String = "Exit OR Cancel"
Dim result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
Application.Exit()
ElseIf result = DialogResult.No Then
frmStart.Show()
Me.Hide()
'tbMessage.Text = "CANCEL"
e.Cancel = True
End If
End Sub
Tested this code on two forms frmStart and frmTwo on frmTwo it does not stop Debugging
Private Sub frmStart_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Are you sure to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
e.Cancel = False
Else
Me.Hide()
frmTwo.Show()
e.Cancel = True
End If
End Sub

It seems with Option Strict ON VB.Net does not like constructing the Sub this way
Private Sub frmStart_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
And as mentioned you then need to include Imports System.ComponentModel
So to STOP playing e.Cancel = True and e.Cancel = False just write a Sub as seen below and Call that Sub from some Button no need for Imports System.ComponentModel
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
CloseForm()
End Sub
Public Sub CloseForm()
Const message As String = "YES Exit Program" + vbCrLf + vbNewLine + "NO Return to Form Start"
Const caption As String = "Exit OR Return"
Dim result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
Application.Exit()
ElseIf result = DialogResult.No Then
Close()
frmStart.Show()
End If
End Sub

Recreating the closing event, the event parameters are (sender As Object, e As CancelEventArgs) not the different types. I'm testing this against .Net Framework 4.8, what .Net are you targeting?
This worked as expected:
Private Sub frmStart_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
Const message As String = "YES Exit Program" + vbCrLf + vbNewLine + "NO CANCEL"
Const caption As String = "Exit OR Cancel"
Dim result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.No Then
Me.Text = "CANCEL"
e.Cancel = True
End If
End Sub

Related

opendialog to show a file and save it with checkbox vb.net

I'm trying to connect to a database (mdb file of my choice) in a login screen and i want to save it for faster logon next times i boot the software.
I click on choose database button, opendialog lets me choose the file, i click OK and the db location shows in a textbox.
there's a checkbox beneath to save it before i connect to it.
But i can't manage to keep the checkbox checked, nor the textbox filled after i restart te program.
here's my current code:
Public Class LoginScreen
Private Sub Loginscreen_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ProgressBar2.Minimum = 0
ProgressBar2.Maximum = 100
ProgressBar2.Visible = False
Panel1.Visible = False
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
Private Sub btnExit2_Click(sender As Object, e As EventArgs) Handles btnExit2.Click
Application.Exit()
End Sub
Private Sub tmrLogin_Tick(sender As Object, e As EventArgs) Handles tmrLogin.Tick
ProgressBar2.Value = ProgressBar2.Value + 20
lblLoginMessages.Text = ProgressBar2.Value & "%" & " Completed"
If ProgressBar2.Value >= 100 Then
tmrLogin.Enabled = False
If txtUser.Text = "azert" And txtPassword.Text = "azert" Then
ProgressBar2.Value = 0
Else
lblLoginMessages.Text = "Wrong credentials, Try again!"
pboxClosed.Visible = True
PboxOpen.Visible = False
ProgressBar2.Value = 0
txtPassword.Text = ""
txtUser.Text = ""
End If
End If
End Sub
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
ProgressBar2.Visible = True
tmrLogin.Enabled = True
pboxClosed.Visible = False
PboxOpen.Visible = True
''navraag doen om dit correct in te stellen! ! ! ! !!
'If ProgressBar2.Value = 100 Then
'lblLoginMessages.Text = "Logging in..."
Me.Hide()
Mainscreen.Show()
'End If
If chkSavePassword.Checked = True Then
My.Settings.databaselocation = txtDatabaselocationshow.Text
My.Settings.SaveLocation = True
End If
End Sub
Private Sub btnDBConnect_Click(sender As Object, e As EventArgs) Handles btnDBConnect.Click
If Panel1.Visible = False Then
Panel1.Visible = True
Else
Application.Exit()
End If
End Sub
Private Sub btnChoose_Click(sender As Object, e As EventArgs) Handles btnChoose.Click
Dim strtext As String
OpenFileDialog1.Filter = "Database Files | *.mdb"
OpenFileDialog1.InitialDirectory = "F:\GoogleDrive\EINDWERK VBNET"
OpenFileDialog1.Title = "Choose your Database"
OpenFileDialog1.ShowDialog()
strtext = OpenFileDialog1.FileName
txtDatabaselocationshow.Text = strtext
'If OpenFileDialog1.ShowDialog = DialogResult.OK Then
' strtext = OpenFileDialog1.FileName
' txtDatabaselocationshow.Text = strtext
'Else
' MsgBox("Error: the database file could not be read, try again.")
'End If
End Sub
Private Sub tmrshowloginpanel_Tick(sender As Object, e As EventArgs) Handles tmrshowloginpanel.Tick
Panel1.Width += 5
If Panel1.Width >= 700 Then
tmrshowloginpanel.Stop()
End If
End Sub
End Class
i've scoured the net but can't really find what to do?
if you need more information, shoot!
Walk through this with me:
Make a new project, one form, add a textbox and a combobox.
Click the textbox:
In the properties grid at the top click (Application Settings), then the 3 dots next to Property Binding. Scroll to Text. Drop down the setting and choose New at the bottom.
Give it a name of ChosenDatabasePath or something useful and descriptive like that
Repeat from "Click the textbox" for the checkbox instead, and this time bind the Checked property not the Text property.. Call the checkbox's Checked binding SavePassword or similar
Close all the dialogs so you're back at the form
Click anywhere on the background of the form when switch to Events in the property grid, find FormClosing and double click it
Put My.Settings.Save() in the FormClosing event handler
Run the app, write something in the textbox, close the form (by the X, not by stopping debugging), then immediately open the app again (run it)

VB messagebox yes/no I want the 'no' to close message box, but its closing the form i want open

Private Sub btnMain_Click(sender As Object, e As EventArgs) Handles btnMain.Click
Dim result As DialogResult = MessageBox.Show("Did you save customer info?", "Save Information", MessageBoxButtons.YesNo, MessageBoxIcon.Stop)
If (result = DialogResult.Yes) Then
'Me.Visible = False
Me.Close()
frmDuneTours.Visible = True
ElseIf (result = DialogResult.No) Then
frmDuneTours.Visible = False
Me.Visible = True
End If
End Sub
When you place Me.Close before
frmDuneTours.Visible = True
the second line never executes because when you close the current form the application ends.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim result As DialogResult = MessageBox.Show("Did you save customer info?", "Save Information", MessageBoxButtons.YesNo, MessageBoxIcon.Stop)
If (result = DialogResult.Yes) Then
frmSortedList.Visible = True
Me.Close()
ElseIf (result = DialogResult.No) Then
frmSortedList.Visible = False
End If
End Sub

Skip Me.FormClosing

i've set a warning message before closing the form, but is there a way to skip it sometimes?
My code:
Sub Me_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Are you sure you want to cancel the installation?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
e.Cancel = False
Else
e.Cancel = True
End If
End Sub
but i have a final code that must close the app without this message:
Private Sub Done_Click(sender As Object, e As EventArgs) Handles Done.Click
'need to close without warning
Close()
End Sub
can you help me change this or add something that allows that button to close the form without launching Me.FormClosing?
Use a Boolean flag to determine the installation status (success or fail/abort)
Private installSuccess As Boolean ' False by default.
Private Sub Install()
Try
' Installer logic here
' ...
Me.installSuccess = True
Catch ' ex As Exception
End Try
End Sub
Then:
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) _
Handles MyBase.FormClosing
If Me.installSuccess Then
Exit Sub
End If
If MessageBox.Show("Are you sure you want to cancel the installation?", "Warning",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) = DialogResult.Yes Then
e.Cancel = False
Else
e.Cancel = True
End If
End Sub

Save Dialog when User Cancel

I need to save a file from different locations in my application, so I created a sub for that; everything work just fine, except when a user clicks cancel when the save dialog shows up; if user clicks "Cancel", the form will close; I tried the TWO Options shown in the code below but both did not work; any suggestion will be appreciated:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SaveFileDialog()
End Sub
Sub SaveFileDialog()
SaveFileDialog1.Filter = "TXT Files (*.txt)|*.txt"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
'If SaveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
Dim MekdamSaveFile = SaveFileDialog1.FileName
System.IO.File.WriteAllText(MekdamSaveFile, "")
My.Computer.FileSystem.WriteAllText(MekdamSaveFile, RichTextBox2.Text, True)
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Dim result = MessageBox.Show("The File: has been changed, do you want to save it? ", _
"Mekdam Message 701", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
If result = DialogResult.Cancel Then
e.Cancel = True
ElseIf result = DialogResult.No Then
'PROCEED...
ElseIf result = DialogResult.Yes Then
SaveFileDialog()
End If
End Sub
End Class
Make your SaveFileDialog Subroutine into a Function, then return False if anything other than OK was done, then test it in your FormClosing EventHandler and stop the Close.
Modified SaveFileDialog
Function SaveFileDialog() As Boolean
SaveFileDialog1.Filter = "TXT Files (*.txt)|*.txt"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
'If SaveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
Dim MekdamSaveFile = SaveFileDialog1.FileName
System.IO.File.WriteAllText(MekdamSaveFile, "")
My.Computer.FileSystem.WriteAllText(MekdamSaveFile, RichTextBox2.Text, True)
Return True 'Return True if Ok is clicked
Else
Return False 'return false this will give you something to conditionaly test
End If
End Function
Modified FormClosing EventHandler
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Dim result = MessageBox.Show("The File: has been changed, do you want to save it? ", _
"Mekdam Message 701", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
If result = DialogResult.Cancel Then
e.Cancel = True
ElseIf result = DialogResult.No Then
'PROCEED...
ElseIf result = DialogResult.Yes Then
If Not SaveFileDialog() Then e.Cancel = True 'this will abort the close
End If
End Sub
Is not too much clear what you need... try this:
Public Class Form1
Private Sub Test() Handles MyBase.Load
Do Until Not ShowSaveFileDialog() = DialogResult.Cancel
ShowSaveFileDialog()
Loop
End Sub
Private Function ShowSaveFileDialog() As DialogResult
Using SFD As New SaveFileDialog With
{
.Filter = "TXT Files (*.txt)|*.txt",
.ValidateNames = True
}
AddHandler SFD.FileOk, AddressOf SFD_FileOk
Return SFD.ShowDialog()
RemoveHandler SFD.FileOk, AddressOf SFD_FileOk
End Using
End Function
Private Sub SFD_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs)
IO.File.AppendAllText(sender.FileName, RichTextBox2.Text, System.Text.Encoding.Default)
End Sub
End Class

Confirming program close with a messagebox

I have a message box that pops up when i press a close button that basicaly says" Are you sure you want to quit" but when i click the no button or cancel but the program closes any how
this is my code:
'Close Button
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
Dim result = MessageBox.Show(" Are you sure you want to quit", "Are you sure?", MessageBoxButtons.YesNoCancel)
Me.Close()
End Sub
You are doing nothing with the value of result. You need to inspect the value and determine whether you call Me.Close(). Code approximately
If result = DialogResult.Yes Then
Me.Close()
End If
If you are using then message box to prevent accidental form close, your approach may not work always. The message box will not be shown if the user closes the application in any other way than clicking the "Close" button.
Try using the FormClosing event.
'Close Button
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
Me.Close()
End Sub
'FormClosing Event
Private Sub MyForm_Closing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show(" Are you sure you want to quit", "Are you sure?", MessageBoxButtons.YesNoCancel) <> DialogResult.Yes
e.Cancel = True
End If
End Sub
You issue Me.Close() no matter what the result is. Check the result and execute Me.Close() only the user clicks Yes
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
If MsgBox("Are you sure you want to quit?", MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2, "Close application") = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If
End Sub
Copy this:
Dim result = MessageBox.Show(" Are you sure you want to end the Application", "School Management System", MessageBoxButtons.YesNoCancel)
If result = DialogResult.Yes Then
Me.Close()
End If
Dim result = MessageBox.Show(" Are you sure you want to quit", "System Reminder", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
Me.Close()
End If
If it is a child form, it opens as a result of a button in a main form:
If MessageBox.Show(" Are you sure you want to exit the application ? ", "Exit ?", MessageBoxButtons.YesNo) = DialogResult.Yes Then
Me.Hide() : MainForm.Show()
Else
e.Cancel = True
End If
You can use the following code:
Dim closingfrm = MsgBox(" Are you sure to close", MsgBoxStyle.YesNo)
If closingfrm = DialogResult.Yes Then
Application.Exit()
End If