none
How to pass to a listview data from other form RRS feed

  • Question

  • I have 2 forms form 1 and form 2 

    Cod on form 1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim lv As ListViewItem
            Form2.ListView1.Items.Add("Blah")
    I want to add data when i click on that button and get the data on listview on form 2
    Wednesday, May 2, 2018 9:31 AM

Answers

  • Khanna Gaurav I want to add a item without open another form



    Hi Rob12212,

    What is your mean? You can also use delegate to add a item in the another form.

    Form 1:

    Public Delegate Sub clickdelegatehander(item1 As String, item2 As String)
    Public Class Form1
        Public Event clickevent As clickdelegatehander
     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            RaiseEvent clickevent("A", "B")
        End Sub
    End Class

    Form 2:

    Public Class Form2
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListView1.View = View.Details
            ListView1.Columns.Add("Item Code")
            ListView1.Columns.Add("Item Count")
            Dim form1 As New Form1
            AddHandler form1.clickevent, AddressOf getmessage
            form1.Show()
        End Sub
      Public Sub getmessage(item1 As String, item2 As String)
            Dim row As String() = {"Item" & item1.ToString(), item2.ToString()}
            Dim listViewItem = New ListViewItem(row)
            ListView1.Items.Add(listViewItem)
        End Sub
    End Class

    Best Regards,

    Cherry


    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.

    • Marked as answer by Rob12212 Thursday, May 3, 2018 9:30 AM
    Thursday, May 3, 2018 5:24 AM
    Moderator

All replies

  • Try following code

    Private mForm2 as Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If mForm2 Is Nothing Then
    mForm2 = New Form2() mForm2.Show()
    End If mForm2.ListView1.Items.Add("Blah") End Sub

     


    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    Wednesday, May 2, 2018 9:39 AM
  • Khanna Gaurav I want to add a item without open another form



    • Edited by Rob12212 Wednesday, May 2, 2018 9:53 AM
    Wednesday, May 2, 2018 9:51 AM
  • Khanna Gaurav I want to add a item without open another form




    You must show (or at least create an instance of) Form2 at some point.  You need to hold a reference to Form2 when you create and/or show it.  Then you would do something like Khanna has shown.  Take his general example and adapt it for your needs.

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

    Wednesday, May 2, 2018 12:01 PM
    Moderator
  • Khanna Gaurav I want to add a item without open another form



    Hi Rob12212,

    What is your mean? You can also use delegate to add a item in the another form.

    Form 1:

    Public Delegate Sub clickdelegatehander(item1 As String, item2 As String)
    Public Class Form1
        Public Event clickevent As clickdelegatehander
     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            RaiseEvent clickevent("A", "B")
        End Sub
    End Class

    Form 2:

    Public Class Form2
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListView1.View = View.Details
            ListView1.Columns.Add("Item Code")
            ListView1.Columns.Add("Item Count")
            Dim form1 As New Form1
            AddHandler form1.clickevent, AddressOf getmessage
            form1.Show()
        End Sub
      Public Sub getmessage(item1 As String, item2 As String)
            Dim row As String() = {"Item" & item1.ToString(), item2.ToString()}
            Dim listViewItem = New ListViewItem(row)
            ListView1.Items.Add(listViewItem)
        End Sub
    End Class

    Best Regards,

    Cherry


    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.

    • Marked as answer by Rob12212 Thursday, May 3, 2018 9:30 AM
    Thursday, May 3, 2018 5:24 AM
    Moderator