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.
Re: Need help with my loop.
:wave:
What line of the code are you getting this error on?
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)"
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:
ListView1.Items.Clear()
Dim page As RadPageViewPage = RadPageView1.SelectedPage
For Each txt As FastColoredTextBox In page.Controls.OfType(Of FastColoredTextBox)()
If bookmarks.Count = 0 Then
Return
End If
For i As Integer = 0 To bookmarks.Count - 1
Dim id As Integer = bookmarks(i)
Dim boom As String
ListView1.BeginUpdate()
For f As Integer = 0 To txt.LinesCount - 1
If txt(f).UniqueId = id Then
Dim breaklist As New ListViewItem
breaklist.Text = CStr((f + 1))
boom = txt.GetLineText(f)
breaklist.SubItems.Add(boom)
ListView1.Items.Add(breaklist)
End If
Next
ListView1.EndUpdate()
Next
Next
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:
Private bookmarksLineId As New Dictionary(Of Integer, Integer)()
' Index - bookmark number, Value - Line.UniqueId
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.
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))
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?
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).
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.
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?
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.
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?
Re: Need help with my loop.
Quote:
Originally Posted by
dolot
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:
Dim bookmarks As New List(Of Integer)
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.
Re: Need help with my loop.
Quote:
Originally Posted by
JoePage
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
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.
Re: Need help with my loop.