VB.NET从另一个类引用新表单对象的listview

时间:2013-06-05 13:46:17

标签: database vb.net forms listview sqldatareader

在表单上我有一个带有contextmenu的列表视图。

右键单击listview项目可显示上下文菜单。

当我点击menubutton时,我想要发生的是以下内容:

我将出现一个名为TaskLog的表单,它只包含一个列表视图 (这是我的代码):

Private Sub ShowLogToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ShowLogToolStripMenuItem.Click
    logform = New TaskLog()
    logform.task = "taskname"
    logform.server = "servername"
    logform.Show()
End Sub

列表视图应该在显示之前填充数据库中的数据,因此我在TaskLog表单的加载事件中有以下内容

Public Class TaskLog
 Private db As Database = New Database
 Public task As String
 Public server As String

Private Sub TaskLog_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    db.connect()
    db.getTaskLog(task, server, Me)
End Sub
End Class

然后在数据库类gettask函数中我调用数据库,将数据存储在一个对象中,这一切都正常。这里唯一的问题是我的数据库对象无法找到listview和表单,即使我将表单作为变量传递给此函数(frm as TaskLog)。

    Public Function getTaskLog(taskname As String, server As String, frm As TaskLog) As Boolean

    'Declare sql command variable
    Dim command As New SqlCommand

    'Try to open db connection
    Try
        connection.Open()
        command.Connection = connection

        'Set query to command object text
        command.CommandText = "select * from tasklog where ltrim(rtrim(server)) Like'" & Trim(server) & "%' and ltrim(rtrim(task)) = '" & Trim(taskname) & "'"
        'Declare data reading pbject
        Dim rdr As SqlDataReader = command.ExecuteReader()

        'Perform operations while rows are returned from database

        While rdr.Read()

            'Store database entry to TaskData Object 
            taskLogData = New TaskData(rdr("frequency").ToString, rdr("start"), rdr("duration"), rdr("delay"), rdr("rescheduled"), rdr("forced"), rdr("task"), rdr("server"), rdr("email"), rdr("message"), rdr("status"), rdr("logtime"))

            Dim new_item As New  _
            ListViewItem(Trim(taskLogData.task_server.ToString))
            new_item.SubItems.Add(Trim(taskLogData.task_name.ToString))
            new_item.SubItems.Add(Trim(taskLogData.task_status.ToString))
            new_item.SubItems.Add(Trim(taskLogData.task_frequency.ToString))
            new_item.SubItems.Add(Trim(taskLogData.task_nextrun.ToString))
            new_item.SubItems.Add(Trim(taskLogData.task_forced.ToString))
            new_item.SubItems.Add(Trim(taskLogData.task_rescheduled.ToString))
            new_item.SubItems.Add(Trim(taskLogData.task_rescheduled.ToString))

            new_item.Group = frm.ListView1.Groups(Trim(taskData.task_status.ToString))

            frm.ListView1.Items.Add(new_item)

        End While

        Return True

    Catch ex As Exception 'If connection fails return error message
        MessageBox.Show("Error while retrieving records on table..." & ex.Message, "Load Records")
        Return True

    Finally 'After connection close database 
        connection.Close()
    End Try

End Function

未设置为对象实例的对象引用显示在上述函数的以下行中:

    frm.ListView1.Items.Add(new_item)

有人能看到问题,希望有所帮助吗?我尝试了几件事,但仍然不确定这里最好的做法。谢谢!!!!!!!

1 个答案:

答案 0 :(得分:2)

这一行

 new_item.Group = frm.ListView1.Groups(Trim(taskData.task_status.ToString))

指的是ListView1中应存在的组。如果不需要,你可以将其评论