Results 1 to 7 of 7

Thread: [2005] VB - Pass Selected ListView Item To Another Form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question [2005] VB - Pass Selected ListView Item To Another Form

    Hi All,

    I have a listview which is populated via SQL. When I select the Item index, I was to open a form which will populate from SQL based on the selected item index.

    e.g

    VB Code:
    1. Dim SQLSelect As String = "SELECT * FROM tblSuppliers WHERE [Supplier Code] = '" + lvwSuppliers.Items(lvwSuppliers.FocusedItem.Index).SubItems(0).Text + "' "

    Any suggestions?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] VB - Pass Selected ListView Item To Another Form

    Add a constructor to the form that takes a ListViewItem. When you create it you pass SelectedItems(0) from your ListView.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2005] VB - Pass Selected ListView Item To Another Form

    Why take the entire listviewitem? Just take the index.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question Re: [2005] VB - Pass Selected ListView Item To Another Form

    After a bit more research, this is what I have come up with. It seems to work as expected.

    Form 2:
    VB Code:
    1. Public Class Form2
    2.  
    3.     Private SelectedListCode As String
    4.  
    5.     Public Sub New(ByVal ListViewSelected As String)
    6.  
    7.         MyBase.New()
    8.         InitializeComponent()
    9.  
    10.         SelectedListCode = ListViewSelected
    11.  
    12.     End Sub
    13.  
    14.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    15.  
    16.         MsgBox(SelectedListCode)
    17.  
    18.     End Sub
    19.  
    20. End Class

    Form1:
    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.  
    5.  
    6.         If ListView1.SelectedItems.Count <= 0 Then
    7.  
    8.             MessageBox.Show("Item Not Selected")
    9.         Else
    10.  
    11.             Dim f2 As New Form2(ListView1.SelectedItems(0).Text)
    12.             f2.ShowDialog()
    13.  
    14.         End If
    15.        
    16.  
    17.     End Sub
    18.  
    19.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    20.  
    21.         ListView1.Columns.Add("This Is A Column")
    22.  
    23.         ListView1.Items.Add("Hello")
    24.         ListView1.Items.Add("GoodBye")
    25.  
    26.  
    27.     End Sub
    28. End Class
    Last edited by nolim8ts; Nov 20th, 2008 at 08:54 AM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] VB - Pass Selected ListView Item To Another Form

    Quote Originally Posted by MaximilianMayrhofer
    Why take the entire listviewitem? Just take the index.
    Good point. I didn't really read the code so didn't see how it was being used.

    I said to use the first SelectedItem, not the FocusedItem. They may or may not be the same object, depending on the circumstances. In your circumstances they are not.

    Also, as Max says, there's no point passing more than you need. If all you need is the Text of the subitem then that's all you should be passing. Not the whole subitem.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Re: [2005] VB - Pass Selected ListView Item To Another Form

    Updated Code in Post #4

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] VB - Pass Selected ListView Item To Another Form

    Looks pretty good. To be safe you should test to make sure that there actually is an item selected before using the first selected item. If that code is executed before the user selects an item then an exception will be thrown.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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