Results 1 to 10 of 10

Thread: VB.NET: Trigger Event inside Listbox....[Resolved]

  1. #1

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    VB.NET: Trigger Event inside Listbox....[Resolved]

    Does anyone know how to trigger the event inside the listbox when i double click on it....

    Say when i double click on one of the items, it will bring me to another form...

    Thanks
    Last edited by toytoy; Nov 24th, 2004 at 10:07 AM.

  2. #2
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494
    Try adding code to the DoubleClick event?

    And use the listbox's selected item property to retrieve the value to pass to the form you want to open.

    Expand on the problem a little more if you want more advice
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

  3. #3

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    can post some examples on doing it....

    I am confuse...

    Thanks

  4. #4
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494
    Before I do, are the items in your listbox just strings?
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

  5. #5
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494
    Okay, this example assumes you have a list box containing five customers. It also assumes you have a customer form with a public variable called CustomerID.

    VB Code:
    1. Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
    2.         Dim customerForm As New frmCustomer
    3.         Select Case ListBox1.SelectedItem.ToString
    4.             Case "Customer 1"
    5.                 customerForm.CustomerID = 1
    6.             Case "Customer 2"
    7.                 customerForm.CustomerID = 2
    8.             Case "Customer 3"
    9.                 customerForm.CustomerID = 3
    10.             Case "Customer 4"
    11.                 customerForm.CustomerID = 4
    12.             Case "Customer 5"
    13.                 customerForm.CustomerID = 5
    14.         End Select
    15.         customerForm.Show()
    16.     End Sub
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

  6. #6

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    Lil Ms Squirrel thanks...it works..

    how about if the items is Integer.....how to do it ?..

    now i have another problem..

    say double click on one of the items in the listbox from Form1, it will bring me to Form2..

    but from Form2, how to link back to form1 again...

    This is my coding..

    Code:
      'Inside Form1 
    
       Public vForm2 As New Form2
       Public vForm3 As New Form3
    
        Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
    
            Dim form2 As New Form2
            Dim form3 As New Form3
            Select Case ListBox1.SelectedItem.ToString
                Case "Form2"
                    form2.Show()
                    vForm2.Owner = Me
                    Me.Hide()
                Case "Form3"
                    form3.Show()
                    vForm3.Owner = Me
                    Me.Hide()
            End Select
     End Sub
    
    'Inside Form2  
    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
            Me.Owner.Show()
        End Sub
    
    'Inside Form3  
        Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
            Me.Owner.Show()
        End Sub
    The above code cannot works....it give an error:
    Code:
    Additional information: Object reference not set to an instance of an object.
    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 07:46 AM.

  7. #7
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494
    You've gotten your variables a bit mixed up.

    Either:

    VB Code:
    1. 'Inside Form1
    2.  
    3.    Public vForm2 As New Form2
    4.    Public vForm3 As New Form3
    5.  
    6.     Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
    7.  
    8.        Select Case ListBox1.SelectedItem.ToString
    9.             Case "Form2"
    10.                 vForm2.Show()
    11.                 vForm2.Owner = Me
    12.                 Me.Hide()
    13.             Case "Form3"
    14.                 vForm3.Show()
    15.                 vForm3.Owner = Me
    16.                 Me.Hide()
    17.         End Select
    18.  End Sub

    Or

    VB Code:
    1. 'Inside Form1
    2.  
    3.     Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
    4.  
    5.         Dim form2 As New Form2
    6.         Dim form3 As New Form3
    7.         Select Case ListBox1.SelectedItem.ToString
    8.             Case "Form2"
    9.                 form2.Show()
    10.                 form2 .Owner = Me
    11.                 Me.Hide()
    12.             Case "Form3"
    13.                 form3.Show()
    14.                 form3 .Owner = Me
    15.                 Me.Hide()
    16.         End Select
    17.  End Sub

    You were using a mixture of the two, hence when you asked form2 what it's Owner was, you were referring to a different object than the one you'd set the property for.

    Hope this helps
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

  8. #8

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    thanks....

    but both your methods cannot work..

    it still give the same error..

  9. #9
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494
    I hate to sound pedantic but have you copied and pasted the code into your project? They both work in the project I have here so maybe we are at cross purposes somewhere :

    Post back what you NOW have in your form and I'll take another look
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

  10. #10

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    ooh.....it is my error..

    it works...

    I left out some coding after i double check it...


    thanks
    Last edited by toytoy; Nov 24th, 2004 at 10:19 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width