断言无效数据后防止表单关闭
Preventing Form From Closing after invalid data asserted
我有一个 FormClosing 事件处理程序,它在操作时显示一个带有两个按钮是或否的消息框。
因此,如果用户单击“是”,那么它会使用 For 循环检查值,并在发现无效值时如何保持在同一个表单上,而不打开新的所有值保留的表单。
我怎样才能做到这一点。
简化代码如图:
(class 代码位于 EditDataForm.vb 中,被 MainForm.vb Clas 代码使用)
''' Closing Event that fired upon user closing the Form
Private Sub EditDataForm_FormClosing(sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show(Me, "Do you want to save your changes?", "Unsaved Changes!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
ButtonSave_Click(Me, e)
Else
Exit Sub 'Exit this Sub
End If
End Sub
''' Button Save Click Event
Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
Dim rowIndex As Integer
Dim CheckDouble As Double
'' Leave the oth and 1th row
For rowIndex = 2 To masterDataGridView.RowCount - 1
If TypeOf(masterDataGridView.Rows(rowIndex).Cells("Val").Value) is String Then
Double.TryParse(masterDataGridView.Rows(rowIndex).Cells("Val").Value, CheckDouble)
If(CheckDouble <= 0) Then
MsgBox("Decimal Number Expect in place of:" & masterDataGridView.Rows(rowIndex).Cells("Val").Value & "at Row Number:" & rowIndex + 1, MsgBoxStyle.Critical, "FAILURE")
'''''''' HOW CAN I STAY ON THIS SAME FORM
Exit Sub
End If
End If
Next
''Other Save Methods etc...
End Sub
在 FormClosing 事件处理程序中,将 FormClosingEventArgs
参数的 Cancel
属性 设置为 true。
它将阻止表单关闭:
https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs(v=vs.110).aspx
为了将其集成到您的应用程序中,我将创建一个方法来验证您的用户输入。
此方法应该 return 一些东西(例如布尔值),您可以使用它来确定表单上的数据是否有效。
在表单关闭事件处理程序中使用此方法,并在那里验证表单是否应关闭:
Private Sub EditDataForm_FormClosing(sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
If IsFormDataValid() = False Then
e.Cancel = true
Exit Sub
End if
SaveData()
End Sub
Private Function IsFormDataValid() As Boolean
' Verify input, return true if data is valid; otherwise false.
End Function
我有一个 FormClosing 事件处理程序,它在操作时显示一个带有两个按钮是或否的消息框。 因此,如果用户单击“是”,那么它会使用 For 循环检查值,并在发现无效值时如何保持在同一个表单上,而不打开新的所有值保留的表单。
我怎样才能做到这一点。
简化代码如图: (class 代码位于 EditDataForm.vb 中,被 MainForm.vb Clas 代码使用)
''' Closing Event that fired upon user closing the Form
Private Sub EditDataForm_FormClosing(sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show(Me, "Do you want to save your changes?", "Unsaved Changes!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
ButtonSave_Click(Me, e)
Else
Exit Sub 'Exit this Sub
End If
End Sub
''' Button Save Click Event
Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
Dim rowIndex As Integer
Dim CheckDouble As Double
'' Leave the oth and 1th row
For rowIndex = 2 To masterDataGridView.RowCount - 1
If TypeOf(masterDataGridView.Rows(rowIndex).Cells("Val").Value) is String Then
Double.TryParse(masterDataGridView.Rows(rowIndex).Cells("Val").Value, CheckDouble)
If(CheckDouble <= 0) Then
MsgBox("Decimal Number Expect in place of:" & masterDataGridView.Rows(rowIndex).Cells("Val").Value & "at Row Number:" & rowIndex + 1, MsgBoxStyle.Critical, "FAILURE")
'''''''' HOW CAN I STAY ON THIS SAME FORM
Exit Sub
End If
End If
Next
''Other Save Methods etc...
End Sub
在 FormClosing 事件处理程序中,将 FormClosingEventArgs
参数的 Cancel
属性 设置为 true。
它将阻止表单关闭:
https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs(v=vs.110).aspx
为了将其集成到您的应用程序中,我将创建一个方法来验证您的用户输入。
此方法应该 return 一些东西(例如布尔值),您可以使用它来确定表单上的数据是否有效。
在表单关闭事件处理程序中使用此方法,并在那里验证表单是否应关闭:
Private Sub EditDataForm_FormClosing(sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
If IsFormDataValid() = False Then
e.Cancel = true
Exit Sub
End if
SaveData()
End Sub
Private Function IsFormDataValid() As Boolean
' Verify input, return true if data is valid; otherwise false.
End Function