Message box on close only show when text is changed - vba

So I take no credit to the following code as it was given to me by another fellow user and don't get me wrong it works great but now I only Want the message box to show when text is changed in textboxes and if all text boxes are empty to just close the app when the X is clicked.
Any help is appreciated!
'Creates _handle as boolean
Private _handle As Boolean = True
Private Sub MainWindow_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
'Creates a messagebox to ask if the user wants to exit the application or no. If yes application closes, if no then application stays open
If _handle = True Then
e.Cancel = True
Dim result = MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButton.YesNo, MessageBoxImage.Question)
If (result = MessageBoxResult.Yes) Then
_handle = False
Environment.Exit(0)
End If
End If
End Sub

Related

Have to Click Form To Give Focus

I have a main form with buttons that open a custom message box form. It works fine if it's just a message and the user just needs to click OK. But if the answer to that message box is important, like "Are you sure you want to delete this file?" I use a while loop to wait for the user to respond and once they do then a flag is set from false to true and the response is recorded.
For some reason any response that uses a while loop to wait is causing the message box form to not have focus after being called. Requiring the user to first click on the form, and then click on OK.
So far I've tried using form.Activate() instead of form.Show(), as well as calling Application.DoEvents() inside the while loop since I believed the while loop was taking focus away from the message form immediately after being called. Neither solved the issue.
Code from a message box that works as intended:
If cmbLoadProgram.SelectedItem = "" Then
frmMessageBox.lblHeader.Text = "Set-Up"
frmMessageBox.lblMessageText.Text = "No Program Selected!"
frmMessageBox.Show()
Exit Sub
End If
Code from a message box that needs to be clicked twice:
If btnGetHexStart.Visible = False And cmbStartCondition.SelectedItem = "Pixel" Then
frmMessageBox.lblHeader.Text = "Hex Set-Up"
frmMessageBox.lblMessageText.Text = "Reset Hex Code Data?"
frmMessageBox.Show()
Me.Hide()
While Flag = False
If frmMain.OKCancel = "OK" Then
btnGetHexStart.Visible = True
btnGetHexStart.Enabled = True
btnGetHexStart.PerformClick()
Flag = True
End If
frmMain.delay(20)
End While
End If
I'm wanting both options to only need to be clicked once in order to confirm or cancel the action. Instead of the while loop questions needing to be clicked twice.
This just an idea from me, just how to open msgboxform, here we need MessageForm and one module1, for example:
Public Class MessageForm
'You can assign any variable to show any data in messageform display
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
theResult = MsgBoxResult.Ok
Me.Close()
Me.Dispose()
End Sub
Private Sub bttcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttcancel.Click
theResult = MsgBoxResult.Cancel
Me.Close()
Me.Dispose()
End Sub
End Class
Module Module1
Public theResult As MsgBoxResult
'You can add some parameter here to submit to MessageForm
Public Function myMessageBox() As MsgBoxResult
myMessageBox = MsgBoxResult.Cancel
MessageForm.ShowDialog()
myMessageBox = theResult
End Function
End Module
and then you can call the myMessageBox anywhere like this:
'if myMessageBox procedure have parameter, apply the paramters too
dim myRslt = myMessageBox()
You don't need to create a form,You can use DialogResult and MessageBox.Show, code:
Dim Result As DialogResult = MessageBox.Show("Set-Up" & vbCrLf & "No Program Selected!", "Warning", MessageBoxButtons.OKCancel)
If Result = DialogResult.OK Then
ElseIf Result = DialogResult.Cancel Then
End If

VB.NET: DialogResult.No is not closing the form

Using Message Box DialogResult.No Condition for closing the form doesnot perform as expected.
The formclosing event aske user whether to save the document or not before closing.
The following is my FormClosing event.
Private Sub PDFViewSimple_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) _
Handles Me.FormClosing
If doc.IsModified Then
Dim message As String = "The document is modified, would you like to save it?"
Dim caption As String = "File Not Saved"
Dim buttons As MessageBoxButtons = MessageBoxButtons.YesNo
Dim DefaultButton As MessageBoxDefaultButton = MessageBoxDefaultButton.Button1
Dim icon As MessageBoxIcon = MessageBoxIcon.Question
Dim result As DialogResult
' Displays A MessageBox.
result = MessageBox.Show(message, caption, buttons, icon, DefaultButton)
If (result = DialogResult.Yes) Then
Me.Save(Me.Text)
Me.Close()
ElseIf (result = DialogResult.No) Then
Me.Close() ''Should I replace with (Application.Exit)
End If
End If
End Sub
There's all sorts wrong with that code. Firstly, given that there are only two options, using ElseIf is pointless, although not strictly wrong. If it's not Yes then it must be No, so you'd only need an Else:
If (result = DialogResult.Yes) Then
Me.Save(Me.Text)
Me.Close()
Else
Me.Close()
End If
Next, even an Else is pointless because you're calling Close regardless of the outcome. All you need to do is check for Yes, do anything specific to Yes and then call Close regardless:
If (result = DialogResult.Yes) Then
Me.Save(Me.Text)
End If
Me.Close()
Finally, you shouldn't be calling Close at all. You're in the FormClosing event handler so the form is already closing. You only have to do something if you want the form to NOT close. So, all you need is this:
If (result = DialogResult.Yes) Then
Me.Save(Me.Text)
End If
If you wanted the form to NOT close then you would set e.Cancel to True.

Log Out message box appear twice when click on logout button

So I would like to perform a confirmation before i close the application or logout the application by clicking the logout button or just click to close the application. If i directly close the application then the message box just appear once. However when i use the logout button then the message box appear twice.
So the coding is basically look like this:
Private Sub btnLogOut_Click(sender As Object, e As EventArgs) Handles btnLogOut.Click
If logOut() Then
Me.Dispose()
frmLogIn.Show()
End If
End Sub
Private Sub frmHome_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If logOut() Then
Me.Dispose()
frmLogIn.Show()
Else
e.Cancel = True
End If
End Sub
Public Function logOut() As Boolean
Dim respond = MessageBox.Show("Are you sure you want to log out?", "Log Out", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If respond = DialogResult.Yes Then
blnResult = True
Else
blnResult = False
End If
logOut = blnResult
End Function
So anyone could help?
In your logout button event click
If logOut() Then //first line
Me.Dispose()//second line
frmLogIn.Show()//third line
End If//fourth line
in your second line which is Me.Dispose, you disposing the form, in other term you CLOSING it. So that, Your form_close event will be trigger because you dispose your form. That's why the message box pop up twice.

Hide a row in DataGridView

I am a new user to vb.net and need to hide a row when a user right clicks on a contextmenu and selects hide. I have googled this but have yet to find a way to do it.
At the moment, when a user clicks on an entry in the grid, the value is entered into a text box which is fine. What I need to do is hide the entry the user right clicked on and hide the selection. As I am new I am finding it hard going to code this as I have just finished my first course which entailed the basics. Any help would be appreciated or if you need anymore code, then please ask.
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub HideToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles pnlContextMenuStrip1.ItemClicked
'Get the text of the item that was clicked on.
'Dim text As String = txtCustomerActive.Text
Try
'txtCustomerActive.Visible = False
pnlContextMenuStrip1.Visible = False
MessageBox.Show(txtCustomerActive.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
You could use Rows.Item() to hide specific DataGridViewRow, like:
If (UserDataGridView.Rows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
End If
I am assuming you are using FullRowSelect here.
If you are not using FullRowSelect you could have this alternative code which could catch both Cell being Selected or Row being Selected:
If (UserDataGridView.SelectedRows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
ElseIf (UserDataGridView.SelectedCells.Count > 0) Then
For Each cell As DataGridViewTextBoxCell In UserDataGridView.SelectedCells
UserDataGridView.Rows.Item(cell.RowIndex).Visible = False
Next
End If
To Unhide everything let's say from a Button Click you could have this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each row As DataGridViewRow In UserDataGridView.Rows
If (row.Visible = False) Then
UserDataGridView.Rows.Item(row.Index).Visible = True
End If
Next
End Sub
As far as I know, you can not make a server-side handler for right mouse click (as you did for HideToolStripMenuItem_Click, which works as part of .NET postback mechanism).
However, I believe that such feature could be done with some client-side javascript progamming.
Hope this helps!

VB.NET - Code after Me.Close is executed

Update: Solution by #Mark Hall:
Using Exit Sub after the Me.Close to prevent further execution of the sub while the Closing event is going on..
I'm trying to open up a popup window on closing a form where the user can decide if he really wants to close the form.
Therefore I wrote:
Private Sub Form1_FormClosing_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
e.Cancel = True 'Fenster wird nicht geschlossen
Dim DialogExit As DialogResult
DialogExit = MsgBox("Do you want to exit the setup process?", vbQuestion Or MsgBoxStyle.OkCancel, "Exit Setup?")
Select Case DialogExit
Case Windows.Forms.DialogResult.OK
Me._Result_ThisForm = Result_Form.Yes
e.Cancel = False
Case Else
'Just no exit
End Select
End Sub
But when I call Me.Close from somewhere in the code, the form just doesn't close but it executes further code!
Like in (somewhere in the code):
If bErr = False Then
Me._Result_ThisForm = Result_Form.Goto_Next ' Just an additional info Enum
Me.Close()
End If
MsgBox("This should not pop up if bErr = false")
-> The "Exit Setup?" MsgBox pops up correctly but the user presses "ok" to exit the programm, the MessageBox from after the "Me.Close()" suddenly appers an then the program closes.
What causes this behavior?
Thank you for your help!