Jump to content
Sign in to follow this  
Chamuanza

Dificuldade com Array

Recommended Posts

Chamuanza

Um Bem Aja a todos

Tenho o código que apresento a seguir e não consigo perceber qual a razão de não me aparecer na grelha a segunda leva dos números aleatórios

Quando inicio o primeiro array funciona e aparece nas labels os valores, depois de limpar a grade e fazer de novo o array funciona com novos dados mas não consigo que me apareçam na grade.

Agradeço ajuda. Obrigado

Option Strict On
Imports System.Drawing
Imports System.Windows.Forms.Label

Public Class Form1

    Dim Numeros As Integer = 81
    Dim Tab_numeros(81) As Integer

    Private SudokuSolution(81) As String

    Public Lbl_Array As Label() = New Label(81) {} ' Set the number you labels need

    Private Sub NovoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NovoToolStripMenuItem.Click

        ' Demo places 9 x 9 (81) lables on form
        Dim CNT As Integer
        Dim X As Integer
        Dim Y As Integer
        Dim H As Integer
        Dim W As Integer
        Dim r, c As Integer

        Y = Lbl_Master.Location.Y
        H = Lbl_Master.Height
        W = Lbl_Master.Width
        CNT = 1


        For c = 1 To 9
            X = Lbl_Master.Location.X

            For r = 1 To 9

                Lbl_Array(CNT) = New Label

                Lbl_Array(CNT).Location = New System.Drawing.Point(X, Y)

                Lbl_Array(CNT).Size = New System.Drawing.Size(W, H)

                Lbl_Array(CNT).AutoSize = Lbl_Master.AutoSize

                Lbl_Array(CNT).BackColor = Lbl_Master.BackColor

                Lbl_Array(CNT).BorderStyle = Lbl_Master.BorderStyle

                Lbl_Array(CNT).TextAlign = Lbl_Master.TextAlign

                Lbl_Array(CNT).ForeColor = Lbl_Master.ForeColor

                Lbl_Array(CNT).Font = Lbl_Master.Font

                '*******************************************************
                'Lbl_Array(CNT).Text = CStr(CNT)
                '*****************************************************

                Lbl_Array(CNT).Tag = CNT ' This is the clever bit so that we can find the index!

                Lbl_Array(CNT).Visible = True

                Lbl_Array(CNT).BringToFront()

                AddHandler Lbl_Array(CNT).Click, AddressOf lblArray_click

                Panel1.Controls.Add(Lbl_Array(CNT))
                X = (X + W) + 8 ' Note 8 is your offset in X
                CNT += 1
            Next
            Y = (Y + H) + 8 ' Note 8 is your offset in Y
        Next

        Call NumerosAleatorios()

        Call CarregarNumeros()

    End Sub

    Private Sub CarregarNumeros()

        For J As Integer = 1 To 81

            Lbl_Array(J).Text = CStr(Tab_numeros(J - 1))

        Next

    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        For x As Integer = 1 To 81
            Lbl_Array(x).Text = ""

        Next

        ' Array.Clear(Lbl_Array, 0, Lbl_Array.Length)


    End Sub
    Private Sub NumerosAleatorios()

        Dim i As Integer            'Var aux p/contador
        Dim Temp As Integer         'Var aux
        Dim flag As Boolean         'Flag de controle


        'Zerar vectores
        For i = 0 To Numeros - 1
            Tab_numeros(i) = 0
        Next


        'Gerar números
        i = 0
        Do While i < Numeros
            Randomize()                     'Baralhar
            Temp = CInt(Int((Rnd() * 81) + 1))    'def um nº à sorte entre 1 e 81
            'Verificar se já existe
            flag = False
            For j As Integer = 0 To Numeros - 1
                If Tab_numeros(j) = Temp Then
                    flag = True
                End If
            Next
            'se não, atribuir nº e actualizar contador
            If flag = False Then
                Tab_numeros(i) = Temp
                i += 1
            End If
        Loop

       
    End Sub

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.