Hi,
VB.NET loses me again.
In my VBA program I had a function to read/write an external data file to retain and recall user settings on a form.
The nature of the function was quite simple:
For Each oObject as Control in Me.Controls
If TypeOf oObject is ComboBox
Do my thing
ElseIf etc.
......
end if
Next
This code is almost identical in my VB.NET references, but when I try to run it I find the "Controls.Count" value for the form is only 1 (that object is a Tab control) thus I'm unable to access the individual controls with this code.
There is also a "ControlCollection" object, but this appears as "Nothing".
Where should I go from here?
Solved! Go to Solution.
Hi,
one of the differences between the forms of VBA and the forms of dotNET is that dotNET has a hierarchical structure where else VBA has a straight onedimensional array of controls.
So in dotNET you have Controls that have Controls as subobjects, e.g. if you use a Panel in a form the Panel-object is to find in the Form.Controls.
If you have 2 TextBoxes laying in the Panel you won't find them under Form.Controls ... insteas you find them under Panel.Controls.
And that is where you are at the moment. As long as you try to save your Form-content in that way you have to go through "Controls"-collection recursivly.
....hope that is described clear enough 😉
And now the way you could also check: if you design your form, select a control like a TextBox and look to it's properties. You'll find a property called "DataBindings" and within that you can define variables or any settings that could automatically be saved by dotNET handling (see also My.Settings) and also retrieved at program start.
HTH, - alfred -
Hi Alfred,
Again I'm greatly indebited to you.
This time when I read the manual, I understood what it was getting at.
Some test code which illustrates what I needed is shown below:
Function RecursiveFormInformation(ByVal oShell As System.Windows.Forms.Control) As Boolean Try If TypeOf oShell Is Panel Or TypeOf oShell Is Form Or TypeOf oShell Is TabPage Or TypeOf oShell Is TabControl Or TypeOf oShell Is GroupBox Then Debug.Print(oShell.Name & " is a container control") If oShell.Controls.Count > 0 Then For Each oCntrl As Control In oShell.Controls RecursiveFormInformation(oCntrl) Next End If ElseIf TypeOf oShell Is ComboBox Then Debug.Print(oShell.Name) Else Debug.Print(oShell.Name & "Object is not a combo box, must be another thingy") End If Return True Catch ex As SystemException Return False End Try End Function ' RecursiveFormInformation ' This can be called with: RecursiveFormInformation(Me)
This may not be comprehensive, but it covers all the controls on the form I'm interested in.