.NET

Reply
Active Contributor
laurie.comerford
Posts: 48
Registered: ‎09-08-2006
Message 1 of 3 (139 Views)
Accepted Solution

Modifying windows controls from external data file

139 Views, 2 Replies
12-11-2011 02:42 AM

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?

Regards

Laurie Comerford

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.

 

 

*Expert Elite*
Alfred.NESWADBA
Posts: 9,417
Registered: ‎06-29-2007
Message 2 of 3 (132 Views)

Re: Modifying windows controls from external data file

12-11-2011 04:59 AM in reply to: laurie.comerford

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 :smileywink:

 

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 -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Active Contributor
laurie.comerford
Posts: 48
Registered: ‎09-08-2006
Message 3 of 3 (111 Views)

Re: Modifying windows controls from external data file

12-12-2011 12:10 AM in reply to: Alfred.NESWADBA

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.

 

 

Regards

Laurie Comerford
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!