-
Aug 16th, 2017, 12:14 AM
#1
Thread Starter
Junior Member
ListBox - finding specific data and index
Hello everyone, what i'm doing now, it's read info from CANBUS and put it in the listbox in vb6. It looks like
Code:
tmp = idtag.Text & vbTab & dlc.Text & vbTab & RxData.Text
SO what i have in listbox after a get it its
so as you can see i have two id 140, so it means listbox just add new line every time. What i want it's make it read ID then if its equal to id that we receive from CANBUS, we put data exactly to that ID. Like "if ID from CANBUS = ID in LISTBOX then update data from CANBUS in that ID, if received id doesnt excist in listbox, then add new item". As you can see from a picture i recevie data from canbus in one like, ID+DLC+DATA like this "0000014008BBBBBBBBBBBBBBBB" then i extract right values from with MID() then add to listbox. My code is:
Code:
Select Case MSComm1.CommEvent
Case comEvReceive
Buffer = MSComm1.Input
Text1.Text = Text1.Text & Buffer
If Len(Text1.Text) = 26 Then
Text4 = Text1
idtag.Text = Mid(Text1.Text, 6, 3)
dlc.Text = Mid(Text1.Text, 10, 1)
RxData.Text = Mid(Text1.Text, 11, 16)
'Call Command1_Click
RxData.Text = Right(Text1.Text, Val(dlc.Text * 2))
RxData = SpaceIt(RxData)
tmp = idtag.Text & vbTab & dlc.Text & vbTab & RxData.Text
If List1.ListCount > 14 Then List1.RemoveItem (a)
List1.AddItem tmp, a
Text1.Text = ""
a = a + 1
If a > 14 Then
'List1.Clear
a = 0
End If
End If
End Select
End Sub
Any ideas? aprecciate any input. Thanks
Last edited by JeezyWonder; Aug 16th, 2017 at 12:23 AM.
-
Aug 16th, 2017, 02:05 AM
#2
Re: ListBox - finding specific data and index
Jeezy
I've reposted your code with slightly different indents to I can follow it better
Code:
Select Case MSComm1.CommEvent
Case comEvReceive
Buffer = MSComm1.Input
Text1.Text = Text1.Text & Buffer
If Len(Text1.Text) = 26 Then
Text4 = Text1
idtag.Text = Mid(Text1.Text, 6, 3)
dlc.Text = Mid(Text1.Text, 10, 1)
RxData.Text = Mid(Text1.Text, 11, 16)
'Call Command1_Click
RxData.Text = Right(Text1.Text, Val(dlc.Text * 2))
RxData = SpaceIt(RxData)
tmp = idtag.Text & vbTab & dlc.Text & vbTab & RxData.Text
If List1.ListCount > 14 Then
List1.RemoveItem (a)
End If
List1.AddItem tmp, a
Text1.Text = ""
a = a + 1
If a > 14 Then
'List1.Clear
a = 0
End If
End If
End Select
End Sub
Some initial questions:
- You've got alot of controls shown, so just to be sure, is List1 the small control, second from the right, at the bottom, containing the entry 140?
- If so, how can you see 14 entries?
- If not, then which control is List1?
Spoo
-
Aug 16th, 2017, 02:19 AM
#3
Thread Starter
Junior Member
Re: ListBox - finding specific data and index
As i told before, i got a data from canbus MCU as ID(4bytes)+DLC(1Byte)+DATA(8Bytes)in HEX, so actually i get this as fresh data in mscomm32 "0000014008BBBBBBBBBBBBBBBB" after that i just exctract values on idtag, dlc and data Textboxes with Mid() cmd then add it with
tmp = idtag.Text & vbTab & dlc.Text & vbTab & RxData.Text
to listbox
.
i didnt understood second and third questions.
Code:
If List1.ListCount > 14 Then
this is was for a limit listbox to 14 indexes only. If its still hard to understand i can upload a youtube video of working principe, if it helps
Last edited by JeezyWonder; Aug 16th, 2017 at 02:24 AM.
-
Aug 16th, 2017, 02:25 AM
#4
Re: ListBox - finding specific data and index
Jeezy
I think now that I was wrong in my prior assumption.
List1 is the "big" control at the upper left, under the captions "ID .. DLC .. DATA", correct?
Spoo
-
Aug 16th, 2017, 02:28 AM
#5
Thread Starter
Junior Member
Re: ListBox - finding specific data and index
Yes you are right , its list1(listbox), its where ID DLC DATA labels located above it. All others are textboxes.
-
Aug 16th, 2017, 02:34 AM
#6
Thread Starter
Junior Member
Re: ListBox - finding specific data and index
Originally Posted by Spooman
Jeezy
I think now that I was wrong in my prior assumption.
List1 is the "big" control at the upper left, under the captions "ID .. DLC .. DATA", correct?
Spoo
Actually what i was thinking its extract data from listbox1 with listboxindex.value, after it filled first time to textbox from RX. Then check it with "idtag textbox" if its equal we put updated data from "data textbox" something like this:
Code:
Private Sub Command3_Click()
Dim tmp As String
Dim Item() As String
Dim x As Integer
Dim IndexCheck As String
For x = 0 To List1.ListCount - 1
IndexCheck = x
Text5.Text = Text5.Text + IndexCheck + Mid(List1.List(x), 1, 3) + vbCrLf
Next
End Sub
Private Sub Command4_Click()
Dim tmp As String
a = Mid(Text5.Text, 1, 1)
If idtag.Text = Mid(Text5.Text, 2, 3) Then
List1.RemoveItem (a)
tmp = idtag.Text & vbTab & dlc.Text & vbTab & RxData.Text
List1.AddItem tmp, Mid(Text5.Text, 1, 1)
End If
Text5.Text = ""
End Sub
But again, i dont know how to make it not on the buttons but on the mscomm after data is filled first time, and also make it check every line.... so code seems wrong, just my thoughts....
-
Aug 16th, 2017, 02:34 AM
#7
Re: ListBox - finding specific data and index
Jeezy
OK, that's a start
However, I'm still confused about what you are trying to accomplish.
Could you given a couple of example situations?
EDIT-1:
You snuck posts #6 in while I was composing, so thanks for the info.
I'll think on that for a bit, after I get some sleep
Spoo
-
Aug 16th, 2017, 02:41 AM
#8
Thread Starter
Junior Member
Re: ListBox - finding specific data and index
Thanks for your attention to the problem.
Want to start with some words about canbus, canbus has a periodic data send, it can send same idtag with data none-stop, until power off. So to make soft easier to debug this data i want to listbox hold value in the same id. Sample: "We got 14 frames from CANBUS add them into listbox, then canbus continue sending the frames to us, so what will listbox do? it will just add new lines with same id and data - its wrong. so what i want to make vb6 code check the id and if id exists in listbox and its equal to canbus data, we just updata a value(or remove a line and add new with same id to that place). To better understand see this: https://www.youtube.com/watch?v=jUuncLav0v8
as you can see in the video, listbox doesnt add new lines it just updating the data
-
Aug 16th, 2017, 02:44 AM
#9
Thread Starter
Junior Member
Re: ListBox - finding specific data and index
Thanks, have a good dreams
-
Aug 16th, 2017, 05:52 AM
#10
Re: ListBox - finding specific data and index
a simulation with a timer
Code:
Private Sub Form_Load()
Timer1.Interval = 200
End Sub
Private Sub Timer1_Timer()
Dim AddItem As Boolean
Dim TheItem As String
TheItem = Format(Int(Rnd * 14), "00") & "--" & Now
AddItem = True
For i = 0 To List1.ListCount
If Left(List1.List(i), 2) = Left(TheItem, 2) Then
AddItem = False
Exit For
End If
Next
If AddItem Then
List1.AddItem TheItem
Else
List1.List(i) = TheItem
End If
End Sub
do not put off till tomorrow what you can put off forever
-
Aug 16th, 2017, 07:38 AM
#11
Re: ListBox - finding specific data and index
I think if it were me I would make use of the item data portion of the list box and place my id there.
Would be quicker and easier to loop through the list and check the item data value rather than parsing the string for the ID
This would give you the index of the item if it exists and allow you to update the text of the item in the list as needed.
-
Aug 16th, 2017, 11:00 AM
#12
Re: ListBox - finding specific data and index
Originally Posted by DataMiser
I think if it were me I would make use of the item data portion of the list box and place my id there.
Would be quicker and easier to loop through the list and check the item data value rather than parsing the string for the ID
This would give you the index of the item if it exists and allow you to update the text of the item in the list as needed.
True, (provided that 'idtag.Text = Mid(Text1.Text, 6, 3)' can always be converted to an integer)
do not put off till tomorrow what you can put off forever
-
Aug 17th, 2017, 01:20 AM
#13
Thread Starter
Junior Member
Re: ListBox - finding specific data and index
Originally Posted by DataMiser
I think if it were me I would make use of the item data portion of the list box and place my id there.
Would be quicker and easier to loop through the list and check the item data value rather than parsing the string for the ID
This would give you the index of the item if it exists and allow you to update the text of the item in the list as needed.
Thanks for your attention, btw can you provide simple exmaple for better understanding?
-
Aug 17th, 2017, 04:14 AM
#14
Thread Starter
Junior Member
Re: ListBox - finding specific data and index
Originally Posted by IkkeEnGij
a simulation with a timer
Code:
Private Sub Form_Load()
Timer1.Interval = 200
End Sub
Private Sub Timer1_Timer()
Dim AddItem As Boolean
Dim TheItem As String
TheItem = Format(Int(Rnd * 14), "00") & "--" & Now
AddItem = True
For i = 0 To List1.ListCount
If Left(List1.List(i), 2) = Left(TheItem, 2) Then
AddItem = False
Exit For
End If
Next
If AddItem Then
List1.AddItem TheItem
Else
List1.List(i) = TheItem
End If
End Sub
Thanks Ikke. I adopted your timer code to mine.
Code:
Private Sub Timer1_Timer()
Dim i As Integer
Dim AddItem As Boolean
Dim TheItem As String
TheItem = idtag.Text & vbTab & dlc.Text & vbTab & RxData.Text
AddItem = True
For i = 0 To List1.ListCount
If Left(List1.List(i), 3) = Left(TheItem, 3) Then
AddItem = False
Exit For
End If
Next
If AddItem Then
List1.AddItem TheItem
Else
List1.List(i) = TheItem
End If
End Sub
But here is 2 problems.
1) Listbox is blinking a lot. It's Bad.
2) It's skipping index 0 in lisbox . And starting to add items from index 1.
By the way, mine timer interval is:
Code:
Timer1.Interval = 10
Because i need it to check for updates every 10ms
-
Aug 17th, 2017, 06:51 AM
#15
Re: ListBox - finding specific data and index
using itemdata
Code:
Private Sub Timer1_Timer()
Dim AddItem As Boolean
Dim TheItem As String
Dim TheItemData As Long
TheItem = Format(Int(Rnd * 14), "00") & "--" & Now
TheItemData = CLng(Left(TheItem, 2))
AddItem = True
For i = 0 To List1.ListCount - 1
If List1.ItemData(i) = TheItemData Then
AddItem = False
Exit For
End If
Next
If AddItem Then
List1.AddItem TheItem
List1.ItemData(List1.NewIndex) = TheItemData
Else
List1.List(i) = TheItem
End If
End Sub
do not put off till tomorrow what you can put off forever
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|