Results 1 to 18 of 18

Thread: Need help with my loop.

  1. #1
    Addicted Member
    Join Date
    Aug 11
    Posts
    200

    Need help with my loop.

    So I posted a thread lastnight looking for help on a problem I ran into. Since then I found a better way I could do it an I almost have it where I want it but need a little help. Here a quick rundown.

    As the title says this is basically my problem. In my application I have a feature to add breakpoints/bookmarks in the textbox that the user is inputting information into. As they add the breakpoint it adds the line number that the item is on and also the contents of that line to a listview. Line number in first column line content goes in the second column. So now I have added multiple document abilities in the application so I devised a way to have it clear the listview on tab change and re- add all breakpoints that are in the current textbox.

    The only problem is that it only works for the first breakpoint added. Then I get an error stating something along the lines that the number three cant be added more than once please remove it or clone it. Im thinking I have something wrong with the multiple loops. Maybe I need to change the location of the loops?

    Anyways if someone could look at it and tell me what is wrong I would appreciate it.

    vb Code:
    1. Private Sub page_TabIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    2.  
    3.         For Each lvitem As ListViewItem In ListView1.Items
    4.             lvitem.Remove()
    5.         Next
    6.         Dim breaklist As New ListViewItem
    7.         Dim page As RadPageViewPage = RadPageView1.SelectedPage
    8.         For Each txt As FastColoredTextBox In page.Controls.OfType(Of FastColoredTextBox)()
    9.             For i As Integer = 0 To bookmarks.Count - 1
    10.                 Dim id As Integer = bookmarks(i)
    11.                 Dim boom As String
    12.                 For f As Integer = 0 To txt.LinesCount - 1
    13.                     If txt(f).UniqueId = id Then
    14.                         breaklist.Text = CStr((f + 1))
    15.                         boom = txt.GetLineText(f)
    16.                         breaklist.SubItems.Add(boom)
    17.                         ListView1.Items.Add(breaklist)
    18.                     End If
    19.                 Next
    20.             Next
    21.         Next
    22.     End Sub

  2. #2
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: Need help with my loop.

    You should probably use ListView1.Items.Clear instead of the loop.
    Also ListView1.BeginUpdate / EndUpdate before and after adding items is a good idea.

    #EDIT: Once again poorly phrased - you should have a BeginUpdate before you begin adding items, and EndUpdate when all items have been added. This to prevent constant updating of the listview. This however will have no impact on possible errors.

    #EDIT2: And ofc I didn't even notice: you're decalring the new listviewitem (breaklist) outside the loop. It should ofc be inside the loop, so a new listviewitem is generated on each iteration.
    Last edited by ThomasJohnsen; Aug 10th, 2012 at 02:39 PM.
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  3. #3
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    745

    Re: Need help with my loop.


    What line of the code are you getting this error on?
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


  4. #4
    Addicted Member
    Join Date
    Aug 11
    Posts
    200

    Re: Need help with my loop.

    Thanks thomas I will add those changes now.

    And dolot I am getting the rror on this line "ListView1.Items.Add(breaklist)"

  5. #5
    Addicted Member
    Join Date
    Aug 11
    Posts
    200

    Re: Need help with my loop.

    Thanks again Thomas I added those changes and it's working great. I really appreciate the advice aswell. I am going to use your advice with other parts containing listviews in my application aswell.

    Here is the code after adding your suggestions.

    vb Code:
    1. ListView1.Items.Clear()
    2.                 Dim page As RadPageViewPage = RadPageView1.SelectedPage
    3.         For Each txt As FastColoredTextBox In page.Controls.OfType(Of FastColoredTextBox)()
    4.             If bookmarks.Count = 0 Then
    5.                 Return
    6.             End If
    7.             For i As Integer = 0 To bookmarks.Count - 1
    8.                 Dim id As Integer = bookmarks(i)
    9.                 Dim boom As String
    10.                 ListView1.BeginUpdate()
    11.                 For f As Integer = 0 To txt.LinesCount - 1
    12.                     If txt(f).UniqueId = id Then
    13.                         Dim breaklist As New ListViewItem
    14.                         breaklist.Text = CStr((f + 1))
    15.                         boom = txt.GetLineText(f)
    16.                         breaklist.SubItems.Add(boom)
    17.                         ListView1.Items.Add(breaklist)
    18.                     End If
    19.                    
    20.                 Next
    21.                 ListView1.EndUpdate()
    22.             Next
    23.         Next

  6. #6
    Addicted Member
    Join Date
    Aug 11
    Posts
    200

    Re: Need help with my loop.

    Okay so im having another issue, that I was going to create another thread but it is basically part of the same issue. Now that I have the code working for it to clear then display the information. I realized that I have another problem that needs to be fixed for it to actually work.

    In the beginning of the code on the form I declared the bookmarks as a list along with the bookmarkslineid(below). So that these bookmarks could be added to a list so I could re-use the data. as you seen in the code I posted earlier

    vb Code:
    1. Private bookmarksLineId As New Dictionary(Of Integer, Integer)()
    2.     ' Index - bookmark number, Value - Line.UniqueId
    3.     Private bookmarks As New List(Of Integer)()

    Well I realized that I need to have a new bookmarks list for each new tab(or more specifically each dynamically created textbox), So I can save the bookmarks from that tab into that list and only that list(just like we did for the listview) and be able to to display them from only that list.

    So with that being said the problem I am having is that when I add a bookmark to a document in my app on say line three, then I add a new document and add a bookmark on line 3, I cant because it is using the same list(that already has a bookmark on line 3)

    So how can I dynamically create the new list for each tab and use them? or even modify it? Im at a complete loss on how to do this.

  7. #7
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    745

    Re: Need help with my loop.

    You can have a dictionary of dictionary values, so that you track each dictionary by tab id

    Code:
    Dim X As Dictionary(Of Integer, Dictionary(Of Integer, Integer))
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


  8. #8
    Addicted Member
    Join Date
    Aug 11
    Posts
    200

    Re: Need help with my loop.

    Thats sounds exactly like what I need. How to I correlate between the tab and the dictionary. Basically assign a dictionary to the tab?

  9. #9
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: Need help with my loop.

    In a situation like this, I would probably use the .Tag property. All controls have a .Tag property, which allows you to store additional info pertaining to that particular control. It is of type object (in order for it to be able to hold any type of data), meaning that you will have to type-cast it to a dictionary to get it back once it has been stored (most likely using DirectCast or similar).
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  10. #10
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    745

    Re: Need help with my loop.

    Actually, I was thinking of using the tab's index as the key of the first 'dictionary' to keep track of it, but you could change the dictionary to have a string as the key and use the tab's name.

    Or, as Thomas suggested, you could skip the 'dictionary of dictionaries' alltogether and just assign the dictionary object to the .tag property of the tab control. There's different ways to skin the cat, depending upon your specific needs.
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


  11. #11
    Addicted Member
    Join Date
    Aug 11
    Posts
    200

    Re: Need help with my loop.

    Yea, I thought about adding it to the .tag tag property of the tab, how do I assign the dictionary into the .tag? this is the problem that is confusing me?

  12. #12
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    745

    Re: Need help with my loop.

    It's pretty simple really:
    Code:
    Dim TP As New Windows.Forms.TabPage
    
    TP.Tag = New Dictionary(Of Integer, Integer)
    Then to access it later, you do:
    Code:
    Dim Dict As Dictionary(Of Integer, Integer)
    
    Dict = CType(TP.Tag, Dictionary(Of Integer, Integer))
    The Tag property is just an object reference - you can assign anything to it you want.
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


  13. #13
    Addicted Member
    Join Date
    Aug 11
    Posts
    200

    Re: Need help with my loop.

    Thanks for the reply dolot. It really helps. My only problem issue is that I am using the tab tag property to store the filename of the current file if it was opened or saved. Do you know where else I could maybe save either?

  14. #14
    Addicted Member
    Join Date
    Aug 11
    Posts
    200

    Re: Need help with my loop.

    Quote Originally Posted by dolot View Post
    It's pretty simple really:
    Code:
    Dim TP As New Windows.Forms.TabPage
    
    TP.Tag = New Dictionary(Of Integer, Integer)
    Then to access it later, you do:
    Code:
    Dim Dict As Dictionary(Of Integer, Integer)
    
    Dict = CType(TP.Tag, Dictionary(Of Integer, Integer))
    The Tag property is just an object reference - you can assign anything to it you want.
    Hey Dolot thanks for the help, your suggestion works. I decided to save it to the dynamically created textbox instead. Now my only problem is I have a list that saves all the bookmark information I have any more items with tags to save it to. do you have any ideas?
    vb Code:
    1. Dim bookmarks As  New List(Of Integer)

  15. #15
    Addicted Member
    Join Date
    Aug 11
    Posts
    200

    Re: Need help with my loop.

    Ok so whether or not it is the right way I dynamically added another textbox into the tab page and set its visible property to false an set its tag as the new list. It works perfectly.

    I just want to say thanks again for the help. I probably would still be scratching my head with no ideas if it weren't for the help.

  16. #16
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: Need help with my loop.

    Quote Originally Posted by JoePage View Post
    Ok so whether or not it is the right way I dynamically added another textbox into the tab page and set its visible property to false an set its tag as the new list. It works perfectly.

    I just want to say thanks again for the help. I probably would still be scratching my head with no ideas if it weren't for the help.
    Just a little heads-up. This is not considered good coding practise (at least not IMO). You should make a class or struct, that will hold all information pertaining to a tabpage (in your case only a dictionary and a bookmarks list) and then add a reference to that in the .Tag property. That way you will be able to access everything without having to resort to .Tag properties of invisible controls. Or use dolot's suggestion and make public lists containing all info on tabpages, and access it through those.
    I'm not trying to tell you how you should write your program - just a warning, that using such measures as invisible controls will make code that can be difficult for others (and even yourself) to read/understand. There are of course also speed issues, though I doubt a dynamically created TextBox will have any significant bearing on execution time .

    Glad that your code is working though, and if your program is only for self-use (or practise) then feel free to just ignore this piece of advice

    Regards Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  17. #17
    Addicted Member
    Join Date
    Aug 11
    Posts
    200

    Re: Need help with my loop.

    Hi tom. Thanks for the advice, I am going to follow it. I could tell it wasn't the right way. I didn't feel comfortable doing it, but was looking for the quick fix. I wish I would of known I could create a class and then assign it to the tag, it seems like that is a solve all to this problem, plus it helps when I want to add more features. Thanks again.

  18. #18
    Addicted Member
    Join Date
    Aug 11
    Posts
    200

    Re: Need help with my loop.

    Double post.

Posting Permissions

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