Reputation: 141
I am having trouble adding an item to a listview from one of my classes. I am missing something that is obviously over my head. If someone could help me to understand what I am missing that would be great.
I don't know how much detail I need to provide. What I am trying to do is to basic so I know I have to be missing something. I have a class that I am trying to use to add an item to a listview I have on a form. There are no errors thrown, yet nothing gets added.
I have even tried using something as simple as frmGiveaways.lstAccounts.items.add("wtf") in the class, it doesn't throw any errors, the line is processed, yet no items appear in the list.
What are some things that would keep me from being able to do this?
Here is the class
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
The part I am having a problem with is PrivMsg under the Region Private.
Upvotes: 2
Views: 2931
Reputation: 38875
you could create a method on the form which receives a string to create a new LV item from the text passed:
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
or shorthand, if there are no subitems, groups etc
lstaccounts.items.Add(New ListViewItem(txtName))
EDIT:
TYPICALLY, the form name is blank on MDI child forms only, I am not sure why that is the case here, but it obviously is. Forms are classes and SHOULD be instanced for use. They can use the old default instance method (FormName.Show
) but that exists mainly for compatibility back to the VB4/5/6 days when forms were something apart from classes. The old default instance also makes it easier for tinkerers (non programmers) who dont have a clue about OOP, to get something woking easily.
First, you will need a reference to frmGiveAways
with app level scope
so that other classes all reference the same object. Add a module to your project (if there is not already one) and add:
Friend frmGive As frmGiveAways
(Or change the form name to something like FormGiveAways
so the instance name and all the code referencing it can still use frmGiveAways
).
Now, when you go to show the form in the menu:
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
Now there is one instance of frmGiveAways
using the reference frmGive
and as long as everyone uses that reference, things will work fine. In the 'Sub AddEntry' it would pay to set a break and monitor the Me.Name value for a while to make sure all the calling code is properly refactored.
HTH
Upvotes: 2
Reputation: 6948
Instead of hard coding the listview, try passing it into the sub as a parameter.
Something like this:
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
Upvotes: 1
Reputation: 34846
The basic syntax to add items to a ListView
is this:
' 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)
Then you can add the list view items to the list view, either individually or as a group, like this:
Adding item one-by-one:
ListView1.Items.Add(item1)
ListView1.Items.Add(item2)
ListView1.Items.Add(item3)
Adding several items at once:
ListView1.Items.AddRange(New ListViewItem() {item1, item2, item3})
Note: The ListView1.Items.Add()
and ListView1.Items.AddRange()
methods both need ListViewItem
objects, not string values.
Upvotes: 0