none
Double click on listbox item to display a different form RRS feed

  • Question

  • I am trying to display a different form for each whenever a listbox items are double clicked. But then i can double click on the items but then when i tried to display the form its showing the last item form only

    this is the code i have done so far: 

    Private Sub ProductListBox_DoubleClick(sender As Object, e As EventArgs) Handles ProductListBox.DoubleClick
            Dim mf As New Form1
            Dim pf As New ProductForm
    
            Dim file As String = ProductListBox.SelectedItem.ToString
            Dim myConn As SqlConnection
            With pf
                myConn = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|ProductSearchDatabase.mdf;Integrated Security=True")
                Dim query As String =
                    "SELECT prod_Name, prod_Price, prod_Type, prod_Image FROM Product WHERE prod_Name LIKE @prodname"
                myConn.Open()
                Dim sqlCommand As SqlCommand = New SqlCommand(query, myConn)
                sqlCommand.Parameters.AddWithValue("prodname", "%" & mf.SearchRichTextBox.Text.ToLower & "%")
                Dim dtAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
                Dim dtSet As New DataSet
                dtAdapter.Fill(dtSet, "Matched Item")
                Dim dtTable As DataTable = dtSet.Tables("Matched Item")
                Dim dr As DataRow
                Dim i As Integer = 0
                'If dtTable.Rows.Count > 0 Then
                'MsgBox(dtTable.Rows.Count & " items are found")
                For i = 0 To dtTable.Rows.Count - 1
                    dr = dtTable.Rows(i)
                    Dim item As String = dr("prod_Name")
                    Dim price As String = dr("prod_Price")
                    Dim type As String = dr("prod_Type")
                    Dim filename As String = dr("prod_Image")
                    'Me.ProductListBox.Items.Add(item)
                    pf.SearchProductLabel.Text = item
                    pf.SearchProdPriceLabel.Text = "RM: " & price
                    pf.SearchProdTypeLabel.Text = type
                    pf.SearchProductPictureBox.Image = Image.FromFile("Images/" & filename)
                    'Me.Show()
                    pf.Show()
                Next
                'End If
            End With
            'ProductForm.Show()
    
        End Sub

    can someone help me to solve this error

    Saturday, February 15, 2014 3:20 AM

Answers

  • If you want to base the search on a clicked item in a listbox, why are you accessing a text box?

    mf.SearchRichTextBox.Text.ToLower

    You appear to capture the selected value of the listbox into the "file" variable, but then do not use it.  So perhaps you meant to have:

    sqlCommand.Parameters.AddWithValue("prodname", "%" & file & "%")


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by raaj5671 Saturday, February 15, 2014 4:18 AM
    Saturday, February 15, 2014 4:15 AM
    Moderator

All replies

  • Hi,

    Are you referring with this?<see screenshot below>

    When the Form4.listbox doubleclick it returns to form5 control like textbox?

    Saturday, February 15, 2014 3:54 AM
  • yes but mine the result is link to a database. 

    once the listbox items is clicked.. example samsung note 3 is clicked the samsung note form should show but mine samsung galaxy tab 2 form is showing. i mean it always shows the last item forms only

    Saturday, February 15, 2014 4:04 AM
  • If you want to base the search on a clicked item in a listbox, why are you accessing a text box?

    mf.SearchRichTextBox.Text.ToLower

    You appear to capture the selected value of the listbox into the "file" variable, but then do not use it.  So perhaps you meant to have:

    sqlCommand.Parameters.AddWithValue("prodname", "%" & file & "%")


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by raaj5671 Saturday, February 15, 2014 4:18 AM
    Saturday, February 15, 2014 4:15 AM
    Moderator