Exit button on login form - vb.net

Is this sufficient code for an exit button on my form?
This is my code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim response As MsgBoxResult
response = MsgBox("Do you want to close form?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
If response = MsgBoxResult.Yes Then
Me.Dispose()
ElseIf response = MsgBoxResult.No Then 'Button to exit the program completely
Exit Sub
End If
End Sub

Yes, it is sufficient but:
You should use Me.Close() instead of Me.Dispose().
If this is the exit from your application completely and you have any unmanaged resources (e.g. database connections, files open, COM objects, etc). you should close them/clean them up.

Related

vb.net formclosing with dialog

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

Expression is not a method

Hello Coders Im trying to code a Anti virus and the following problem was here!
Im trying to add MsgBoxStyle.YesNo and will the problem in this...
So im trying to add a Question for delete file or no with MsgBox.
My code:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Label1_Click(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If My.Settings.ifTmpR = True Then
For Each aF In Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.Temp)
Try
MsgBoxStyle.YesNo()
MsgBox("Biztosan törli: " + aF)
IO.File.Delete(aF)
MsgBox("Temp kiürítése: " + aF)
MsgBox(aF + "kitötrölve")
Catch ex As Exception
End Try
Next
End If
If My.Settings.ifTmpR = False Then
Try
MsgBox("Kérlek kapcsold be a TEMP Queryt!")
Catch ex As Exception
End Try
End If
End Sub
Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
My.Settings.ifTmpR = True
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = False Then
My.Settings.ifTmpR = False
End If
End Sub
End Class
And what the problem?
Help?
So you're coding an Anti-Virus application but you seem to have no basic knowledge about how to use methods and parameters.
You are supposed to include MsgBoxStyle.YesNo as one of the parameters to the MsgBox() method. You cannot call it like you do since it's simply an Integer value.
As Steve suggested you should use MessageBox.Show() instead, due to that MsgBox only exists for backwards compability.
The first parameter is the message you want it to show, and the second parameter is what title the window should have. They're just plain strings.
The third parameter however is the one you pass to tell the method what buttons to include in the MessageBox, and it should be passed like this:
MessageBox.Show("I am a message", "I am a title", MessageBoxButtons.YesNo)
This will show a message box with the buttons "Yes" and "No" inside it.
Now to get use out of this you should put your MessageBox in an If-statement, otherwise the application won't care if you press either Yes or No.
If MessageBox.Show("Are you sure you want to delete this file?", "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
'Delete code here.
End If
There. Now I suggest that you would have started with something like a "Hello World"-application instead of an Anti-Virus application, as Anti-Virus is pretty much harder than just showing a simple MessageBox.

How to close first form?

I have 2 forms in my project. Users cannot exit from the first from, frmOptometry. I have this implemented this way:
Private Sub frmOptometry_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
MessageBox.Show("You can only close the application from the receipt screen!")
e.Cancel = True
End Sub
The second form, frmReceipt can be exited from. I have it implemented this way:
Private Sub frmReceipt_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Are you sure you want to quit?",
"Obi-Wan Optometry Closing",
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation) =
DialogResult.No Then
e.Cancel = True
Else
frmOptometry.Close()
End If
End Sub
Right now, it does not close the program completely because it goes to the frmOptometry_FormClosing and prevents it. What should I do to fix this? Is there anyway to figure out where the user is trying to shut down the program and then decide from there?
One way to solve this is by introducing a flag (say, called FromReceipt) which indicates who asks the frmOptometry to close itself.
Something like this on frmOptometry will do:
Public FromReceipt As Boolean = False 'Add this
Private Sub frmOptometry_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Not FromReceipt Then 'add this condition
MessageBox.Show("You can only close the application from the receipt screen!")
e.Cancel = True
End If
End Sub
And then on the frmReceipt:
Private Sub frmReceipt_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If MessageBox.Show("Are you sure you want to quit?",
"Obi-Wan Optometry Closing",
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation) =
DialogResult.No Then
e.Cancel = True
Else
frmOptometry.FromReceipt = True 'Add this line to tell the frmOptometry: this time you can really close it
frmOptometry.Close()
End If
End Sub

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

Click Button Event in VB that displays a message about user input

I have a really quick and simple question, I’m learning programming in C# and VB and I need to create a GUI application in Windows Form with Visual Studio. This GUI will prompt the user to enter in an integer. I believe I have this part alright but then I need to have the user click a button that will convert the user's entry to an integer and display a message indicating whether the user was successful. I think I even have the conversion done correctly but I am having a problem displaying that message if the user was successful. Basically I need to know how to function the click method in VB that will allow this message to appear. Any help with this would be greatly appreciated. The following code is what I have already written for this project:
Public Class Form1
Private Sub EvaluateInput()
Dim InputValue As String
InputValue = ValueTextBox.Text
If IsNumeric(InputValue) Then
MessageBox.Show(InputValue & " is a number.")
Else
MessageBox.Show(InputValue & " is not a number.")
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 'Continue Button
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Exit Button
Dim button As DialogResult
button = MessageBox.Show _
("Are you sure you want to exit this application?", _
"Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
If button = Windows.Forms.DialogResult.Yes Then
Me.Close()
Else
'Do Nothing
End If
End Sub
End Class
If I understand your question correctly, then you would need a simply change:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) _
Handles Button2.Click 'Continue Button
EvaluateInput()
End Sub
When you press Button2, it will call the EvaluateInput sub and display a message accordingly.