|
I suspect I can solve my problem on my own, but I'm wanting to find the most efficient solution.
I have a form with several picture boxes on it as well as a handful of other controls. I would like to cycle through the controls and, if the control is a PictureBox, remove it. I tried this:
Private Sub CreateAndDisplayTableau(ByRef frm As Form)
'Destroy picture boxes on form
Dim ctrl As Control
For Each ctrl In frm.Controls
If TypeOf ctrl Is PictureBox Then
frm.Controls.Remove(ctrl)
End If
Next
End Sub
I see now that what this does is only removes half of the PictureBox. If the first PictureBox is the 4th control encountered out of 20 controls, after removal, the 5th control becomes the 4th control and, since the 4th control was already evaluated, it gets skipped.
I see that I could set the Tag property to "Mark for deletion" or something and then cycle back through and delete them that way. Or, I could use a For Next Loop and decrement the counter after removal. So I can solve this problem, I feel sure. But what is the most efficient solution?
|
|
|
|
|
When deleting from a collection, it is best to start at the end and work backwards... count downto 0 or some such thing.
|
|
|
|
|
I thank you for your response. I am biased to using For Each when I can, but maybe that's just silly prejudice. I will iterate backwards though the collection - this works.
|
|
|
|
|
you cannot delete the picture boxes if the picture boxes is inside the controls of controls
|
|
|
|
|
I'm not exactly sure what you mean though I have since realized I must call:
ctrl.Dispose
in order to destroy the picture box. Presumably I could do this whether "detaching" it from the form's control collection or not?
|
|
|
|
|
depends on how you attempt to remove the control.
if you use
For Each ctrl as Control in Me.Controls
Next
it complains at runtime that the collection has changed. but as someone else pointed out using a pointer variable and moving from the end of the list to the start i.e.
dim NumberOfControl as Integer = Me.Controls.Count
for Counter as integer = 0 to NumberOfControls step -1
if typeof ctrl is picturebox then
end if
Next
it doesn't give you any hassle like the for each approach
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
Simon_Whale wrote: for Counter as integer = 0 to NumberOfControls step -1
Shouldn't it be
for Counter as integer = NumberOfControls to 0 step -1
|
|
|
|
|
yes it should sorry I typing in between meetings on my windows phone
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
Yes, that's essentially the solution I'm using now.
Thank you everyone.
|
|
|
|
|
Hello!
Got a little problem and i'm sure that you guys could help me easily.
I have to List-Controls 1. CONTACT-LIST 2. VIP-LIST
You could add contacts to the vip-list per drag&drop.
Next step is, that i would like to delete vip-list-entries when i just drag them out of the vip-list.
How could i do that? Because the entry should be deleted when left mouse button goes up and the mouse is not over the vip-list. Because the event "PreviewMouseLeftButtonUp" could be fired everywhere (outside of the applicationwindow, somewhere else on my applicationwindow) i don't know how to handle it.
I hope you understand my problem?!
Thanks for your help!
|
|
|
|
|
We got a WPF-forum; I haven't used the tech, but..
If it works like WinForms, there will not be any event that tells you it's dropped "outside" of your application. We worked around the limitation once by implementing the dragdrop to the shell, but that was a dirty and expensive hack that simply didn't work.
Simplest way is to "not use" drag and drop, and simply have a button that deletes the selected item. You will not get notifications from other apps if the item is dropped there.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Try using the DragLeave event. It won't cancel the drag-drop, but it will allow you to remove the entry from the list once the mouse leaves the control.
|
|
|
|
|
I want to build a module to combine multiple text file in a .csv file by using vb.net.
My text file data like this:
(T1.txt)
UnitCode|Qty
001|56
002|45
003|100
004|78
005|67
(T2.txt)
UnitCode|Qty
001|78
002|166
003|10
005|12
I want the output like this:
(Output.csv)
UnitCode,T1,T2
001,56,78
002,45,166
003,100,10
004,78,
005,67,12
Anyone expert can help me?
|
|
|
|
|
You need to think this through logically. Use a StreamReader to open both files, use String.Split on each line of each text file and add it to a StringBuilder on each iteration, using a comma to separate each item. When you have finished traversing the text files, you can save the csv file using a StreamWriter.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
|
Just a simple sample
Sub Main()
Dim dict = New Dictionary(Of String, List(Of Integer))
Dim unitCode = String.Empty
Dim qty = 0
For Each file In GetFiles()
For Each row In file
unitCode = GetUnitCode(row)
qty = GetQuantity(row)
If dict.ContainsKey(unitCode) Then
dict(unitCode).Add(qty)
Else
Dim qtyList = New List(Of Integer)
qtyList.Add(qty)
dict.Add(unitCode, qtyList)
End If
Next
Next
End Sub
Hope that helps!
|
|
|
|
|
I was thinking of basically the same solution.
stevelk,
At then end of the process you need to write the contents of the dictionary out and you will have the desired combined results.
|
|
|
|
|
|
Hello,
I'm newbie in VB.Net(VB2010), maybe you can help me on my problem about the loading of data from SQL to text boxes on form load, and also i have created a button for navigation, inserting, delete, updating the record.
You have any sample code/form for this scenario. Hoping you can help me guys.
Thank You So Much.
Jon
|
|
|
|
|
jon2371 wrote: sample code/form
Nope. Sure don't. Not very likely that anyone is going to hand over a solution to you. It's expected that people try doing things for themselves and ask specific questions relating to a specific problem that they are having.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
There is lots of sample code, complete with details in the CodeProject Articles[^] section.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi,
I want to change thye lcd display of a G510 keyboard.
I have looked but didn't find anyting in vb.net(2010). I foiund a lot of C but that I don't know.
Can someone point me into a good direction with samples in vb.net
I found 1 sample that reads part of the display.
Jan
|
|
|
|
|
jan212r wrote: I want to change thye lcd display of a G510 keyboard.
Coool project! There are .NET examples for the G15 keyboard, loved trying them. Loved it a bit less once I saw the price of the keyboard, and with no idea on whether the product would be continued, I dropped the idea.
You might want to download one of the examples for the older versions of the gaming-keyboard, they might be compatible. I doubt that anyone has an example-version that targets a specific keyboard in a language like VB, but you should be able to use the C#-libraries from VB. You'd probably also want the SDK, as it contains samples on how to set the display.
I'd love to see an article on this subject!
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy,
the price is not that high (100€ in Be)
the older models are compatible (normaly).
the sdk is free on download and on the site
there are no real libs. only 2 dll and almost no documentation
Jan
|
|
|
|
|
jan212r wrote: the price is not that high (100€ in Be)
Just noticed, did a quick Google. Thank you for the hint, wouldn't have looked for the current price without a small push.
jan212r wrote: the older models are compatible (normaly).
That's what I expect from Windows, but it's not always guaranteed with other manufacturers. I stopped working on it when I got doubts on whether the product-line would be continued in the future.
jan212r wrote: there are no real libs. only 2 dll and almost no documentation
There's a wrapper on CodePlex[^], an example on using it is here[^]. If you don't mind the German, there's also a P/Invoke-example on this[^] forum.
Working without docs is a bit harder, but also a bit more rewarding; I can imagine Logitech not putting much effort in .NET documentation for the display. A shame, since it's really a convenient technology, and IMO a better location for status-indicators than the tray-icon.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|