 |
|
 |
''Here is an implementation that doesn't use tag properties:
Dim branchNode As Control = Me Dim branchLevel As Integer = 0 Dim numBranchesOnLevel As Integer = Me.Controls.Count Dim branchStore As New Stack(Of Integer) Dim tmpInt As Integer
While numBranchesOnLevel > 0 branchStore.Push(0)
branchNode = branchNode.Controls(0)
numBranchesOnLevel = branchNode.Controls.Count
branchLevel += 1
End While
If TypeOf branchNode Is ComboBox Then Dim theCombo As ComboBox = branchNode
theCombo.SelectedIndex = 0
End If
For branchLevel = branchLevel To 1 Step -1 branchNode = branchNode.Parent
numBranchesOnLevel = branchNode.Controls.Count
tmpInt = branchStore.Pop()
tmpInt += 1
If Not tmpInt >= numBranchesOnLevel Then
branchStore.Push(tmpInt)
branchNode = branchNode.Controls(tmpInt)
numBranchesOnLevel = branchNode.Controls.Count
branchLevel += 1
While numBranchesOnLevel > 0 branchNode = branchNode.Controls(0)
branchStore.Push(0)
tmpInt = 0
numBranchesOnLevel = branchNode.Controls.Count
branchLevel += 1
End While
End If
If TypeOf branchNode Is ComboBox Then Dim theCombo As ComboBox = branchNode
theCombo.SelectedIndex = 0
End If
Next branchLevel
|
|
|
|
 |
|
 |
Good work. Just a minor improvement by me.
Dim branchNode As Control = Me Dim branchLevel As Integer = 0 Dim numBranchesOnLevel As Integer = Me.Controls.Count Dim branchStore As New Stack(Of Integer) Dim tmpInt As Integer
While branchNode.Controls.Count > 0 branchStore.Push(0)
branchNode = branchNode.Controls(0)
branchLevel += 1
End While
For branchLevel = branchLevel To 1 Step -1 If TypeOf branchNode Is ComboBox Then Dim theCombo As ComboBox = branchNode
theCombo.SelectedIndex = 0
End If
branchNode = branchNode.Parent
numBranchesOnLevel = branchNode.Controls.Count
tmpInt = branchStore.Pop()
tmpInt += 1
If Not tmpInt >= numBranchesOnLevel Then
branchStore.Push(tmpInt)
branchNode = branchNode.Controls(tmpInt)
numBranchesOnLevel = branchNode.Controls.Count
branchLevel += 1
While numBranchesOnLevel > 0 branchNode = branchNode.Controls(0)
branchStore.Push(0)
tmpInt = 0
numBranchesOnLevel = branchNode.Controls.Count
branchLevel += 1
End While
End If
Next branchLevel
|
|
|
|
 |
|
 |
Recursion might normally be a good solution but this does give a good alternative for your bag of tricks.
I don't like to use the tag property either because there's no telling what other code might use that property. It's a simple fix... just create a Generic dictionary that uses the controls as a key. Store your level information in the value of the dictionary. This also can be used to prevent doing something more than once.
I also agree the method of checking type is not the best... I'd fix that in the example.
I'd reccommend reworking a bit.
Great Idea.
|
|
|
|
 |
|
 |
Thanks for the tips. The tag property worked for me, but I will probably do a rework using a Stack. I originally didn't spend very long on the code, so I will improve it a little more. As for the type checking - this is something I haven't done alot with. Do you have any suggestions?
Note:
Though I would like to just upgrade this article, it does not appear possible - instead I will have to post a new article. Why? Well, I recently changed my email address on Code Project. This caused all of the email to be correctly sent to me, but suddenly my user would not work. In fact, the email address I changed to and the email address I used before were listed as unused user names in the Code Project. This being said, I used the same email address that I originally changed my old user to and re-setup my user. Hmm...
|
|
|
|
 |
|
 |
To check if an object is a certain type in Visual Bisic use this syntax
If TypeOf controlToCheck Is ComboBox Then
'do work
End If
|
|
|
|
 |
|
 |
Dim ctl as control
For each ctl in me.controls
select case true
case ctl.something=something
end select
Next
|
|
|
|
 |
|
 |
This method may appear to work (and will for very simple forms), but you will begin to notice that if your form has controls within several group boxes or there is any branching to the controls, it will not go beyond one branch. Do this simple test: Place two group boxes on a form and then place several controls within the group boxes. The simple method will not reach the controls in all of the group boxes.
-- modified at 8:58 Tuesday 7th November, 2006
|
|
|
|
 |
|
 |
I'm curious why you would not want to use recursion.
|
|
|
|
 |
|
 |
Recursion is eaiser to understand And what is the strange way you check types for compatibility?
|
|
|
|
 |
|
 |
Sometimes recursion is the best solution, but take the case where the number of controls on the form gets really large. If there are a lot of tabs, group boxes and other layered controls, then recursion will not be the best solution. Recursion requires more stack memory every time a recursive call is made. If too many recursive calls are made you may just find yourself out of memory in a tight situation.
|
|
|
|
 |
|
 |
Logically, its recursion that is required, and it seems to me totally perverse to avoid what is a simple and elegant solution in favour of this rather messy code.
brian smith
|
|
|
|
 |
|
 |
Actually, recursion is not always the best solution - in fact there are many examples in the coding world where recursion will fail because of memory constraints. If you have taken classes in algorithm development, you will discover that this solution is a rather elegant one, and simple as well. Also keep in mind that because all of the memory allocated within a recursive call must be saved to the stack and then reallocated for the next call, recursion will probably also be slower.
|
|
|
|
 |
|
 |
Sorry Ky, but elegant - Not.
1. your action code (setting Combo index to 0) is performed in two separate places
2. you zap the tag of every control on the form, permanently
3. you visit each control twice
The memory overhead for recursion is not going to be a problem in this scenario - its unlikely to get beyond 6 or 7 levels deep, and costs very little in overall terms.
brian smith
|
|
|
|
 |
|
 |
Yes, I see your point for items 1 & 2. Item 3, however, is not an issue because nothing is done to the control more than once. Recursion would surely be worse for item 3 than for my code. Recursion may not visit every control more than once explicitly, but it does implicitly (it visits the entire function again).
Mostly recursion versus non-recursion in THIS SITUATION is a matter of personal preference. There are probably a balanced number of pro's and con's for each. Though this situation may be so, the algorithm itself is rather eloquent (though ancient).
|
|
|
|
 |
|
 |
Recursion is sometimes a good solution, but it is most often not the best solution. Recursion does make code easier to understand (in most cases), but it also requires more memory and can drastically reduce the speed of your code in a highly recursed function.
|
|
|
|
 |