viegasss Posted March 16, 2012 Report Share Posted March 16, 2012 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 More sharing options...
edmolko Posted March 16, 2012 Report Share Posted March 16, 2012 Tens que verificar se ListBox1.SelectedIndex > -1 Link to comment Share on other sites More sharing options...
viegasss Posted March 16, 2012 Author Report Share Posted March 16, 2012 no label7.text ainda dá erro diz que "Índice fora dos limites da matriz." Link to comment Share on other sites More sharing options...
edmolko Posted March 16, 2012 Report Share Posted March 16, 2012 Em debug, coloca um breakpoint nessa linha e verifica qual o length do array nome e qual o valor do ListBox1.SelectedIndex. Link to comment Share on other sites More sharing options...
viegasss Posted March 16, 2012 Author Report Share Posted March 16, 2012 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 More sharing options...
edmolko Posted March 16, 2012 Report Share Posted March 16, 2012 Não podes mudar de sítio a inicialização da variável i, caso contrário i vai ter sempre o valor 0 (por defeito). Tens é que tentar perceber porque é que o valor do SelectedIndex está fora da dimensão do array nomes Link to comment Share on other sites More sharing options...
viegasss Posted March 16, 2012 Author Report Share Posted March 16, 2012 existe algum método para Atualizar a listbox ? Link to comment Share on other sites More sharing options...
edmolko Posted March 16, 2012 Report Share Posted March 16, 2012 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 More sharing options...
viegasss Posted March 16, 2012 Author Report Share Posted March 16, 2012 http://imageshack.us/photo/my-images/210/semttulogo.jpg/ Está aqui uma print amigo! vê Link to comment Share on other sites More sharing options...
edmolko Posted March 16, 2012 Report Share Posted March 16, 2012 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 More sharing options...
viegasss Posted March 16, 2012 Author Report Share Posted March 16, 2012 http://imageshack.us/photo/my-images/692/semttulocaj.jpg/ Tipo no programa so dá para alterar o luis os outros não consigo alterar! Consegues ver qual é o problema está aqui um trabalho 🙂 Link to comment Share on other sites More sharing options...
viegasss Posted March 16, 2012 Author Report Share Posted March 16, 2012 https://rapidshare.com/files/3710618258/array_xD.rar Tens aqui o link do meu trabalho, vê sff Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now