Common Function to populate ListView from various Forms

[b][red]This message was edited by Lennie at 2005-1-27 18:40:32[/red][/b][hr]
I have a few forms (Form1,Form2,Form3)which each has a ListView control to display data. In order to avoid having the same routine in all the forms, I want to create a generic common Function in a Module where it accept FormName and Listview name properties as pass-in parameters. I have no joy in getting it going. Thanks

Here is an sample of my script.

Form1, Form2 and Form3
Public Class Form1
Inherits System.Windows.Forms.Form
Dim GFormName As System.Windows.Forms.Form
Dim GListView As System.Windows.forms.Control


Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load

Dim vsql As String

vsql = "Select * from products"

GFormName = sender.name
GListView.Name = "ListProducts"
GFLoadListView(vsql, GFormName, GListView)


End Sub
End Class
......................
MODULE
Public Function GFLoadListView( _
ByVal iSql As String, _
ByRef GFormName As System.Windows.Forms.Form, _
ByRef GListView As System.Windows.Forms.Control)


Dim itmListItem As ListViewItem
Dim i As Short = 0
Dim lvwColumn As ColumnHeader

testing listview clearing
GFormName.GlistView.clear()

End Function





Comments

  • I understand the desire to reduce repetitive coding, but passing references to your controls to unrelated subroutines so they can be filled is rarely the proper way to do it. Doing so introduces tight coupling into your code that increases the number of components that must be changed each time you change your mind about how the data should be displayed on your form (and if you never change your mind about the way your data should be displayed, your design documents are a lot better than mine :)) There is usually a better way, though I don't know the best thing for your application.

    In any case, the code you posted doesn't work because of the way you're trying to access the ListView. You don't need a reference to the Form if you have a reference to the ListView. The following will work:

    [code]Public Sub GFLoadListView(ByRef GListView As ListView)
    GListView.Items.Clear()
    ' Do what you need to do
    End Sub[/code]
    : [b][red]This message was edited by Lennie at 2005-1-27 18:40:32[/red][/b][hr]
    : I have a few forms (Form1,Form2,Form3)which each has a ListView control to display data. In order to avoid having the same routine in all the forms, I want to create a generic common Function in a Module where it accept FormName and Listview name properties as pass-in parameters. I have no joy in getting it going. Thanks
    :
    : Here is an sample of my script.
    :
    : Form1, Form2 and Form3
    : Public Class Form1
    : Inherits System.Windows.Forms.Form
    : Dim GFormName As System.Windows.Forms.Form
    : Dim GListView As System.Windows.forms.Control
    :
    :
    : Private Sub Form1_Load( _
    : ByVal sender As System.Object, _
    : ByVal e As System.EventArgs) _
    : Handles MyBase.Load
    :
    : Dim vsql As String
    :
    : vsql = "Select * from products"
    :
    : GFormName = sender.name
    : GListView.Name = "ListProducts"
    : GFLoadListView(vsql, GFormName, GListView)
    :
    :
    : End Sub
    : End Class
    : ......................
    : MODULE
    : Public Function GFLoadListView( _
    : ByVal iSql As String, _
    : ByRef GFormName As System.Windows.Forms.Form, _
    : ByRef GListView As System.Windows.Forms.Control)
    :
    :
    : Dim itmListItem As ListViewItem
    : Dim i As Short = 0
    : Dim lvwColumn As ColumnHeader
    :
    : testing listview clearing
    : GFormName.GlistView.clear()
    :
    : End Function
    :
    :
    :
    :
    :
    :

  • [b][red]This message was edited by Lennie at 2005-1-29 5:52:1[/red][/b][hr]
    [b][red]This message was edited by Lennie at 2005-1-29 5:50:38[/red][/b][hr]
    : I understand the desire to reduce repetitive coding, but passing references to your controls to unrelated subroutines so they can be filled is rarely the proper way to do it. Doing so introduces tight coupling into your code that increases the number of components that must be changed each time you change your mind about how the data should be displayed on your form (and if you never change your mind about the way your data should be displayed, your design documents are a lot better than mine :)) There is usually a better way, though I don't know the best thing for your application.
    :
    : In any case, the code you posted doesn't work because of the way you're trying to access the ListView. You don't need a reference to the Form if you have a reference to the ListView. The following will work:
    :
    : [code]Public Sub GFLoadListView(ByRef GListView As ListView)
    : GListView.Items.Clear()
    : ' Do what you need to do
    : End Sub[/code]
    : : [b][red]This message was edited by Lennie at 2005-1-27 18:40:32[/red][/b][hr]
    :
    Thanks Baldusarius for your suggestions. I have tried it out althought the script running smoothly the listview in Form1 did not display any data. Here are the rest of the scripts that is supposed to populate the Listview in Form1

    Public Sub GFLoadListView(ByVal iSql as string,ByRef GListView As ListView)
    Dim itmListItem As ListViewItem
    Dim i As Short
    Dim lvwColumn As ColumnHeader

    adoConn = AdoConnect()

    Try
    With AdoCmd
    .Connection = New SqlClient.SqlConnection(adoConn)
    .Connection.Open()
    .CommandText = adoSql
    adoDataReader = .ExecuteReader
    End With

    GListView.Clear()

    For i = 0 To adoDataReader.FieldCount() - 1
    lvwColumn = New ColumnHeader
    lvwColumn.Text = adoDataReader.GetName(i)
    GListView.Columns.Add(lvwColumn)
    Next

    Do While adoDataReader.Read
    itmListItem = New ListViewItem
    itmListItem.Text = adoDataReader(0)

    For i = 1 To adoDataReader.FieldCount() - 1
    itmListItem.SubItems.Add(adoDataReader(i))
    End If
    Next

    GListView.Items.Add(itmListItem)
    Loop

    Catch ex As Exception
    MsgBox(ex.Message)
    End Try




  • : Form1 did not display any data.

    I don't know what the problem is, but you need to debug your code to find out. Put a breakpoint at the beginning of the routine and follow along until you find the issue.

    Does it work in the other forms? If so, you have a great starting point.

    Don't pass the ListView by reference. ListView is already a reference type, so you are passing a reference by reference, which is usually not what you want to do. You'd normally use ByRef for value types when you want the original instance to be affected by the code you're passing the reference to.
  • : : Form1 did not display any data.
    :
    : I don't know what the problem is, but you need to debug your code to find out. Put a breakpoint at the beginning of the routine and follow along until you find the issue.
    :
    : Does it work in the other forms? If so, you have a great starting point.
    :
    : Don't pass the ListView by reference. ListView is already a reference type, so you are passing a reference by reference, which is usually not what you want to do. You'd normally use ByRef for value types when you want the original instance to be affected by the code you're passing the reference to.
    :

  • [b][red]This message was edited by Lennie at 2005-1-29 14:33:39[/red][/b][hr]
    : : : Form1 did not display any data.
    : :
    : : I don't know what the problem is, but you need to debug your code to find out. Put a breakpoint at the beginning of the routine and follow along until you find the issue.
    : :
    : : Does it work in the other forms? If so, you have a great starting point.
    : :
    : : Don't pass the ListView by reference. ListView is already a reference type, so you are passing a reference by reference, which is usually not what you want to do. You'd normally use ByRef for value types when you want the original instance to be affected by the code you're passing the reference to.
    : :


    :
    :

    Hi, thanks for the suggestion. Answer to both your questions are YES. if the scrips reside within FORM1,FORM2 or FORM3, it works very well. But when the repeative routines are taken out and place in a common FUNCTION in MODULE LEVEL it runs without error message but doesn't populate the Listview control of the calling FORMs. I have tried debugging routine and couldn't pick up the problem due to being new to Dot Net.



  • : [b][red]This message was edited by Lennie at 2005-1-29 14:33:39[/red][/b][hr]
    : : : : Form1 did not display any data.
    : : :
    : : : I don't know what the problem is, but you need to debug your code to find out. Put a breakpoint at the beginning of the routine and follow along until you find the issue.
    : : :
    : : : Does it work in the other forms? If so, you have a great starting point.
    : : :
    : : : Don't pass the ListView by reference. ListView is already a reference type, so you are passing a reference by reference, which is usually not what you want to do. You'd normally use ByRef for value types when you want the original instance to be affected by the code you're passing the reference to.
    : : :
    :
    :
    : :
    : :
    :
    : Hi, thanks for the suggestion. Answer to both your questions are YES. if the scrips reside within FORM1,FORM2 or FORM3, it works very well. But when the repeative routines are taken out and place in a common FUNCTION in MODULE LEVEL it runs without error message but doesn't populate the Listview control of the calling FORMs. I have tried debugging routine and couldn't pick up the problem due to being new to Dot Net.
    :
    :
    :
    Thanks to all for helping. Got the problems sorted out.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories