很多文本框中的连续编号VB.Net

时间:2019-01-26 00:55:11

标签: vb.net

我有一个项目,我想在一个文本框中以连续的数字显示在另一个文本框中。我们的文本框将包含20个值。 示例:65,67,68,69,73,74,75,77,80 ..... 99(从最低到最高依次显示值)因此不再需要排列它们。也就是说,连续的数字始终从左到右,反之亦然。

在Textbox2.Text中,我想显示连续的数字:67,68,69,73,74,75

那么如何使用下面的代码来做到这一点?那么如何将其应用于文本框? (Textbox1Textbox2

Public Shared Function TestMethod() As String
        Dim _integers() As Integer = {1, 2, 3, 4, 10, 11, 15}
        Dim _temp As Integer = _integers(0)
        Dim _result As String = _temp.ToString()
        For i As Integer = 1 To _integers.Length - 1
            If (i + 1) = _integers.Length Then
                _result += " and " + _integers(i).ToString()
            ElseIf (_integers(i) - _temp) = 1 Then
                _result += ", " + _integers(i).ToString()
            Else
                _result += " through " + _integers(i).ToString()
            End If
            _temp = _integers(i)
        Next
        Return _result
End Function

那么如何将其应用于我的文本框? (Textbox1Textbox2

如何为2个连续数字或3个连续数字制作代码,

示例:Dim _integers()为Integer = {1、2、3、4、10、11、15}

2个连续数字:输出:Textbox2.Text

Textbox2.Text =行(1)= 1,2 行(2)= 2,3 行(3)= 10,11

3个连续的数字:输出:Textbox3.Text

Textbox3.Text = 行(1)= 1,2,3 行(2)= 2,3,4

1 个答案:

答案 0 :(得分:0)

就是...

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim number As Integer
    Dim numbers As New List(Of Integer)
    For Each strNum As String In TextBox1.Text.Split(",")
        If Integer.TryParse(strNum.Trim, number) Then
            numbers.Add(number)
        End If
    Next
    numbers.Sort()

    For seriesLength As Integer = 2 To numbers.Count
        Dim ctl As Control = Me.Controls.Find("TextBox" & seriesLength, True).FirstOrDefault
        If TypeOf (ctl) Is TextBox Then
            Dim tb As TextBox = DirectCast(ctl, TextBox)
            tb.Clear()
            For i As Integer = 0 To (numbers.Count - seriesLength)
                If (numbers(i + (seriesLength - 1)) - numbers(i)) = (seriesLength - 1) Then
                    tb.AppendText(String.Join(",", numbers.GetRange(i, seriesLength)) & vbCrLf)
                End If
            Next
        End If
    Next
End Sub

不确定为什么您的版本无法正常工作。

这是我的代码运行的屏幕截图: Posted Code Running Without Errors on My System