在For Next循环中断行

时间:2014-03-22 08:50:04

标签: vb.net loops for-loop

我有2个嵌套循环,如下所示。

对应的变量x和y是x轴,y轴。当x从1到10运行然后退回类似的行

例如:

x = (1 -> 10) then y = 1

x = (11 -> 20) then y = 2

x = (xn -> XNN) then y = n

就像这样

Private Sub R1(ByRef RoomTotal As Integer, ByRef RoomInLine As Integer, ByRef RoomW As Integer, RoomH As Integer, ByRef RoomPrefix As String, ByRef RoomTopLeft As Integer, RoomMargin As Integer)

    Dim TotalRoomLine As Integer
    If RoomTotal Mod RoomInLine <> 0 Then
        TotalRoomLine = RoomTotal \ RoomInLine + 1
    Else
        TotalRoomLine = RoomTotal / RoomInLine
    End If

    For y As Integer = 1 To TotalRoomLine Step 1

            For x As Integer = 1 To RoomTotal Step 1

            Dim RoomNo As New Button
            If x <= RoomInLine Then
                RoomNo.Top = RoomTopLeft
                RoomNo.Left = RoomTopLeft + (RoomW * (x - 1)) + (RoomMargin * (x - 1))
                RoomNo.ForeColor = SystemColors.ControlLightLight
            ElseIf RoomInLine < x <= (RoomInLine * y) Then
                RoomNo.Top = RoomTopLeft + (RoomH * (y - 1)) + (RoomMargin * (y - 1))
                RoomNo.Left = RoomTopLeft + (RoomW * (x - 1)) + (RoomMargin * (x - 1))
                RoomNo.ForeColor = Color.Green
            End If

                RoomNo.Width = RoomW
                RoomNo.Height = RoomH
                RoomNo.Name = RoomPrefix & Convert.ToString(x)
                RoomNo.Text = RoomPrefix & Convert.ToString(x)
                RoomNo.BackColor = SystemColors.ControlDark

                RoomNo.FlatStyle = FlatStyle.Flat
                RoomNo.Cursor = Cursors.Hand
                Me.Controls.Add(RoomNo)
            Next
        Next

End Sub


i call function: R1(36, 10, 100, 100, "ROOM", 100, 5)

只有第一行显示正确使用2它开始显示不正确的

这是在http://imageshack.com/a/img34/9489/drue.png

上运行代码时的结果

我希望它像这个新的精确

一样运行

http://i.stack.imgur.com/ZYTFW.png

1 个答案:

答案 0 :(得分:0)

你有太多的循环。试试这个:

For iRoom As Integer = 0 To RoomTotal - 1

    Dim RoomNo As New Button
    Dim x As Integer = iRoom Mod RoomInLine
    Dim y As Integer = iRoom \ RoomInLine
    RoomNo.Top = RoomTop + (RoomH + RoomMargin) * y
    RoomNo.Left = RoomLeft + (RoomW + RoomMargin) * x

    RoomNo.Width = RoomW
    RoomNo.Height = RoomH
    RoomNo.Name = RoomPrefix & Convert.ToString(iRoom + 1)
    RoomNo.Text = RoomPrefix & Convert.ToString(iRoom + 1)
    RoomNo.BackColor = SystemColors.ControlDark

    RoomNo.FlatStyle = FlatStyle.Flat
    RoomNo.Cursor = Cursors.Hand
    Me.Controls.Add(RoomNo)

Next