ultraman Posted September 2, 2003 Posted September 2, 2003 Hi ! Another really stupid thing to do with VB6 just turn into a nightmare with the .NET version... I'm just trying to loop throu all the controls of my Form, butnow, the Controls collection just give you access to top level controls, which means you have to drill down to get all the controls in GroupBox. I've seen some thread (in this forum) that suggests that I can use a recursive method, but I just can't make it work properly. Here's the code : Protected Overridable Sub AddTextBoxHandler(ByRef colControls As ControlCollection) Dim ctrlTemp As Control For Each ctrlTemp In colControls If TypeOf ctrlTemp Is GroupBox Then Try AddTextBoxHandler(DirectCast(grpTemp, GroupBox).Controls) Catch e As Exception MessageBox.Show(e.Message & vbCrLf & ctrlTemp.Name & " --- " & grpTemp.GetType.Name) End Try End If If TypeOf ctrlTemp Is TextBox Then AddHandler ctrlTemp.TextChanged, AddressOf CellTextChanged Next ctrlTemp End Sub As soon as I'm trying to call it back with the Controls collection of the GroupBox, I get an "Invalid cast" error. But the type is Windows.Forms.ControlCollection. Does anyone have an idea on what I'm doing wrong here ? Quote Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone! --Homer Simpson
*Experts* mutant Posted September 2, 2003 *Experts* Posted September 2, 2003 You can go through child controls of a control like this: dim ctrl as Control Dim ctrl2 as Control For Each ctrl in Me.Control If typeOf ctrl Is GroupBox Then 'if its a groupbox For Each ctrl2 In ctrl.Controls 'go through all controls in a groupbox 'do something to each control Next End If Next Quote
ultraman Posted September 2, 2003 Author Posted September 2, 2003 I totally agree with you on that one. As a matter of fact, that's the solution I tried first, but I then have to limit the number of drill down, because I would create only a certain number of temp "ctrl". That's basically why I'm searching for a more flexible solution, because you can have a lot of GroupBoxes embedded in one another, like russian nesting dolls... Quote Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone! --Homer Simpson
Administrators PlausiblyDamp Posted September 2, 2003 Administrators Posted September 2, 2003 You could always use a recursive function Dim ctrl As Control private sub Func1 (ctlParent as control) For Each ctrl In ctlParent If TypeOf ctrl Is GroupBox Then 'if its a groupbox Func1(ctrl) End If 'Do something to control here Next Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ultraman Posted September 2, 2003 Author Posted September 2, 2003 I just found this exemple on another forum. This is about the same as yours, but this one manages Levels, so it can dig in all controls of a Form, no matter what it is. 'global variables, may not have to be Dim allControls As StringBuilder Dim level As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load allControls = New StringBuilder allControls.Append(Me.Name & vbCrLf) level = 0 listControls(Me) MessageBox.Show(allControls.ToString) End Sub Private Sub listControls(ByVal ctrl As Control) Dim c As Control Dim i As Integer Dim j As Integer For i = 0 To ctrl.Controls.Count - 1 level += 1 For j = 1 To level allControls.Append(vbTab) Next allControls.Append(ctrl.Controls(i).Name() & vbCrLf) listControls(ctrl.Controls(i)) level -= 1 Next End Sub Hope this helps other people in the future ! Thanks guys Quote Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone! --Homer Simpson
ultraman Posted September 2, 2003 Author Posted September 2, 2003 Well, looking to it closely, the Levels are just there for display. No real use. But it shows a differnt way of looping thru the controls with the Count property instaead of For Each Next. Anyway, it works ! Quote Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone! --Homer Simpson
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.