Jump to content

Índice fora dos limites da matriz


viegasss
 Share

Recommended Posts

Public Class Form1
    Dim n1() As Integer
    Dim n2() As Integer
    Dim max As Integer
    Dim nome() As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        max = 0
        ReDim nome(max)
        ReDim n1(max)
        ReDim n2(max)
        nome(0) = "Ana"
        n1(0) = 11
        n2(0) = 12
        ListBox1.Items.Add(nome(0))
        ListBox1.SelectedIndex = 0
        Label7.Text = nome(0)
        Label8.Text = n1(0)
        Label9.Text = n2(0)
        Label10.Text = ((n1(0) + n2(0)) / 2)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        max = max + 1
        ReDim Preserve nome(max)
        ReDim Preserve n1(max)
        ReDim Preserve n2(max)
        nome(max) = InputBox("Nome:", "Nome")
        n1(max) = InputBox("Introduza a nota", "Primeira Nota")
        n2(max) = InputBox("Introduza a nota", "Segunda Nota")
        ListBox1.Items.Add(nome(max))
        ListBox1.SelectedIndex = max
        Label7.Text = nome(max)
        Label8.Text = n1(max)
        Label9.Text = n2(max)
        Label10.Text = ((n1(max) + n2(max)) / 2)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim indx As Integer

        If ListBox1.SelectedIndex - 1 Then
            MsgBox("Selecione um elemento na lista", MsgBoxStyle.Information, "Tente de Novo")
        Else
            indx = ListBox1.SelectedIndex
            nome(indx) = InputBox("Introduza o nome", "Nome")
            n1(indx) = InputBox("Introduza a nota", "Primeira Nota")
            n2(indx) = InputBox("Introduza a nota", "Segunda nota")
            ListBox1.Items(indx) = nome(indx)
            Label7.Text = nome(max)
            Label8.Text = n1(max)
            Label9.Text = n2(max)
            Label10.Text = ((n1(max) + n2(max)) / 2)
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim i As Integer
      [b]  i = ListBox1.SelectedIndex
        Label7.Text = nome(i)[/b] ---> dá-me um erro que diz "Índice fora dos limites da matriz."
        Label8.Text = n1(i)
        Label9.Text = n2(i)
        Label10.Text = ((n1(i) + (n2(i))) / 2)
    End Sub
End Class

ajudem sff

boa tarde!

Link to comment
Share on other sites

  Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        Dim i As Integer

        Label7.Text = nome(i)

        Label8.Text = n1(i)

        Label9.Text = n2(i)

        Label10.Text = ((n1(i) + (n2(i))) / 2)

      i = ListBox1.SelectedIndex > -1 --> Assim dá! só que na listbox só aparece uma vez depois se selecionar outro já nao dá

    End Sub

End Class

Link to comment
Share on other sites

se colocares este código:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim i As Integer
        i = ListBox1.SelectedIndex
        Debug.Print("Valor de i = " & i)
        Debug.Print("Tamanho do array = " & nome.Length)
        Label7.Text = nome(i) ---> dá-me um erro que diz "Índice fora dos limites da matriz."
        Label8.Text = n1(i)
        Label9.Text = n2(i)
        Label10.Text = ((n1(i) + (n2(i))) / 2)
    End Sub

O que é que ele escreve na immediate window?

Link to comment
Share on other sites

Tal como te disse tens que verificar se é o index é maior que -1:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim i As Integer
        i = ListBox1.SelectedIndex
        if i >-1 then
             Label7.Text = nome(i) 
             Label8.Text = n1(i)
             Label9.Text = n2(i)
             Label10.Text = ((n1(i) + (n2(i))) / 2)
        end if
    End Sub
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • 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.