dcsimg
Results 1 to 17 of 17

Thread: [Resolved]Help creating For Each Loop - Multiple GPS Coordinates

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    67

    [Resolved]Help creating For Each Loop - Multiple GPS Coordinates

    I have a chat program in which each user that comes online automatically joins the chat. All of the online usernames are automatically put into a listbox. When a user leaves, their name is removed from the listbox.

    Some of the users may not have GPS enabled and the way it works is: If a user has GPS enabled, their chat program will send a special string to all chat participants every 5 seconds. Once the chat program receives that special string, it knows that it contains a username, latitude, and longitude number within that string. That string gets parsed and separates the latitude and longitude. The next step is to add a marker on a google map webbrowser control that I have.

    Where the for each loop comes in that I need help with:

    Only those users that are sending this special string need to be added to a separate list or variable. I want to take that list or variable that contains all of the users who are using GPS and set a new marker with their username and their coordinates. I already have the code for this to function but only for a single user.

    I have a timer that runs every 10 seconds that will be setting the markers on the google map. I need that for each loop to run under that timer but I don't know how to write the for each loop.

    In plain English this should happen:

    If the special chat string received = Then
    Add this user to a different listbox or variable and will need to store Username, Latitude, Longitude which will be included in that special parsed string.
    End If

    Keep in mind, that the chat program will be sending it's own GPS message to all participants over and over and over again as long as they are online. All of these users and their GPS Coordinates need to be stored in some type of listbox or variable so that when the map timer goes off every 10 seconds it gets the Usernames, Latitudes, and Longitudes of every user in that list. At that point we need the for each loop to kick in on that 10 second timer so it can set markers on the map for each user.

    THANK YOU
    Last edited by ouroboro; Jun 25th, 2014 at 08:16 AM. Reason: Resolved

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    I would do that step by step.
    At the beginning you recieve that special string,
    Take that string at parse it (i.e. get the username , lat and long) at store those
    Now check if you did already recieve such a string from that user
    If yes, update its stored position
    if no make a new entry ( to store those I would use the same technique as you used for storing the actual members on the chat)

    If you drop a member from the chat drop its entry in that positional storage.

    As for the for each loop, the exact syntax depends on the way you store the data. It could be an array, a list or......
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,248

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    If I'm understanding the problem correctly, what I would do is build a dictionary with usernames and coordinates, and then loop through the objects in the dictionary to do whatever I needed to do.

    Code:
        Public Class UserInfo
            Public UserName As String
            Public Latitude As Double
            Public Longitude As Double
            Public Sub New(ByVal User As String, ByVal NewLat As Double, ByVal NewLong As Double)
                Me.UserName = User
                Me.Latitude = NewLat
                Me.Longitude = NewLong
            End Sub
        End Class
    
        Private m_CoordLibrary As New Dictionary(Of String, UserInfo)
    
        Private Sub AddUser(ByVal UserName As String, ByVal Latitude As Double, ByVal Longitude As Double)
            If Not m_CoordLibrary.ContainsKey(UserName) Then
                m_CoordLibrary.Add(UserName, New UserInfo(UserName, Latitude, Longitude))
            End If
        End Sub
    
        Private Sub ProcessUsers()
            For Each User As UserInfo In Me.m_CoordLibrary.Values
                'do something with the user/gps coordinates.
            Next
        End Sub
    I always add to the reputation of those whose post are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    67

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    Excellent, thanks.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    67

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    Another issue I'm having in a different form that is using a ListBox.

    What I'm trying to accomplish and what is actually happening:

    Let's assume this is running on a Timer Event every 10 seconds.
    If ListBox2 has nothing in it then add the string located in TextBox1.Text.
    If ListBox2 has an item with certain text that is in TextBox1.Text (ListBox2.Items(i).Contains(MyUnitOutput)) then delete that item and add the new item again. (The new item will have similar text but not the exact same string. This is why I am using the Contains(MyUnitOutput)))

    What is happening when this code runs: The form loads with nothing in the ListBox which is good. The timer triggers 10 seconds later and then the String in TextBox1 is put into ListBox2.
    If I leave the string in Textbox1 to where it is identical, it deletes the item in ListBox2 and then adds it again. So far so good.

    When I add a different string to TextBox1, it adds it to the Listbox as well but then it starts adding it again and then it adds it 2 times, then 3 times, etc.

    Code:
    Dim MyUnitOutput As String = Me.TextBox1.Text
            If ListBox2.Items.Count() > 0 Then
                For i As Integer = 0 To ListBox2.Items.Count - 1
                    If ListBox2.Items(i).Contains(MyUnitOutput) Then
                        ListBox2.Items.RemoveAt(i)
                        ListBox2.Items.Add(MyUnitOutput)
    
                    Else
                        ListBox2.Items.Add(MyUnitOutput)
                    End If
    
                Next
    
            Else
                ListBox2.Items.Add(MyUnitOutput)
            End If
    Last edited by ouroboro; Jun 24th, 2014 at 04:02 PM.

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,335

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    You can write a chat program that "MONITORS" GPS LOCATION , but can't a for each loop?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    67

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    Quote Originally Posted by ident View Post
    You can write a chat program that "MONITORS" GPS LOCATION , but can't a for each loop?
    While I would love to blow smoke up my rear and say "Yes, I wrote that program." The fact of the matter is that I found code online for the chat program and have been modifying it. I'm still a novice.

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,292

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    Just an idea... Create a ChatUser class. Add the Location property (along with whatever other properties that you're interested in). When a user join in to chat, you create a new chat user object and add it to your listbox. When a user leave chat, you remove the corresponnding chatuser object from the listbox. Now it's just a matter of looping through the objects in your listbox to get the location of each user... Obviously, if the location is null then that user doesn't have GPS on.
    PS: for simplicity, you can use a datatable (which binds to your listbox) to store the chat users. Adding/removing chat users is just a matter of adding/removing corresponding rows in the datatable. Looping datatable is also easier than looping listbox items not strongly typed.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,335

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    Quote Originally Posted by ouroboro View Post

    Code:
    Dim MyUnitOutput As String = Me.TextBox1.Text
            If ListBox2.Items.Count() > 0 Then
                For i As Integer = 0 To ListBox2.Items.Count - 1
                    If ListBox2.Items(i).Contains(MyUnitOutput) Then
                        ListBox2.Items.RemoveAt(i)
                        ListBox2.Items.Add(MyUnitOutput)
    
                    Else
                        ListBox2.Items.Add(MyUnitOutput)
                    End If
    
                Next
    
            Else
                ListBox2.Items.Add(MyUnitOutput)
            End If
    If my listbox contains some value already remove it
    Then add it again
    If it doesn't have it add it any way
    If all else above fails lets just simply add it.

    *Shrug*

  10. #10
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,335

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    Quote Originally Posted by ouroboro View Post
    While I would love to blow smoke up my rear and say "Yes, I wrote that program." The fact of the matter is that I found code online for the chat program and have been modifying it. I'm still a novice.
    There is no shame in being a novice. Just you wont learn any thing messing with applications way advanced for you. In actual fact how do you know they are not written really bad?

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    67

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    Quote Originally Posted by ident View Post
    If my listbox contains some value already remove it
    Then add it again
    If it doesn't have it add it any way
    If all else above fails lets just simply add it.

    *Shrug*
    Fantastic reply, this will help tremendously. I am open to other suggestions.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    67

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    Quote Originally Posted by ident View Post
    There is no shame in being a novice. Just you wont learn any thing messing with applications way advanced for you. In actual fact how do you know they are not written really bad?
    This is the last stumbling block for me. Once I get past this, I will be all set and it will be working fine.

  13. #13
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,335

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    You add the item regardless of any out come.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    67

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    Quote Originally Posted by ident View Post
    You add the item regardless of any out come.
    That is not what is happening. Items are getting added 2,3,4,5,6,7 times each time the code runs and they are not being deleted. Create a form, add a ListBox2 control and a TextBox1 control. Create a button to run the code instead of a timer, so you can see how it behaves.

    All I am trying to accomplish:

    Form loads with nothing in the ListBox2.
    Run the code I mentioned via a timer
    If nothing is in TextBox1 then nothing will be in the Listbox2. Once something is in ListBox2, then it does not matter if TextBox1 is null or not.
    Once something (string) is in TextBox1 then put that into ListBox2.
    10 seconds later the code runs again via the timer.
    If the same exact string is in TextBox1 then check ListBox2 for that exact string and if it exists, then do nothing (or it can be deleted, it doesn't matter)
    If the string in TextBox1 has changed then add that string to ListBox2.
    If the string in TextBox1 Contains a certain string and that certain string matches any part of an item in the ListBox2 then delete the item in ListBox2 and add the item from TextBox1.

    For example:
    ListBox2 contains:
    UserA T 11:30
    UserB T 11:45
    UserC T 8:30

    Now 10 second timer triggers:
    If TextBox1.Text = "User A 11:33" Then
    Remove UserA T 11:30 from ListBox2 (That "certain string" would be ListBox2.Items(i).Contains("User A") Although I am using a username variable and not "User A")
    Add UserA T 11:33 to ListBox2
    Leave UserB T 11:45 and UserC T 8:30 inside the ListBox2

    ListBox2 now contains:
    UserA T 11:33
    UserB T 11:45
    UserC T 8:30


    Now 10 second timer triggers again:
    If TextBox1.Text = "User A 11:33" Or String.IsNullOrEmpty(TextBox2.Text)Then
    Nothing changes:

    ListBox2 contains:
    UserA T 11:33
    UserB T 11:45
    UserC T 8:30
    Last edited by ouroboro; Jun 24th, 2014 at 04:41 PM.

  15. #15
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,335

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    TextBox1.Text = "User A 11:33" is not contains now is it. Also note contains is case sensitive.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    67

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    Quote Originally Posted by ident View Post
    TextBox1.Text = "User A 11:33" is not contains now is it. Also note contains is case sensitive.
    No and I never said it was. I said there was a username variable. That variable would always be equal to only the username portion of the string so in this case the Contains would be looking for only "User A". The variable already has the username parsed from the full string.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    67

    Re: Help creating For Each Loop - Multiple GPS Coordinates

    FYI, I have been able to get the listbox issue resolved. This code does exactly what I was wanting:

    Code:
    Dim i As Integer
                For i = 0 To ListBox2.Items.Count - 1
                    If i > ListBox2.Items.Count - 1 Then Exit For
                    If ListBox2.Items(i).Contains(MyUnitOutput) Then
                        ListBox2.Items.Remove(ListBox2.Items(i))
                        i -= 1
                    End If
                Next
     
                For i = 0 To ListBox2.Items.Count - 1
                    If i > ListBox2.Items.Count - 1 Then Exit For
     
                    If Not ListBox2.Items(i).Contains(MyUnitOutput) Then
                        ListBox2.Items.Add(MyUnitOutput & "!" & MyOtherString)
                        Exit For
                    End If
                Next
     
                If ListBox2.Items.Count() < 1 Then
                    ListBox2.Items.Add(MyUnitOutput & "!" & MyOtherString)
                End If
    If anyone has any suggestions to clean that code up a bit, I'm all for it. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width