[Help]Listview[Solved]

Posts 1–15 of 20 · Page 1 of 2
[Help]Listview[Solved]
Ok, well I never worked with listviews at all.

This is how my listview looks like:



I have multiple questions.

1. How to delete the SELECTED row? - Answered

2. How to save listviews content? - Answered

3. How to load content? - Answered

4. How to avoid double entrys by checking the ips - Subitem(2)

5. How to sort columns? - Answered

like...

if listview1.contains...


I know that some of these questions are newbie, but I'd like to know them :P

Thanks in advance ^_^
1.

[php]
Private Sub listView1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
If e.KeyCode = Keys.Delete Then
listView1.Items.Remove(listView1.FocusedItem)
End If
End Sub
[/php]

2.

[php]
Dim Fb As New System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter()

Dim FS As New System.IO.FileStream("application.startuppath & "listview.txt", IO.FileMode.Create)
Fb.Serialize(FS, New ArrayList(ListView1.Items))
FS.Close()
[/php]

3.
[php]
Dim Fb2 As New System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter()
Dim FS2 As New System.IO.FileStream(Application.StartupPath & "listview.txt", IO.FileMode.Open)
ListView1.Items.AddRange(Fb2.Deserialize(FS2).ToAr ray(GetType(ListViewItem)))
FS2.Close()
[/php]


4.
[php]
Dim item As ListViewItem = ListView1.FindItemWithText(Textbox1.Text)
If Not ListView1.Items.Contains(item) Then
' doesn't exist, add it
End If

[/php]

Hope this helps
4. -> *********enceException

Null = 0 = Zero

Code:
Dim item As ListViewItem = ListView1.FindItemWithText(enterip.Text)

        If Not ListView1.Items.Contains(item) Then

            Dim lv As ListViewItem = ListView1.Items.Add(iname.Text)
            'The remaining columns are subitems  
            lv.SubItems.Add(enterip.Text)
            lv.SubItems.Add(enterport.Text)
            lv.SubItems.Add(iplayers.Text)
            lv.SubItems.Add(imap.Text)

        End If
Didn't try saving/loading yet.

Removing the selected line is working fine <3
Quote Originally Posted by Blubb1337 View Post
4. -> *********enceException

Null = 0 = Zero

Code:
Dim item As ListViewItem = ListView1.FindItemWithText(enterip.Text)

        If Not ListView1.Items.Contains(item) Then

            Dim lv As ListViewItem = ListView1.Items.Add(iname.Text)
            'The remaining columns are subitems  
            lv.SubItems.Add(enterip.Text)
            lv.SubItems.Add(enterport.Text)
            lv.SubItems.Add(iplayers.Text)
            lv.SubItems.Add(imap.Text)

        End If
Didn't try saving/loading yet.

Removing the selected line is working fine <3
@4: There must be at least 1 row of items in the listview to avoid Null Reference Exception.

Just enclose the code in the following IF block:
[php]If Listview.items.count>0 Then

End If
[/php]

So, your final code will be:
[php]If Listview.items.count>0 Then

Dim item As ListViewItem = ListView1.FindItemWithText(enterip.Text)

If Not ListView1.Items.Contains(item) Then

Dim lv As ListViewItem = ListView1.Items.Add(iname.Text)
'The remaining columns are subitems
lv.SubItems.Add(enterip.Text)
lv.SubItems.Add(enterport.Text)
lv.SubItems.Add(iplayers.Text)
lv.SubItems.Add(imap.Text)

End If
Else

Dim lv As ListViewItem = ListView1.Items.Add(iname.Text)
'The remaining columns are subitems
lv.SubItems.Add(enterip.Text)
lv.SubItems.Add(enterport.Text)
lv.SubItems.Add(iplayers.Text)
lv.SubItems.Add(imap.Text)
End If
[/php]

This will remove you Null Reference Exception
ahh logical...sorry for being nub :/

tyvm mj

//still getting the error =O

//saving & loading is working fine <3333

Another question -.-

If I hit start - Run and enter "C:\Programme\Activision\Call of Duty 4 - Modern Warfare\iw3mp.exe" it is working fine

Shell("C:\Programme\Activision\Call of Duty 4 - Modern Warfare\iw3mp.exe") isn't working >_<

Gives me weird game errors.
Try Process.start instead

[php]
System.Diagnostics.Process.Start("C:\Programme\Act ivision\Call of Duty 4 - Modern Warfare\iw3mp.exe")[/php]

Shell uses command prompt, it should be avoided anyway


Quote Originally Posted by NextGen1 View Post
Try Process.start instead

[php]
System.Diagnostics.Process.Start("C:\Programme\Act ivision\Call of Duty 4 - Modern Warfare\iw3mp.exe")[/php]

Shell uses command prompt, it should be avoided anyway


Same error, neither working.

Code:
WIN_IMPROPER_QUIT_BODY
Thats a call of duty error.

Try moving your compiled application to the call of duty folder

and running

process.start again but instead use

[php]
application.startuppath & iw3mp.exe
[/php]

Just as a test
Yep that's actually working. Why does Start - Run work? =/

Mind helping me with the null reference error?
Sure let me read back, and I think the reason it isn't working outside is because it automatically looks for prerequisites (dll's etc) that our outside of your application path. weird ,but plausible, glad it's working

Im on a phone call do a software demo for a client, but I will read back and see what's going on


What line is populating the error?

I am asking because the reason you would get a null error is if fields are blank or it is trying to load a blank txt file etc etc.

Null error 0
could be data or value.


If Not ListView1.Items.Contains(item) Then

This line is causing the error.
Still not working, One sec.

Quick question, are you using a textbox to add items, or how are they being added?
Yes, those are textboxes.

// Do you know how to sort by clicking on the different columns?
Then try adding it like this

Create a sub that you will call when you need it

[php]
'Creates a sub called nditemadd which means No Duplicates Item Add
Private Sub ndItemadd(ByVal ndItemText As String)
If ListView1.FindItemWithText(ndItemText) Is Nothing Then
ListView1.Items.Add(ndItemText)
End If
End Sub
[/php]

This code says, look for an existing item = to the text of textbox1.text , if it isn't there, add it. You can add a else which gives an error

like

[php]
'Creates a sub called nditemadd which means No Duplicates Item Add
Private Sub ndItemadd(ByVal ndItemText As String)
If ListView1.FindItemWithText(ndItemText) Is Nothing Then
ListView1.Items.Add(ndItemText)
Else
MsgBox ("Item Already exists")
' or the very popular database error
'Msgbox ("Duplicates Items Not Allowed")
End If
End Sub
[/php]

Then whenever you want to add an item, let's say a button click event

do

[php]
nditemadd (Item to add)
[/php]

So you can do

[php]
nditemadd(textbox1.text)
[/php]

or individual

[php]
nditemadd("Hello World")
[/php]

Sorting ListView

Quote Originally Posted by Fryan
ne of my favorite .NET control is the ListView. It is very useful for displaying multiple records on a spreadsheet format. Column sorting is one the feature of spreadsheet applications that most of end-users are used to. Unfortunately, this is not readily available as property in VB.NET. The ListView Sorting property only sorts items and not the sub-items so it can't be use if we want to allow the user to sort your list by any clicked column.


To make your ListView application capable of column sorting, follow these steps:

1. On your existing project, add a new class with following code:

view plainprint?
Public Class clsListviewSorter ' Implements a comparer
Implements IComparer
Private m_ColumnNumber As Integer
Private m_SortOrder As SortOrder
Public Sub New(ByVal column_number As Integer, ByVal sort_order As SortOrder)
m_ColumnNumber = column_number
m_SortOrder = sort_order
End Sub
' Compare the items in the appropriate column
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim item_x As ListViewItem = DirectCast(x, ListViewItem)
Dim item_y As ListViewItem = DirectCast(y, ListViewItem)
' Get the sub-item values.
Dim string_x As String
If item_x.SubItems.Count <= m_ColumnNumber Then
string_x = ""
Else
string_x = item_x.SubItems(m_ColumnNumber).Text
End If
Dim string_y As String
If item_y.SubItems.Count <= m_ColumnNumber Then
string_y = ""
Else
string_y = item_y.SubItems(m_ColumnNumber).Text
End If
' Compare them.
If m_SortOrder = SortOrder.Ascending Then
If IsNumeric(string_x) And IsNumeric(string_y) Then
Return Val(string_x).CompareTo(Val(string_y))
ElseIf IsDate(string_x) And IsDate(string_y) Then
Return DateTime.Parse(string_x).CompareTo(DateTime.Parse( string_y))
Else
Return String.Compare(string_x, string_y)
End If
Else
If IsNumeric(string_x) And IsNumeric(string_y) Then
Return Val(string_y).CompareTo(Val(string_x))
ElseIf IsDate(string_x) And IsDate(string_y) Then
Return DateTime.Parse(string_y).CompareTo(DateTime.Parse( string_x))
Else
Return String.Compare(string_y, string_x)
End If
End If
End Function
End Class

2. Declare a private variable on the form where the listview you want to be sorted is located.
view plainprint?
Private m_SortingColumn As ColumnHeader


3. Then on the listview's ColumnClick event, add the following code
view plainprint?
Private Sub ListView1_ColumnClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
' Get the new sorting column.
Dim new_sorting_column As ColumnHeader = ListView1.Columns(e.Column)
' Figure out the new sorting order.
Dim sort_order As System.Windows.Forms.SortOrder
If m_SortingColumn Is Nothing Then
' New column. Sort ascending.
sort_order = SortOrder.Ascending
Else ' See if this is the same column.
If new_sorting_column.Equals(m_SortingColumn) Then
' Same column. Switch the sort order.
If m_SortingColumn.Text.StartsWith("> ") Then
sort_order = SortOrder.Descending
Else
sort_order = SortOrder.Ascending
End If
Else
' New column. Sort ascending.
sort_order = SortOrder.Ascending
End If
' Remove the old sort indicator.
m_SortingColumn.Text = m_SortingColumn.Text.Substring(2)
End If
' Display the new sort order.
m_SortingColumn = new_sorting_column
If sort_order = SortOrder.Ascending Then
m_SortingColumn.Text = "> " & m_SortingColumn.Text
Else
m_SortingColumn.Text = "< " & m_SortingColumn.Text
End If
' Create a comparer.
ListView1.ListViewItemSorter = New clsListviewSorter(e.Column, sort_order)
' Sort.
ListView1.Sort()
End Sub
Code is extensive, so I figure pointing you to a tut would help.



Aight I do get your code.

Sorting is working fine.

But what about the sub items?
Posts 1–15 of 20 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us