Valhalla Legends Forums Archive | Visual Basic Programming | [VB6] Listview functions for you.

AuthorMessageTime
dRAgoN
Note: These functions are for a duel column listview in Visual Basic 6, useing the ReportView.
[code]Public Function AddUser(lvListView As ListView, StrUsername As String, StrStats As String, LngFlags As Long, LngPing As Long)
   With lvListView
       .ListItems.Add , , StrUsername, , GetIconCode(StrStats, LngFlags)
       .ListItems(.ListItems.Count).ListSubItems.Add , , , GetPingCode(LngPing, LngFlags)
   End With
End Function

Public Function RemoveUser(lvListView As ListView, StrUsername As String)
Dim usrIndex As Integer
   With lvListView
       usrIndex = .FindItem(StrUsername).Index
       .ListItems.Remove usrIndex
   End With
End Function

Public Function ModifyUser(lvListView As ListView, StrUsername As String, StrStats As String, LngFlags As Long, LngPing As Long)
Dim usrIndex As Integer
   If StrStats = "" Then StrStats = "TAHC"
   With lvListView
       usrIndex = .FindItem(StrUsername).Index
       .ListItems.Remove usrIndex
       .ListItems.Add usrIndex, , StrUsername, , GetIconCode(StrStats, LngFlags)
       .ListItems(usrIndex).ListSubItems.Add , , , GetPingCode(LngPing, LngFlags)
   End With
End Function

Public Function ClearItems(lvListView As ListView)
   lvListView.ListItems.Clear
End Function

Public Function ItemCount(lvListView As ListView) As Integer
   ItemCount = lvListView.ListItems.Count
End Function[/code]
I'm sure you can make or find your own geticon/getping code, i would post mine but 75% of you would probably get lost in it.

"Oh joy!" for some of you.
July 17, 2004, 10:51 AM
ChR0NiC
[quote author=dRAgoN link=board=31;threadid=7743;start=0#msg70932 date=1090061479]\I'm sure you can make or find your own geticon/getping code, i would post mine but 75% of you would probably get lost in it.

"Oh joy!" for some of you.
[/quote]

Uh, they aren't hard to understand, especially if you comment them well.

For example:
[code]
Private Function GetPingCode(Flags As Long)
If Flags And &H16 Then GetPingCode = ICON_LAGPLUG: Exit Sub
Select Case
Case 5 To 199 '// Ping of 5 to 300 :o
GetPingCode = ICON_LAG1
Case 200 To 300 '//Ping Of 200 to 300
GetPingCode = ICON_LAG2
Case 301 To 400 '//Ping Of 301 to 400
GetPingCode = ICON_LAG3
Case 401 To 600 '//Ping Of 401 to 600
GetPingCode = ICON_LAG4
Case 601 To 1200 '//Ping Of 601 to 1200
GetPingCode = ICON_LAG5
Case Is > 1200 '//Ping Higher than 1201
GetPingCode = ICON_LAG6
End Select
End Function
[/code]

If yours is any more complicated than that, I think you should consider rewriting it.
July 20, 2004, 8:22 AM
UserLoser.
[quote author=ChR0NiC link=board=31;threadid=7743;start=0#msg71465 date=1090311746]
Uh, they aren't hard to understand, especially if you comment them well.

For example:
[code]
Private Function GetPingCode(Flags As Long)
If Flags And &H16 Then GetPingCode = ICON_LAGPLUG: Exit Sub
Select Case
Case 5 To 199 '// Ping of 5 to 300 :o
GetPingCode = ICON_LAG1
Case 200 To 300 '//Ping Of 200 to 300
GetPingCode = ICON_LAG2
Case 301 To 400 '//Ping Of 301 to 400
GetPingCode = ICON_LAG3
Case 401 To 600 '//Ping Of 401 to 600
GetPingCode = ICON_LAG4
Case 601 To 1200 '//Ping Of 601 to 1200
GetPingCode = ICON_LAG5
Case Is > 1200 '//Ping Higher than 1201
GetPingCode = ICON_LAG6
End Select
End Function
[/code]

If yours is any more complicated than that, I think you should consider rewriting it.
[/quote]

Can't Exit Sub in a function. Also, > operator is greater than, not greater than or equal to.
July 20, 2004, 1:12 PM
Grok
Another way, not better, but leaves no holes.

[code]Private Function GetPingCode(Flags As Long)
If Flags And &H16 Then GetPingCode = ICON_LAGPLUG: Exit Sub
Select Case pingval
Case Is < 5:
GetPingCode = ICON_LAG0
Case Is < 201 '// Ping of 5 to 200
GetPingCode = ICON_LAG1
Case Is < 301 '//Ping Of 200 to 300
GetPingCode = ICON_LAG2
Case Is < 401 '//Ping Of 301 to 400
GetPingCode = ICON_LAG3
Case Is < 601 '//Ping Of 401 to 600
GetPingCode = ICON_LAG4
Case Is < 1201 '//Ping Of 601 to 1200
GetPingCode = ICON_LAG5
Case Else '//Ping Higher than 1201
GetPingCode = ICON_LAG6
End Select
End Function[/code]

Your Select Case did not have a variable argument, so I made one up.
July 20, 2004, 9:15 PM
Spht
Anyway, you're using incorrect range (assuming you're trying to emulate Blizzard client). You can use your own ranges, but it might confuse users who are used to the others.

[quote author=Spht link=board=17;threadid=7357;start=0#msg66402 date=1087771995]
(ping being an unsigned long)

[code] Ping Is < 10
' No latency bars
Ping Is < 200
' ONE latency bar
Ping Is < 300
' TWO latency bars
Ping Is < 400
' THREE latency bars
Ping Is < 500
' FOUR latency bars
Ping Is < 600
' FIVE latency bars
Ping Is >= 600
' SIX latency bars[/code]
[/quote]
July 20, 2004, 10:31 PM
Quarantine
Yes I use Sphts way now since its cleaner than mine. Ty Spht.
July 21, 2004, 4:50 PM
Grok
[quote author=Spht link=board=31;threadid=7743;start=0#msg71557 date=1090362691]

[code] Ping Is < 10
' No latency bars
Ping Is < 200
' ONE latency bar
Ping Is < 300
' TWO latency bars
Ping Is < 400
' THREE latency bars
Ping Is < 500
' FOUR latency bars
Ping Is < 600
' FIVE latency bars
Ping Is >= 600
' SIX latency bars[/code]
[/quote]

I recommend getting in the habit of using a Case Else, even if you believe you have covered all the possibilities and ranges. It is similar to always providing a default case in C. You never know when you will go back and modify code, and inadvertently stop handling a case. Plus, it is a good habit since more complex cases can leave non-obvious logic holes, which a Case Else would handle, making debugging simpler.
July 21, 2004, 6:50 PM
Quarantine
[code]Public Function GetPing(ByVal Ping As Long) As Integer
Select Case Ping
Case Is < 10
' No latency bars
Case Is < 200
GetPing = PING_1G
Case Is < 300
GetPing = PING_2G
Case Is < 400
GetPing = PING_3Y
Case Is < 500
GetPing = PING_4Y
Case Is < 600
GetPing = PING_5R
Case Is >= 600
GetPing = PING_6R
Case Is > 1201
GetPing = PING_6R
Case Is < 0
GetPing = PING_6R
Case Else
GetPing = PING_PLUG
End Select
End Function[/code]


Thats my Ping Code I wrote just now.


Edit: Silly me forgot [ code ] tags
July 21, 2004, 7:22 PM
Spht
Note that what I pasted was merely pseudocode to explain the correct ping range for matching icons which Blizzard's legacy clients use.

[quote author=WaR[KL] link=board=31;threadid=7743;start=0#msg71701 date=1090437743][code] Case Is > 1201
GetPing = PING_6R[/code][/quote]

The Is >= 600 check covers your Is > 1201 check.

[quote author=WaR[KL] link=board=31;threadid=7743;start=0#msg71701 date=1090437743][code] Case Is < 0
GetPing = PING_6R[/code][/quote]

The Is < 10 check covers your Is < 0 check.

[quote author=WaR[KL] link=board=31;threadid=7743;start=0#msg71701 date=1090437743][code] Case Else
GetPing = PING_PLUG[/code][/quote]

Not sure what you're doing here. You covered all possible cases. A user's UDP state is in their flags, not ping.
July 21, 2004, 7:49 PM
Grok
[quote author=Spht link=board=31;threadid=7743;start=0#msg71708 date=1090439395]
Note that what I pasted was merely pseudocode to explain the correct ping range for matching icons which Blizzard's legacy clients use.

[code] Case Else
GetPing = PING_PLUG[/code][/quote]

Not sure what you're doing here. You covered all possible cases. A user's UDP state is in their flags, not ping.
[quote][/quote]

Right - that's exactly my point. You are looking at your code and going "ok great, covered all possible cases", and then not putting in a Case Else. I promise you, that is a dangerous habit to get into. Even when all cases are covered, you should have a Case Else. Someone, you or someone else, will modify your code someday and the changes will leave a hole open, on some Select or some switch, and this failure to have good coding habits will bite you in the ass. Promise!
July 21, 2004, 9:08 PM
Adron
And the Case Else should always contain a Stop statement. Don't try to handle it; you can't handle a coding error. For this reason, you should never have a valid case end up in a Case Else handler, unless this is really "what should happen for anything unhandled".
July 22, 2004, 7:43 PM
ChR0NiC
[quote author=UserLoser. link=board=31;threadid=7743;start=0#msg71483 date=1090329121]
Can't Exit Sub in a function. Also, > operator is greater than, not greater than or equal to.
[/quote]

Sorry Exit Sub just due to force of habit, and VB usually just corrects it for me, kind of made me lazy :P Also I used just the > because I didn't want 1200, only 1201 and higher.
July 26, 2004, 7:46 AM
Grok
[quote author=Adron link=board=31;threadid=7743;start=0#msg71907 date=1090525383]
And the Case Else should always contain a Stop statement. Don't try to handle it; you can't handle a coding error. For this reason, you should never have a valid case end up in a Case Else handler, unless this is really "what should happen for anything unhandled".
[/quote]

You're right of course, and I should do this more like you described.

[code]
Case Is >= 600
GetPing = PING_6R
Case Else
Debug.Print "You missed a case in your Select cases..."
Stop
End Select
[/code]
July 26, 2004, 4:59 PM
Quarantine
I put the Plug there because I had nothing more to put there :P . I changed it with all of your suggestions. Thanks.
July 26, 2004, 5:54 PM

Search