none
visual basic 2010 i want this today RRS feed

  • Question

  • when we select one option the listbox1 then that relevant photo goes to picture box . then the value of that picture in then picture box moves to listbox2.if we change a picture in the picture box  how to removes value on listbox2 ?

    Public Class Form1
        Dim rosa(11) As String
        Dim c(11) As Integer
        Dim x As Integer
        Dim trovato As Boolean
        Dim j As Integer
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            rosa(0) = "Andrea Pirlo"
            rosa(1) = "Beckem"
            rosa(2) = "Marcelo"
            rosa(3) = "Ronaldinho"
            rosa(4) = "Cristiano Ronaldo"
            rosa(5) = "Moharmed Salah"
            rosa(6) = "Luis"
            rosa(7) = "Lionel Messi"
            rosa(8) = "Eden Hazard"
            rosa(9) = "Neymar"
            rosa(10) = "Zlatan"
            rosa(11) = "Davide"
            For i = 0 To 11
                ListBox1.Items.Add(rosa(i))
            Next
            For i = 0 To 11
                c(i) = 99
            Next
            For i = 0 To 11
                ListBox2.Items.Add(c(i))
            Next
           
        End Sub
        Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            trovato = False
            For i = 0 To 11
                If ListBox1.SelectedIndex = c(i) Then
                    trovato = True
                End If
            Next
            If trovato = False And x > 0 Then
                ' MsgBox("NON trovato")
                c(j) = ListBox1.SelectedIndex
                j = j + 1
                If x = 1 Then
                    player1.Image = ImageList1.Images(ListBox1.SelectedIndex)
                End If
                If x = 2 Then
                    player2.Image = ImageList1.Images(ListBox1.SelectedIndex)
                End If
                If x = 3 Then
                    player3.Image = ImageList1.Images(ListBox1.SelectedIndex)
                End If
                If x = 4 Then
                    player4.Image = ImageList1.Images(ListBox1.SelectedIndex)
                End If
                If x = 5 Then
                    player5.Image = ImageList1.Images(ListBox1.SelectedIndex)
                End If
                If x = 6 Then
                    player6.Image = ImageList1.Images(ListBox1.SelectedIndex)
                End If
                If x = 7 Then
                    player7.Image = ImageList1.Images(ListBox1.SelectedIndex)
                End If
                If x = 8 Then
                    player8.Image = ImageList1.Images(ListBox1.SelectedIndex)
                End If
                If x = 9 Then
                    player9.Image = ImageList1.Images(ListBox1.SelectedIndex)
                End If
                If x = 10 Then
                    player10.Image = ImageList1.Images(ListBox1.SelectedIndex)
                End If
                If x = 11 Then
                    player11.Image = ImageList1.Images(ListBox1.SelectedIndex)
                End If
                x = 0
            Else
                'MsgBox("Trovato")
            End If
            ListBox2.Items.Clear()
            For i = 0 To 11
                ListBox2.Items.Add(c(i))
            Next
            Label1.Text = ListBox1.SelectedIndex
        End Sub
        Private Sub player1_Click(sender As System.Object, e As System.EventArgs) Handles player1.Click
            x = 1
        End Sub
        Private Sub player2_Click(sender As System.Object, e As System.EventArgs) Handles player2.Click
            x = 2
        End Sub
        Private Sub player3_Click(sender As System.Object, e As System.EventArgs) Handles player3.Click
            x = 3
        End Sub
        Private Sub player4_Click(sender As System.Object, e As System.EventArgs) Handles player4.Click
            x = 4
        End Sub
        Private Sub player5_Click(sender As System.Object, e As System.EventArgs) Handles player5.Click
            x = 5
        End Sub
        Private Sub player6_Click(sender As System.Object, e As System.EventArgs) Handles player6.Click
            x = 6
        End Sub
        Private Sub player7_Click(sender As System.Object, e As System.EventArgs) Handles player7.Click
            x = 7
        End Sub
        Private Sub player8_Click(sender As System.Object, e As System.EventArgs) Handles player8.Click
            x = 8
        End Sub
        Private Sub player9_Click(sender As System.Object, e As System.EventArgs) Handles player9.Click
            x = 9
        End Sub
        Private Sub player10_Click(sender As System.Object, e As System.EventArgs) Handles player10.Click
            x = 10
        End Sub
        Private Sub player11_Click(sender As System.Object, e As System.EventArgs) Handles player11.Click
            x = 11
        End Sub
    End Class

    Tuesday, December 3, 2019 8:06 PM

All replies

  • Hi

    First of all, put these 2 Options at the top of your code, then correct all the issues that become apparent, then post back here with amended code.

    BTW: are you demanding a solution TODAY? If so, then best be quick with the editing you will need to do.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim rosa(11) As String
    	Dim c(11) As Integer
    	Dim x As Integer
    	Dim trovato As Boolean
    	Dim j As Integer


    Regards Les, Livingston, Scotland

    Tuesday, December 3, 2019 8:38 PM
  • Hi,

    Based on your ideas, I made the following modifications to your code.

    I mainly use Dictionary, and comments where needed are already added.

    You can make a reference and hope it will help you.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim rosa(3) As String 'Listbox1.Item
        Dim c(3) As Integer 'Listbox2.Item and picture's value 
    
        Dim rosa_c As New Dictionary(Of String, Integer)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
            rosa(0) = "Andrea Pirlo"
            rosa(1) = "Beckem"
            rosa(2) = "Marcelo"
            rosa(3) = "Ronaldinho"
    
            For i = 0 To 3
                ListBox1.Items.Add(rosa(i))
                c(i) = i
                ListBox2.Items.Add(c(i))
                rosa_c.Add(rosa(i), c(i))
            Next
    
        End Sub
        Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    
            'relevant photo goes to relevant picturebox
            For Each control As PictureBox In Me.Controls.OfType(Of PictureBox)
                Dim playerindex As String = "player"
                playerindex &= ListBox1.SelectedIndex
    
                If control.Name = playerindex Then
                    control.Image = ImageList1.Images(ListBox1.SelectedIndex)
                End If
            Next
    
            'convert relevant value of diffent picture to listbox2
            ListBox2.Items.Clear()
            For Each item In rosa_c
                If item.Key = ListBox1.SelectedItem.ToString Then
                    ListBox2.Items.Add(item.Value)
                End If
            Next
    
            'change label1.text
            Label1.Text = CType(ListBox1.SelectedIndex, String)
        End Sub
    
        'removes value on listbox2 when change a picture in the picture box
        Private Sub player1_Click(sender As System.Object, e As System.EventArgs) Handles player1.Click
    
            player1.Image = ImageList1.Images(0)
            For Each item In rosa_c
                If item.Key = ListBox1.SelectedItem.ToString Then
                    ListBox2.Items.Remove(item.Value)
                End If
            Next
        End Sub
    
        Private Sub player2_Click(sender As System.Object, e As System.EventArgs) Handles player2.Click
            player2.Image = ImageList1.Images(0)
            For Each item In rosa_c
                If item.Key = ListBox1.SelectedItem.ToString Then
                    ListBox2.Items.Remove(item.Value)
                End If
            Next
        End Sub
    
        Private Sub player3_Click(sender As System.Object, e As System.EventArgs) Handles player3.Click
            player3.Image = ImageList1.Images(0)
            For Each item In rosa_c
                If item.Key = ListBox1.SelectedItem.ToString Then
                    ListBox2.Items.Remove(item.Value)
                End If
            Next
        End Sub
    
    End Class

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Alex Li-MSFT Tuesday, December 10, 2019 7:21 AM
    Wednesday, December 4, 2019 8:04 AM
    Moderator