从类中向ListView添加项

时间:2013-11-20 16:58:03

标签: vb.net listview

我无法将某个项目添加到其中一个类的列表视图中。我错过了一些明显超出我头脑的东西。如果有人能帮助我理解我所缺少的东西,那就太好了。

我不知道需要提供多少细节。我想要做的是基本所以我知道我必须遗漏一些东西。我有一个类,我试图将项目添加到我在表单上的列表视图。没有抛出任何错误,但没有添加任何内容。

我甚至尝试在类中使用像frmGiveaways.lstAccounts.items.add(“wtf”)这样简单的东西,它不会抛出任何错误,行处理,但列表中没有任何项目。< / p>

有什么东西会让我无法做到这一点?

这是班级

    Imports Simple_IRC_Client.Delegates
    Imports System.Text.RegularExpressions

    ''' <summary>
    ''' Handles minimal IRC server response messages.
''' </summary>

Public Class OutputMessages : Implements IDisposable

#Region " Private Members "
    Private ReadOnly ColorQuit As Color = Color.FromArgb(102, 54, 31)
    Private ReadOnly ColorPrivmsg As Color = Color.FromArgb(76, 76, 76)
    Private ReadOnly ColorTopic As Color = Color.FromArgb(176, 55, 176)
    Private ReadOnly ColorKick As Color = Color.FromArgb(199, 50, 50)
    Private ReadOnly ColorUserEvent As Color = Color.FromArgb(128, 128, 128)
    Private WithEvents _ircConnection As InitiateConnection
    Private _mainView As MainView
    Private _window As RichTextBox
#End Region

#Region " Constructor "
    Public Sub New(ByVal mainView As MainView, ByVal ircConnection As InitiateConnection)
        _mainView = mainView
        _ircConnection = ircConnection
        _window = _mainView.rtbChannelView
    End Sub
#End Region

#Region " EventArghs "

    Private Sub ServerResponse(ByVal serverResponse As String) Handles _ircConnection.ServerResponseOutputEventArghs

        ' This setting has only been added for demonstration purposes of what raw data
        ' looks like.
        If _mainView.mnuMainMenuOptionsrawData.Checked Then
            OutputResponse(_window, serverResponse, Color.Black)
            Exit Sub
        End If

        Dim parts() As String = serverResponse.Split(" "c)
        Dim address As String = parts(0)

        Select Case parts(1)
            Case "PRIVMSG" : Privmsg(address, serverResponse.Substring(indexOf(serverResponse, 3) + 1).Substring(1))
            Case "JOIN" : Join(address)
            Case "PART", "QUIT" : Quit(address)
            Case "ERROR" : Disconnected()
            Case "332" : TopicOnjoin(serverResponse.Substring(indexOf(serverResponse, 4) + 1).Substring(1))
        End Select
    End Sub
#End Region

#Region " Private"

    ''' <summary>
    ''' Outputs a GUI message on me/user Privmsg.
    ''' </summary>
    ''' <param name="address">The source of the user's local host.</param>
    ''' <param name="message">The message text.</param>
    ''' <remarks>
    ''' Displays an output message to the normalview window with correct format and colouring on Me, 
    ''' User Privmsg.
    ''' </remarks>
    Private Sub Privmsg(ByVal address As String, ByVal message As String)
        Dim outputFormat As String = String.Format("<{0}> {1}", Split(address), message)
        OutputResponse(_window, outputFormat, Color.Black)
        Select Case message
            Case "" : _ircConnection.SendMessage(String.Format("PRIVMSG " & ConnectionInformation.Channel & " :" & "{0}", Split(address)))
            Case frmGiveaways.keyword
                _ircConnection.SendMessage(String.Format("PRIVMSG " & ConnectionInformation.Channel & " :" & "recieved keyword", Split(address)))
                frmGiveaways.lstAccountsEntered.Items.Add(Split(address))
        End Select

    End Sub

    Private Sub Join(ByVal address As String)
        If Split(address) = ConnectionInformation.ChannelNick Then
            Exit Sub
        End If

        Dim outputFortmat As String = String.Format("{0} has joined the conversation.", Split(address))
        OutputResponse(_window, outputFortmat, ColorUserEvent)

        'Welcome message proof of concept
        '_ircConnection.SendMessage(String.Format("PRIVMSG " & ConnectionInformation.Channel & " :" & "Welcome, {0}", Split(address)))

    End Sub

    ''' <summary>
    ''' Outputs a GUI message on user Quitting.
    ''' </summary>
    ''' <param name="address">The source of the user's local host.</param>
    ''' <remarks>
    ''' Displays an output message to the normalview window with correct format on user Quitting with Quit message.
    ''' </remarks>
    Private Sub Quit(ByVal address As String)
        Dim outputFortmat As String = String.Format("{0} has left the conversation.", Split(address))
        OutputResponse(_window, outputFortmat, ColorUserEvent)
    End Sub

    Private Sub Disconnected()
        Dim outputFortmat As String = "Disconnected!"
        OutputResponse(_window, outputFortmat, Color.Red)
    End Sub

    Private Sub TopicOnjoin(ByVal subject As String)
        OutputResponse(_window, String.Format("The chat's topic is: {0} ", subject), Color.Black)
        NewLine()
    End Sub
#End Region

#Region " Output Response "
    ''' <summary>
    ''' Displays the servers output response message.
    ''' </summary>
    ''' <param name="control">The control name.</param>
    ''' <param name="output">The server output.</param>
    ''' <param name="color">The control output line color</param>
    ''' <remarks>
    ''' Responsible for displaying all server and user response messages.
    ''' </remarks>
    Public Sub OutputResponse(ByVal control As RichTextBox, ByVal output As String, ByVal color As Color)
        Dim outputFormat As String = String.Format("{0}", output)

        If control.InvokeRequired Then
            control.Invoke(New OutputEventHandler(AddressOf OutputResponse), control, output, color)
        Else
            Dim start = control.TextLength
            Dim length = outputFormat.Length

            With control
                .AppendText(outputFormat & Environment.NewLine)
                .ScrollToCaret()
                .Select(start, length)
                .SelectionColor = color
            End With
        End If
    End Sub

    Private Sub NewLine()
        If _window.InvokeRequired Then
            _window.Invoke(New MethodInvoker(AddressOf NewLine))
        Else
            _window.AppendText(Environment.NewLine)
        End If
    End Sub

#End Region

#Region " Functions "
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="s"></param>
    ''' <param name="instance"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function indexOf(ByVal s As String, ByVal instance As Integer) As Integer
        Dim startAt As Integer = -1
        For x As Integer = 1 To instance
            startAt = s.IndexOf(" "c, startAt + 1)
        Next
        Return startAt
    End Function

    Private Function Split(ByVal name As String) As String
        Return name.Split("!"c)(0)
    End Function
#End Region

#Region " IDisposable "
    Public Sub dispose() Implements IDisposable.Dispose

    End Sub
#End Region

End Class

我遇到问题的部分是Private Private下的PrivMsg。

3 个答案:

答案 0 :(得分:1)

你可以在表单上创建一个方法,接收一个字符串,从传递的文本中创建一个新的LV项目:

Public Sub AddNewLVItem(txtName As String)
    Dim LVI as New ListViewItem
    LVI.Text = txtName

    LVI.Group = xxx     ' whatever other props there are

    lstaccounts.items.Add(LVI)
End Sub

或简写,如果没有子项,组等

  lstaccounts.items.Add(New ListViewItem(txtName))

编辑:

通常,表单名称仅在MDI子表单上是空白的,我不确定为什么会出现这种情况,但显然是这样。表单是类,应该实例化以供使用。他们可以使用旧的默认实例方法(FormName.Show),但主要是为了兼容回到VB4 / 5/6天,当表单与类不同时。旧的默认实例也使得那些对OOP没有任何线索的修补匠(非程序员)更容易获得轻松的东西。

首先,您需要使用应用级frmGiveAways引用scope,以便其他类都引用同一个对象。将模块添加到项目中(如果还没有)并添加:

Friend frmGive As frmGiveAways

(或者将表单名称更改为FormGiveAways,以便实例名称和引用它的所有代码仍然可以使用frmGiveAways。)

现在,当您在菜单中显示表单时:

If frmGive Is Nothing Then        ' check to see if it already exists
    frmGive = New frmGiveAways
    frmGive.Show                  ' could be a separate 'If' if needed
End if

现在使用引用frmGiveAways一个 frmGive实例,只要每个人都使用该引用,事情就可以了。在'Sub AddEntry'中,设置一个中断并监视Me.Name值一段时间以确保所有调用代码都被正确重构是值得的。

HTH

答案 1 :(得分:1)

不要对listview进行硬编码,而是尝试将其作为参数传递给sub。

这样的事情:

Private Sub Privmsg(ByVal address As String, ByVal message As String, ByRef LV as ListView)
    Dim outputFormat As String = String.Format("<{0}> {1}", Split(address), message)
    OutputResponse(_window, outputFormat, Color.Black)
    Select Case message
        Case "" : _ircConnection.SendMessage(String.Format("PRIVMSG " & ConnectionInformation.Channel & " :" & "{0}", Split(address)))
        Case frmGiveaways.keyword
            _ircConnection.SendMessage(String.Format("PRIVMSG " & ConnectionInformation.Channel & " :" & "recieved keyword", Split(address)))
            LV.Items.Add(Split(address))
    End Select

End Sub

答案 2 :(得分:0)

将项目添加到ListView的基本语法是:

' Create list view items
Dim item1 As New ListViewItem("Item #1", 0)
Dim item2 As New ListViewItem("Item #2", 1)
Dim item3 As New ListViewItem("Item #3", 2)

然后,您可以将列表视图项单独或作为一组添加到列表视图中,如下所示:

逐项添加项目:

ListView1.Items.Add(item1)
ListView1.Items.Add(item2)
ListView1.Items.Add(item3)

一次添加多个项目:

ListView1.Items.AddRange(New ListViewItem() {item1, item2, item3})

注意:ListView1.Items.Add()ListView1.Items.AddRange()方法都需要ListViewItem个对象,而不是字符串值。