Shorther a value Textbox in VB.Net [closed]

2019-09-26 05:44发布

How do I make this code shorter? to maximum value TxtConsecutiveNumber2.Lines(i)=("79,80") Thanks. I want to make this code short,, TxtConsecutiveCount will always be + 1'd value. TxtConsecutiveCount1.Text, TxtConsecutiveCount2.Text,TxtConsecutiveCount3.Txt and TxtConsecutiveNumber2.Lines(i) remains the same, but will be equal as follows.

   For i As Integer = 1 To Val(TxtConsecutiveNumber2Count.Text)
        If TxtConsecutiveNumber2.Lines(i) = ("1,2") Then
            TxtConsecutiveCount1.Text = Val(TxtConsecutiveCount1.Text) + Val(1)
        Else
        End If
        If TxtConsecutiveNumber2.Lines(i) = ("2,3") Then
            TxtConsecutiveCount2.Text = Val(TxtConsecutiveCount2.Text) + Val(1)
        Else
        End If
        If TxtConsecutiveNumber2.Lines(i) = ("3,4") Then
            TxtConsecutiveCount3.Text = Val(TxtConsecutiveCount3.Text) + Val(1)
        Else
        End If
        If TxtConsecutiveNumber2.Lines(i) = ("4,5") Then
            TxtConsecutiveCount4.Text = Val(TxtConsecutiveCount4.Text) + Val(1)
        Else
        End If
        If TxtConsecutiveNumber2.Lines(i) = ("5,6") Then
            TxtConsecutiveCount5.Text = Val(TxtConsecutiveCount5.Text) + Val(1)
        Else
        End If

This code should go up to.

    If TxtConsecutiveNumber2.Lines(i) = ("79,80") Then
        TxtConsecutiveCount79.Text = Val(TxtConsecutiveCount5.Text) + Val(1)
    Else
    End If

How?

标签: vb.net
1条回答
Rolldiameter
2楼-- · 2019-09-26 06:38

Sorry, but your question is still unclear, but i think i know what you want to achieve.

Dim iCount As Integer = Integer.Parse(TxtConsecutiveNumber2Count.Text)
Dim i As Integer = 0

For i = 1 to iCount
    Dim txt As TextBox = DirectCast(Me.Controls("TxtConsecutiveCount" & i), TextBox)
    If TxtConsecutiveNumber2.Lines(i) = String.Concat(i, ",", i+1) Then 
        Dim parts As String()  =  txt.Text.Split(",", StringSplitOptions.RemoveEmptyEntries)
        txt.Text = String.Join(",", parts.Select(Function(x) Integer.Parse(parts(j)) + 1))
    End If
Next
查看更多
登录 后发表回答