Ivo Pereira Posted November 11, 2007 at 01:16 AM Report Share #146675 Posted November 11, 2007 at 01:16 AM tenho uma combobox, e com uma streamreader quero que cada linha da streamreader vá sendo adicionada à combobox... o objectivo é uma listagem de alunos... o código que eu tinha feito é este: Private Sub cmbAlunos_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbAlunos.SelectedIndexChanged Dim swreader As New StreamReader("C:\alunos.txt") swreader.ReadToEnd() Dim linha As String While Not swreader.EndOfStream linha = swreader.ReadLine cmbAlunos.Items.Add(linha) End While swreader.Close() End Sub o problema é que nao sei porque nao lista nada e ja agora gostava que alguem me ajudasse no codigo para a eliminaçao de uma linha por exemplo com um dos nomes da combobox... thanks 😛 Link to comment Share on other sites More sharing options...
Cybernavigator Posted November 11, 2007 at 01:50 AM Report Share #146677 Posted November 11, 2007 at 01:50 AM olha. da pa ler uma linha numerada? tipo, ler so a linha dois. se sim diz.me como k acho k t posso ajudar. Melhores cumprimentos,João Lopes***Esta mensagem foi escrita ao abrigo do novo Acordo Ortográfico*** Link to comment Share on other sites More sharing options...
Ivo Pereira Posted November 11, 2007 at 02:00 AM Author Report Share #146678 Posted November 11, 2007 at 02:00 AM se dá eu nao sei :/ se calhar só fazendo um loop... Link to comment Share on other sites More sharing options...
Cybernavigator Posted November 11, 2007 at 04:06 PM Report Share #146732 Posted November 11, 2007 at 04:06 PM Bom, encontrei isto pra ti: Dim TextLine As String Dim FILE_NAME As String = "C:\test.txt" If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) Do While objReader.Peek() <> -1 TextLine = TextLine & objReader.ReadLine() & vbNewLine Loop Textbox1.Text = TextLine Else MsgBox("File Does Not Exist") End If So inside the loop, we go round building up the TextLine variable. Once all the file has been read (when Peek() has a value of -1), we then place it into Textbox1. Melhores cumprimentos,João Lopes***Esta mensagem foi escrita ao abrigo do novo Acordo Ortográfico*** Link to comment Share on other sites More sharing options...
Ivo Pereira Posted November 11, 2007 at 05:36 PM Author Report Share #146742 Posted November 11, 2007 at 05:36 PM e para eliminar sabes? e já agora, para que serve o Peek? é verdade, também já tinha encontrado outra maneira de fazer: Dim swreader As New StreamReader("C:\alunos.txt") cmbAlunoEliminado.Items.AddRange(Split(swreader.ReadToEnd, _ vbCrLf)) cmbAlunoEliminado.Text = "Escolha o aluno a ser eliminado:" swreador.Close() pus isso no form_load e lá lista... mas por exemplo, eu tenho duas combos, uma para simplesmente mostrar os alunos e outra com possibilidade de se carregar num botão e o aluno em escolha ser eliminado, e queria que isso listasse os alunos para as duas combos, por isso tinha este codigo: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim swreader As New StreamReader("C:\alunos.txt") cmbAlunoEliminado.Items.AddRange(Split(swreader.ReadToEnd, _ vbCrLf)) cmbAlunoEliminado.Text = "Escolha o aluno a ser eliminado:" cmbAlunos.Items.AddRange(Split(swreader.ReadToEnd, _ vbCrLf)) cmbAlunos.Text = "Escolha o aluno:" swreader.Close() End Sub o problema é que não sei porquê só lista para o primeiro ? Link to comment Share on other sites More sharing options...
Tiago Salgado Posted November 11, 2007 at 10:59 PM Report Share #146818 Posted November 11, 2007 at 10:59 PM DJ_BloodHunter o teu codigo inical nao te listava nada pq estavas a mandar logo para o fim a posição no ficheiro. swreader.ReadToEnd() Link to comment Share on other sites More sharing options...
Ivo Pereira Posted November 12, 2007 at 09:35 PM Author Report Share #146977 Posted November 12, 2007 at 09:35 PM ah ? e para eliminar a linha será que sabes? ? 😛 Link to comment Share on other sites More sharing options...
Cybernavigator Posted November 13, 2007 at 07:15 PM Report Share #147194 Posted November 13, 2007 at 07:15 PM Espero k isto te ajude Dim FILE_NAME As String = "C:\test.txt" 'define o documento Dim TextLine As String If System.IO.File.Exists(FILE_NAME) = True Then 've se o ficheiro existe, se nao existir vai po else Dim objReader As New System.IO.StreamReader(FILE_NAME) Do While objReader.Peek() <> -1 'isto representa quando todo o ficheiro foi lido (peek fica com o valor -1) TextLine = TextLine & objReader.ReadLine() & vbNewLine 'aqui ele estah a escrever a linha, usa um loop pro fazer, eh aki k alteras o teu código Loop Textbox1.Text = TextLine 'atribui o valor da textline a uma textbox. Else MsgBox("File Does Not Exist") End If Melhores cumprimentos,João Lopes***Esta mensagem foi escrita ao abrigo do novo Acordo Ortográfico*** Link to comment Share on other sites More sharing options...
Ivo Pereira Posted November 13, 2007 at 09:41 PM Author Report Share #147235 Posted November 13, 2007 at 09:41 PM podias explicar o codigo sff? Link to comment Share on other sites More sharing options...
Cybernavigator Posted November 13, 2007 at 11:26 PM Report Share #147258 Posted November 13, 2007 at 11:26 PM Experimenta ler agora, eu não sei pk posto sem comentários, vou ter de os meter depois... Melhores cumprimentos,João Lopes***Esta mensagem foi escrita ao abrigo do novo Acordo Ortográfico*** Link to comment Share on other sites More sharing options...
Ivo Pereira Posted November 14, 2007 at 02:53 PM Author Report Share #147329 Posted November 14, 2007 at 02:53 PM mas isso é suposto ser para eliminar? Link to comment Share on other sites More sharing options...
Asgorath Posted November 14, 2007 at 03:59 PM Report Share #147340 Posted November 14, 2007 at 03:59 PM Sim retira o ReadToEnd. "The Dark Side Clouds Everthing. Impossible To See The Future Is."My rig: Intel Core 2 Quad Q9450 | abit IP35P | 4G Kingston 800 Mhz | XFX GeForce 9800 GX2 1G ddr3 | 2X WD5000AAJS 500Gb Sata 2 | PSU 600W || Caixa El-Diablo | Creative XMod Link to comment Share on other sites More sharing options...
Ivo Pereira Posted November 14, 2007 at 08:09 PM Author Report Share #147416 Posted November 14, 2007 at 08:09 PM não eliminou nada :dontgetit: Link to comment Share on other sites More sharing options...
Asgorath Posted November 14, 2007 at 11:36 PM Report Share #147481 Posted November 14, 2007 at 11:36 PM Jovem estas a usar o evento errado para preencher a combobox com os alunos. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '' prenche combo Dim swreader As New StreamReader("C:\alunos.txt") Dim linha As String = String.Empty While Not swreader.EndOfStream linha = swreader.ReadLine Me.ComboBox1.Items.Add(linha) End While swreader.Close() End Sub "The Dark Side Clouds Everthing. Impossible To See The Future Is."My rig: Intel Core 2 Quad Q9450 | abit IP35P | 4G Kingston 800 Mhz | XFX GeForce 9800 GX2 1G ddr3 | 2X WD5000AAJS 500Gb Sata 2 | PSU 600W || Caixa El-Diablo | Creative XMod Link to comment Share on other sites More sharing options...
Ivo Pereira Posted November 15, 2007 at 05:20 PM Author Report Share #147561 Posted November 15, 2007 at 05:20 PM eu ja consegui preencher a combobox, falta-me é para eliminar... Link to comment Share on other sites More sharing options...
Ivo Pereira Posted November 18, 2007 at 12:30 PM Author Report Share #148324 Posted November 18, 2007 at 12:30 PM e sabes-me dizer como, por exemplo listar os valores do textfile para duas comboboxes? é que se faço para uma aquilo lê até ao fim e depois não sei voltar ao principio para ler a outra... Link to comment Share on other sites More sharing options...
Cybernavigator Posted November 18, 2007 at 05:47 PM Report Share #148417 Posted November 18, 2007 at 05:47 PM Olha, podes-me fazer um favor e outro a ti? Comenta a parte k tá a funcar do teu código e manda aí o source, acho k te consigo resolver, mas tenho de perceber melhor o código... Aight? Fico à espera... 🙂 Melhores cumprimentos,João Lopes***Esta mensagem foi escrita ao abrigo do novo Acordo Ortográfico*** Link to comment Share on other sites More sharing options...
Ivo Pereira Posted November 18, 2007 at 06:59 PM Author Report Share #148435 Posted November 18, 2007 at 06:59 PM Imports System.IO Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'declara o swreader1 como um StreamReader Dim swreader1 As New StreamReader("C:\alunos.txt") 'Faz um while enquanto o EndOfStream não for -1 ele adiciona tudo o que está separado por uma linha 'à combobox denominada de cmbAlunoEliminado, mas como lê tudo até ao fim para preencher essa combobox 'depois não lê para a outra Do While swreader1.EndOfStream = -1 cmbAlunoEliminado.Items.AddRange(Split(swreader1.ReadToEnd, _ vbCrLf)) cmbAlunos.Items.AddRange(Split(swreader1.ReadToEnd, _ vbCrLf)) 'fecha o StreamReader swreader1.Close() Loop End Sub Private Sub btnAddAluno_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddAluno.Click 'declara a variavel swriter como um novo StreamWriter ao carregar no botao para adicionar o aluno Dim swriter As New StreamWriter("C:\alunos.txt", FileMode.Append) 'se o TextBox denominado txtNomeAluno estiver em branco o erro irá soar... If txtNomeAluno.Text = "" Then MsgBox("Por favor digite o nome do aluno a ser adicionado.", MsgBoxStyle.Information, "Gestão de Alunos Virtuais") Else 'caso contrário, ERA SUPOSTO, escrever o nome do aluno noutra linha, fazer refresh ao combobox 'de alunos que no principio do código não preencheu, e por fim fechar o StreamWriter swriter.Write(vbNewLine & txtNomeAluno.Text) cmbAlunos.Refresh() swriter.Close() 'Adicionaria também a informação numa StatusBar que eu acrescentei a dizer que o aluno teria sido adicionado, 'e deixaria o TextBox em branco, para possíveis adições futuras. stBarAlunos.Text = "O aluno " & txtNomeAluno.Text & " foi adicionado!" txtNomeAluno.Text = "" End If End Sub Private Sub btnDelAluno_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelAluno.Click 'declara-se de novo esta variavel ao carregar-se no botao de Eliminar Dim swriter As New StreamWriter("C:\alunos.txt", FileMode.Append) 'se o texto da combobox for igual ao pré-definido, logo não haverá nenhum aluno seleccionado para ser eliminado 'logo apresentará um erro If cmbAlunoEliminado.Text = "Escolha o aluno a ser eliminado:" Then MsgBox("Ainda não escolheu o aluno a ser eliminado.", MsgBoxStyle.Information, "Gestão de Alunos Virtuais") End If 'aqui falta-me o resto do código para eliminar o aluno seleccionado End Sub End Class aqui está.... faz bom proveito 😁 Link to comment Share on other sites More sharing options...
Revolt Posted November 19, 2007 at 06:45 PM Report Share #148691 Posted November 19, 2007 at 06:45 PM cmbAlunoEliminado.SelectedItem.Remove ou Dim selected as Integer = cmbAlunoEliminado.SelectedIndex cmbAlunoEliminado.Items.RemoveAt(selected) Revolt Website Antigo Nick: Pauzinhos Link to comment Share on other sites More sharing options...
Ivo Pereira Posted November 19, 2007 at 06:59 PM Author Report Share #148699 Posted November 19, 2007 at 06:59 PM e isso também remove do textfile? 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