Paulo Jorge Posted July 5, 2012 at 04:31 PM Report Share #467500 Posted July 5, 2012 at 04:31 PM (edited) Boa tarde a todos , estou a criar um programa com arraylist e estruturas que consiste em adicionar login e listar numa listview, eu já fiz a parte de adicionar mas estou agora com duvidas de como fazer para inserir os dados na listview alguém me ajuda ? Structure Login Dim Username As String Dim Password As String Dim Nivel As Integer End Structure Private Sub btnGravar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGravar.Click Dim lista_Login As New ArrayList Dim umLogin As Login umLogin.Username = txtUsenrame.Text umLogin.Password = txtPassword.Text umLogin.Nivel = cboNivel.Text lista_Login.Add(umLogin) End Sub Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click Me.Close() End Sub Edited July 5, 2012 at 04:33 PM by ribeiro55 Link to comment Share on other sites More sharing options...
ribeiro55 Posted July 5, 2012 at 04:37 PM Report Share #467508 Posted July 5, 2012 at 04:37 PM http://wiki.portugal-a-programar.pt/dev_net:vb.net:listviews_36_utilidades_directas#adicionar_itens Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips" Link to comment Share on other sites More sharing options...
Paulo Jorge Posted July 5, 2012 at 07:18 PM Author Report Share #467537 Posted July 5, 2012 at 07:18 PM (edited) coloquei assim : For Each i As Login In lista_Login Dim aux As New ListViewItem aux.SubItems.Add(i.Username) aux.SubItems.Add(i.Username) aux.SubItems.Add(i.Nivel) aux.Tag = i ListView1.Items.Add(aux) Next só que não aparece os campos direitos, aparece o username na coluna da password e do nivel e a password e nível nao aparece na listview, alguém sabe qual o erro ? Edited July 6, 2012 at 10:23 AM by ribeiro55 Link to comment Share on other sites More sharing options...
Caça Posted July 6, 2012 at 07:57 AM Report Share #467605 Posted July 6, 2012 at 07:57 AM Porque só estas a adicionar SubItems, lê melhor esse artigo. Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
Paulo Jorge Posted July 6, 2012 at 01:25 PM Author Report Share #467685 Posted July 6, 2012 at 01:25 PM (edited) já consigo listar ao adicionar mas ao carregar de um ficheiro não consigo listar da erro Private Sub btnCarregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCarregar.Click Dim fr As StreamReader = Nothing Try fr = New StreamReader(Application.StartupPath & "\Lista_Login.txt", System.Text.Encoding.Default) Dim linha As String linha = fr.ReadLine While linha <> Nothing linha = fr.ReadLine If linha IsNot Nothing Then lista_Login.Add(New Login()) End If End While fr.Close() MsgBox("Dados carregados com sucesso!!!") Catch ex As Exception If fr IsNot Nothing Then fr.Close() MsgBox("Erro ao carregar os dados do ficheiro!" & vbNewLine & ex.Message) End Try For Each i As Login In lista_Login Dim LVI As New ListViewItem Dim SLVI1 As New ListViewItem.ListViewSubItem Dim SLVI2 As New ListViewItem.ListViewSubItem LVI.Name = "Username" LVI.Text = umLogin.Username SLVI1.Name = "Password" SLVI1.Text = umLogin.Password SLVI2.Name = "Nivel" SLVI2.Text = umLogin.Nivel LVI.SubItems.Add(SLVI1) LVI.SubItems.Add(SLVI2) ListView1.Items.Add(LVI) Next End Sub Edited July 6, 2012 at 01:57 PM by ribeiro55 Link to comment Share on other sites More sharing options...
ribeiro55 Posted July 6, 2012 at 02:08 PM Report Share #467691 Posted July 6, 2012 at 02:08 PM Bem sei que no artigo está assim como colaste, mas podes abreviar para: For Each i As Login In lista_Login Dim LVI As New ListViewItem LVI.Text = umLogin.Username LVI.SubItems.Add(umLogin.Password) LVI.SubItems.Add(umLogin.Nivel) ListView1.Items.Add(LVI) Next Uma vez que não precisas de grande controlo. Quanto ao erro... como esperas que adivinhemos o erro? Em 99% dos casos não vamos agarrar no teu código e compilar. Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips" Link to comment Share on other sites More sharing options...
Paulo Jorge Posted July 6, 2012 at 03:11 PM Author Report Share #467710 Posted July 6, 2012 at 03:11 PM o que aparece na listview ao carregar e o campo username e password em branco e o nivel a 0 , mas o ficheiro txt tem dados Link to comment Share on other sites More sharing options...
ribeiro55 Posted July 6, 2012 at 04:15 PM Report Share #467723 Posted July 6, 2012 at 04:15 PM (edited) Faz todo o sentido que assim o seja. Olhando para o teu ciclo: For Each i As Login In lista_Login Dim LVI As New ListViewItem LVI.Text = umLogin.Username LVI.SubItems.Add(umLogin.Password) LVI.SubItems.Add(umLogin.Nivel) ListView1.Items.Add(LVI) Next Estás basicamente a dizer: "por cada i, que é um Login, de todos os items de lista_login" Até aqui tudo bem. Mas depois em baixo não estás a usar a referência "i". Troca para: For Each i As Login In lista_Login Dim LVI As New ListViewItem LVI.Text = i.Username LVI.SubItems.Add(i.Password) LVI.SubItems.Add(i.Nivel) ListView1.Items.Add(LVI) Next Edited July 6, 2012 at 04:17 PM by ribeiro55 Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips" Link to comment Share on other sites More sharing options...
Paulo Jorge Posted July 10, 2012 at 03:07 PM Author Report Share #468197 Posted July 10, 2012 at 03:07 PM (edited) continua ao carregar a só aparecer um valor no campo nivel e a ser o 0, que terá de mal este código ? Private Sub btnCarregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCarregar.Click Dim fr As StreamReader = Nothing Try fr = New StreamReader(Application.StartupPath & "\Lista_Login.txt", System.Text.Encoding.Default) Dim linha As String linha = fr.ReadLine While linha <> Nothing linha = fr.ReadLine If linha IsNot Nothing Then lista_Login.Add(New Login()) End If End While fr.Close() MsgBox("Dados carregados com sucesso!!!") Catch ex As Exception If fr IsNot Nothing Then fr.Close() MsgBox("Erro ao carregar os dados do ficheiro!" & vbNewLine & ex.Message) End Try For Each i As Login In lista_Login Dim LVI As New ListViewItem LVI.Text = i.Username LVI.SubItems.Add(i.Password) LVI.SubItems.Add(i.Nivel) ListView1.Items.Add(LVI) Next End Sub Edited July 10, 2012 at 03:11 PM by Caça 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