我在DataGridView中进行编码,并验证单元格是否只有数字格式和逗号,该单元格用于文档页面, 将返回true或接受的样本格式:1,2或1,2,5但不是1,,2或1 ,,, 6,2
我已经为它做了一个功能,它工作正常,但我不习惯使用我的代码,我希望有比我更好的代码。
请更正我的代码。
感谢。
Private Function isCELLPageNumb(ByRef valyo As String, ByVal origMaxPage As String) As Boolean
If valyo = "0" Or valyo = "," Then
valyo = origMaxPage
Return False
End If
Dim allowedChars As String = "0123456789,"
For i As Integer = (valyo.Length - 1) To 0 Step -1
If allowedChars.IndexOf(valyo(i)) = -1 Then
valyo = origMaxPage
Return False
End If
Try
If valyo(i) = "," Then
If valyo(i + 1) = "," Then
valyo = origMaxPage
Return False
End If
End If
Catch ex As Exception
valyo = origMaxPage
Return False
End Try
''I THINK I HAVE TO SEE IF THE COMMA NEXT NUMBER IS GREATER THAN THE MAXPAGE
''If valyo(i)>origMaxPage then
''End If
Next
Return True
End Function
编辑origMaxPage
Private Function isCELLPageNumb(ByRef valyo As String, ByVal origMaxPage As String) As Boolean
If valyo = "0" Or valyo = "," Then
valyo = origMaxPage
Return False
End If
Dim allowedChars As String = "0123456789,"
For i As Integer = (valyo.Length - 1) To 0 Step -1
''IF ALLOWED CHARACTERS NOT IN THE INDEX
If allowedChars.IndexOf(valyo(i)) = -1 Then
valyo = origMaxPage
Return False
End If
Try
''IF VALYO IS COMMA REPEATED
If valyo(i) = "," Then
If valyo(i + 1) = "," Then
valyo = origMaxPage
Return False
End If
End If
Catch ex As Exception
valyo = origMaxPage
Return False
End Try
Try
''IF VALYO GREATHER THAN THE MAXPAGE
If valyo(i) = "," Then
Dim twodigit As String = valyo(i + 1) & valyo(i + 2)
Dim numtwodigit As UInt32 = Val(twodigit)
If numtwodigit > origMaxPage Then
valyo = origMaxPage
Return False
End If
End If
Catch ex As Exception
valyo = origMaxPage
Return False
End Try
Next
Return True
End Function
代码的问题,如果maxpage是12,那么用户输入1,3,5,1111 ?
输入可能不接受负数,如:-1或-123
由于
答案 0 :(得分:2)
我认为在某些时候你还是需要获取页码,所以你应该从那开始:
Public Function ParsePageNumbers(value As String, maxPage As Integer) As List(Of Integer)
Dim values As New List(Of Integer)()
For Each strNumber As var In value.Split(","C)
Dim intValue As Integer
' if it wasn't an integer or it's greater than the max page, restore the original value
If Not Integer.TryParse(strNumber, intValue) OrElse intValue > maxPage Then
Return Nothing
End If
values.Add(intValue)
Next
Return values
End Function
如果页码具有无效值,则此函数将返回Nothing
。然后在您的实际方法中,您可以调用此方法并检查Nothing
:
Private Function isCELLPageNumb(ByRef valyo As String, ByVal origValue As String) As Boolean
Dim maxPage As Integer = Integer.Parse(origMaxPage)
' if it's not parsible, restore the original value
If ParsePageNumbers(value, maxPage) Is Nothing Then
value = origMaxPage
Return False
End If
' it was all valid
Return True
End Function
答案 1 :(得分:1)
组合到MackieChan solution来解析整数,您应该先使用Regex
private rgxNumberWithComma As New System.Text.RegularExpressions.Regex("^([0-9]+,?)+$")
Public Function CheckInput(ByVal valyo As String, _
ByVal origMaxPage As Integer) As Boolean
Dim match = rgxNumberWithComma.Match(valyo)
If Not match.Success Then
Return False
Else
Dim numbers as new List(Of Integer) ‘will store added numbers
For Each Item In valyo.Split(","c)
Dim intValue As Integer
‘Check if number is a valid integer
‘Check if number is 0
‘Check if number has already added the number list
‘Check if number is greater that MaxPage
If Not Integer.TryParse(Item, intValue) _
OrElse intValue > origMaxPage _
OrElse intValue = 0 _
OrElse numbers.Contains(IntValue) Then
Return False
Else
‘Item is valid, continue
Numbers.Add(intValue)
End If
Next
End If
Return True
End Function
答案 2 :(得分:1)
在您的Datagridview EditingControlShowing事件中尝试...
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Try
If UCase(sCellName) = "PAGENUM" '------> change this with yours
AddHandler e.Control.KeyPress, AddressOf PageKeypress
End If
Catch ex As Exception
'...
End Try
End Sub
Private Sub PageKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Static sLast As String = ""
Dim k As Byte = Asc(e.KeyChar)
Dim sN As String = "0123456789,"
Dim sO As String = Chr(8) & Chr(13) & Chr(1) & Chr(3) & Chr(22)
Dim nMaxPage As Integer = 12 '-------change this with yours
If Not (sN & sO).Contains(e.KeyChar) Then
e.Handled = True
Else
Select Case e.KeyChar
Case ","
If sLast = "," Then
e.Handled = True
Else
e.Handled = False
sLast = ","
End If
Exit Sub
Case "0"
If sLast = "," Or sLast = "" Then
e.Handled = True
Exit Sub
End If
Case Chr(13) '-- avoid "," in end of text OR YOU CAN REMOVE THIS
If sLast = "," Then e.Handled = True
End Select
If sLast = "," Then sLast = ""
If Val(sLast & e.KeyChar) > nMaxPage Then
e.Handled = True
Exit Sub
End If
sLast &= IIf(sN.Contains(e.KeyChar), e.KeyChar, "")
End If
End Sub