Stocker la Réponse d'une ListBox dans une Variable

Profil bloqué - 31 mai 2011 à 14:32
 Profil bloqué - 31 mai 2011 à 23:08
Bonjour, je souhaiterais savoir comment récupérer la ligne sélectionnée d'une ListBox et la renvoyer dans une variable. Puis il faudrait renvoyer la variable dans un ficier texte. Merci.
Voilà mon code
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



        ' End Sub
        'Private Sub ReadFile()
        Dim You As String
        Dim listitems As New List(Of String)
        Dim pathfichier As String
        You = FreeFile()
        'Dim List1 As New ListBox
        ' Create an instance of the ListBox.
        Dim listBox1 As New Windows.Forms.ListBox()
        ' Set the size and location of the ListBox.
        listBox1.Size = New System.Drawing.Size(209, 103)
        listBox1.Location = New System.Drawing.Point(-2, 86)
        ' Add the ListBox to the form.
        Me.Controls.Add(listBox1)
        ' Set the ListBox to display items in multiple columns.
        listBox1.MultiColumn = True
        ' Set the selection mode to multiple and extended.
        'listBox1.SelectionMode = SelectionMode.MultiExtended

        ' Shutdown the painting of the ListBox as items are added.
        listBox1.BeginUpdate()

        'le chemin du fichier 
        pathfichier = "E:Modif.TXT"
        'je lis tout le fichier dans listitems 
        Try
            listitems = System.IO.File.ReadAllLines(pathfichier).ToList
        Catch ex As Exception
            'MessageBox.Show()
            'MsgBox(listitems)
        End Try
        'copier à partir de la 3 ligne attention la premiere est indexée par 0 
        For iter = 2 To listitems.Count - 1
            listBox1.Items.Add(listitems(iter))
            ' Allow the ListBox to repaint and display the new items.
            listBox1.EndUpdate()
            'listBox1.SetSelected(0, True)
            'System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems(0).ToString(You))
            'MsgBox(You)

            '(0).ToString()
        Next
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class

12 réponses

cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
31 mai 2011 à 15:40
Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
        Dim x As String
        x = DirectCast(sender, ListBox).SelectedItem

    End Sub
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
31 mai 2011 à 16:04
Envois le code que tu utilises
0
Profil bloqué
31 mai 2011 à 16:06
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



        ' End Sub
        'Private Sub ReadFile()
        'Dim You As String
        Dim listitems As New List(Of String)
        Dim pathfichier As String
        'You = FreeFile()
        'Dim List1 As New ListBox
        ' Create an instance of the ListBox.
        Dim listBox1 As New Windows.Forms.ListBox()
        ' Set the size and location of the ListBox.
        listBox1.Size = New System.Drawing.Size(209, 103)
        listBox1.Location = New System.Drawing.Point(-2, 86)
        ' Add the ListBox to the form.
        Me.Controls.Add(listBox1)
        ' Set the ListBox to display items in multiple columns.
        listBox1.MultiColumn = True
        ' Set the selection mode to multiple and extended.
        'listBox1.SelectionMode = SelectionMode.MultiExtended

        ' Shutdown the painting of the ListBox as items are added.
        listBox1.BeginUpdate()

        'le chemin du fichier 
        pathfichier = "E:Modif.TXT"
        'je lis tout le fichier dans listitems 
        Try
            listitems = System.IO.File.ReadAllLines(pathfichier).ToList
        Catch ex As Exception
            'MessageBox.Show()
            'MsgBox(listitems)
        End Try
        'copier à partir de la 3 ligne attention la premiere est indexée par 0 
        For iter = 2 To listitems.Count - 1
            listBox1.Items.Add(listitems(iter))
            ' Allow the ListBox to repaint and display the new items.
            listBox1.EndUpdate()
            'listBox1.SetSelected(0, True)
            'System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems(1).ToString(You))
        Next

        'Dim Boy As String = "aaa"
        'Boy = listBox1.SelectedItem
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
    Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
        Dim x As String
        x = DirectCast(sender, ListBox).SelectedItem

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

        Dim listBox1 As New Windows.Forms.ListBox()
        Dim Boy As String = "aaa"
        Boy = listBox1.SelectedItem

        'listBox1.SelectedItem.ToString(Boy)

        'Dim curItem As String = listBox1.SelectedItem.ToString()
        'listBox1.SelectedValue.ToString(Boy)

        MsgBox(Boy)
    End Sub
End Class
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Profil bloqué
31 mai 2011 à 16:23
Le problème c'est que je crée ma liste moi-même et non avec le visuel car javais des problèmes.
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
31 mai 2011 à 16:32
Pourquoi tu déclares ta listbox at runtime
et non avec l'IDE (placer sur ta form ta listbox)
0
Profil bloqué
31 mai 2011 à 16:40
Car chu un noob :/
Stu arrive à me faire le bon code ce serait génial.
0
Profil bloqué
31 mai 2011 à 16:43
En ait je la crée en runtime car j'arrive pas à afficher son contenu en IDE
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
31 mai 2011 à 17:14
En ait je la crée en runtime car j'arrive pas à afficher son contenu en IDE


et en runtime tu sais afficher son contenu
tu sais au moins placer un listbox sur ta form
après modifie sa hauteur et largeur
0
Profil bloqué
31 mai 2011 à 18:19
Ba je te dit que en fait jarrive à placer ma listbox sur la form mais je n'arrive pas à faire afficher dedans mes Item que j'ai rajouté donc par exemple: Ceci ne fonctionne pas, je n'ai rien dans ma list quand je lance le programme:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



        ' End Sub
        'Private Sub ReadFile()
        'Dim You As String
        Dim listitems As New List(Of String)
        Dim pathfichier As String
        'You = FreeFile()
        'Dim List1 As New ListBox
        ' Create an instance of the ListBox.
        Dim listBox1 As New Windows.Forms.ListBox()
        ' Set the size and location of the ListBox.
        'listBox1.Size = New System.Drawing.Size(209, 103)
        'listBox1.Location = New System.Drawing.Point(-2, 86)
        ' Add the ListBox to the form.
        'Me.Controls.Add(listBox1)
        ' Set the ListBox to display items in multiple columns.
        listBox1.MultiColumn = True
        ' Set the selection mode to multiple and extended.
        'listBox1.SelectionMode = SelectionMode.MultiExtended

        ' Shutdown the painting of the ListBox as items are added.
        listBox1.BeginUpdate()

        'le chemin du fichier 
        pathfichier = "E:Modif.TXT"
        'je lis tout le fichier dans listitems 
        Try
            listitems = System.IO.File.ReadAllLines(pathfichier).ToList
        Catch ex As Exception
            'MessageBox.Show()
            'MsgBox(listitems)
        End Try
        'copier à partir de la 3 ligne attention la premiere est indexée par 0 
        For iter = 2 To listitems.Count - 1
            listBox1.Items.Add(listitems(iter))
            ' Allow the ListBox to repaint and display the new items.
            listBox1.EndUpdate()
            'listBox1.SetSelected(0, True)
            'System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems(1).ToString(You))
        Next

        'Dim Boy As String = "aaa"
        'Boy = listBox1.SelectedItem
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
    Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim x As String
        x = DirectCast(sender, ListBox).SelectedItem
        MsgBox(x)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        'Dim listBox1 As New Windows.Forms.ListBox()
        'Dim Boy As String = "aaa"
        'Boy = listBox1.SelectedItem

        'listBox1.SelectedItem.ToString(Boy)

        'Dim curItem As String = listBox1.SelectedItem.ToString()
        'listBox1.SelectedValue.ToString(Boy)

        'MsgBox(Boy)
    End Sub

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

    End Sub
End Class
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
31 mai 2011 à 20:10
Si tu as placé un listbox sur ta form
pourquoi écrire
Dim listBox1 As New Windows.Forms.ListBox()

ta lisbox existe déjà pas la peine de créer un autre

est ce que tu as besoin de lire le fichier à
chaque fois que tu cliques sur button1 ?

que doit faire ton button_click event ?
0
Profil bloqué
31 mai 2011 à 23:08
Merci mec c'est bon j'ai réussi.
0
Rejoignez-nous