0

我正在尝试通过检测页面的逗号来更改或更新代码,

下面的代码展示了如何用逗号输入页面,

示例:1,2,5,3,8 不接受 0 或大于最大页数

我要问的是添加可以接受的代码:

2-5,8,9

8,9,2-5

2-5,8-10

所以这意味着,要打印的页面是 2,3,4,5,8,9,10

但它不会接受像 2-5,4,8,9 这样的输入,因为 4 已经在 2-5 中使用

如果这可能很难,那么可以输入一个简单的范围,例如:2-5 并且没有逗号,所以用户不能输入逗号,如果他们想输入逗号,那么 - 符号也不能输入。

''CODED By: Chris, Combined to MackieChan solution    
Public Function isCELLPageNumb(ByRef valyo As String, ByVal origMaxPage As Integer) As Boolean
   Dim rgxNumberWithComma As New System.Text.RegularExpressions.Regex("^([0-9]+,?)+$")
   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   


Private Sub DbGridPapers_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DbGridPapers.CellEndEdit

   Dim pagestoprint As String = Nothing

   Try
      pagestoprint = DbGridPapers.Rows(e.RowIndex).Cells(1).Value.ToString
   Catch ex As Exception
   End Try

   If (e.ColumnIndex = 1) And (pagestoprint IsNot Nothing) Then    
      If Not isCELLPageNumb(DbGridPapers.Rows(e.RowIndex).Cells(1).Value, OrigPage(e.RowIndex)) Then
         MyThreadedControl(lbltest, "Text", "INVALID INPUT FOR [PAGES] AT ROW " & (e.RowIndex).ToString)
         DbGridPapers.Rows(e.RowIndex).Cells(1).Value = OrigPage(e.RowIndex)
         Return
      Else
         MyThreadedControl(lbltest, "Text", "The Maximum Page is:" & OrigPage(e.RowIndex).ToString)
      End If

      Dim pageDest As String = Nothing
      If Me.btnpaperpay.Enabled Then
         pageDest = DbGridPapers.Rows(e.RowIndex).Tag & "\"
      Else
         pageDest = docPrintnationPath & "\"
      End If

      Dim filename As String = pageDest & DbGridPapers.Rows(e.RowIndex).HeaderCell.Value.ToString
      Dim OldRegularPrice As Decimal = DbGridPapers.Rows(e.RowIndex).Cells(3).Value
      Dim FILEpages As New List(Of Integer)

      ''IF , AND - CAN BE MIX TO GET THE PAGE THEN ITS BETTER, AND I HAVE TO UPDATE THE CODE HERE ALSO.

      If pagestoprint.Split(",").Length > 1 Then 'Split Length is +1 based
         Dim pageFILES() As String = pagestoprint.Split(",")
         For Each filePids As Integer In pageFILES
            FILEpages.Add(filePids) ''GET range in comma sample page1,page3,page8,page2
         Next
      ElseIf pagestoprint.Split("-").Length > 1 Then 'Split Length is +1 based
         Dim pageFILES() As String = pagestoprint.Split("-")              
         For page As Integer = pageFILES(0) To pageFILES(1)    
            FILEpages.Add(page) ''GET range sample pages2 to page5
         Next
      Else
         Dim pages As Integer
         If (Integer.TryParse(pagestoprint, pages)) Then
            If pages = OrigPage(e.RowIndex) Then
               DbGridPapers.Rows(e.RowIndex).Cells(2).Value = OrigImage(e.RowIndex)
               DbGridPapers.Rows(e.RowIndex).Cells(3).Value = OrigPay(e.RowIndex)
               DbGridPapers.Rows(e.RowIndex).Cells(4).Value = OrigCountedImage(e.RowIndex)
               GoTo pCounter ''Return Original Cells Value
            Else
               FILEpages.Add(pages)''GET single page only
            End If
          End If
       End If   

pCounter:
       Dim paperToTpay As Decimal = txtpapertotpay.Text.Substring(0, txtpapertotpay.Text.LastIndexOf(" ")) 
       paperToTpay -= OldRegularPrice
       paperToTpay += DbGridPapers.Rows(e.RowIndex).Cells(3).Value
       MyThreadedControl(txtpapertotpay, "Text", paperToTpay.ToString & " dollar(s)")
    End If 
End Sub

我认为它非常难。

4

2 回答 2

1

描述

目前尚不清楚您要查找的内容,但是此 powershell 解决方案显示了我将如何解决该问题以允许用户输入0,2-5,4,8,9以忽略零位和多余的冗余数字的逻辑。

例子

$string = "0,2-5,4,8,9"

[hashtable]$hashPages = @{}

foreach ($chunk in $String -split ",") {

    # if the string has a dash then process it as a range
    if ($chunk -match "(\d+)-(\d+)") {
        # itterate through all the pages in the range
        foreach ($Page in $Matches[1] .. $Matches[2]) {
            # insert this page into a hash, which will keep the numbers unique
            $hashPages[[string]$Page] = $true
            } # next page
        } # end if

    # if string is only a number then process it as a single number
    if ($chunk -match "(\d+)") {
        # insert this page into a hash, which will keep the numbers unique
        $hashPages[[string]$Matches[1]] = $true
        } # end if

    } # next chunk

# remove the undesireable numbers like zero if they were added
$hashPages.Remove("0");

Write-Host "these pages where requested:" $(($hashPages.Keys | sort ) -join ",")

输出

these pages where requested: 2,3,4,5,8,9
于 2013-07-07T15:35:33.143 回答
1

我了解自定义算法是可以接受的。在这里,您有一种方法可以考虑所有描述的条件:

Private Function extractPages(ByVal inputString As String) As List(Of Integer)
    Dim outList As List(Of Integer) = New List(Of Integer)

    If (inputString.Contains(",")) Then
        outList = extractCommas(inputString, outList)
    ElseIf (inputString.Contains("-")) Then
        outList = extractDashes(inputString, outList)
    End If

    If (outList.Count > 0) Then
        For i As Integer = outList.Count - 1 To 0 Step -1
            If (outList.IndexOf(outList(i)) <> outList.LastIndexOf(outList(i))) Then
                'Repeated item
                'It can be just deleted or shall the function return an error?
                outList.RemoveAt(i)
            End If
        Next
    End If

    Return outList
End Function

Private Function extractCommas(ByVal inputString As String, curList As List(Of Integer)) As List(Of Integer)

    If (inputString.Contains(",")) Then
        Dim temp() As String = inputString.Split(",")
        For Each item In temp
            If (Not item.Contains("-") And IsNumeric(item)) Then
                If (Convert.ToInt32(item.Trim()) > 0) Then
                    curList.Add(Convert.ToInt32(item.Trim()))
                End If
            ElseIf (item.Contains("-")) Then
                curList = extractDashes(item.Trim(), curList)
            End If
        Next
    End If

    Return curList
End Function

Private Function extractDashes(ByVal inputString As String, curList As List(Of Integer)) As List(Of Integer)

    If (inputString.Contains("-")) Then
        Dim temp() = inputString.Split("-")
        If (temp.Length = 2) Then
            If (Convert.ToInt32(temp(0)) <= Convert.ToInt32(temp(1))) Then
                Dim count As Integer = Convert.ToInt32(temp(0)) - 1
                If (count < 0) Then
                    count = 0
                End If
                Do
                    count = count + 1
                    curList.Add(count)
                Loop While (count < Convert.ToInt32(temp(1)))
            End If
        End If
    End If

    Return curList
End Function

您可以调用extractPages并获取所有页码:

Dim InputString As String = "2-5, 4,8-10"
Dim allPages As List(Of Integer) = extractPages(InputString) 'It returns 2, 3, 4, 5, 8, 9, 10
于 2013-07-07T15:47:41.607 回答