2

I'm translating my VB.Net application, and I need to loop through all the controls on my form. Using a recursive function such as

Public Sub TranslateControl(ByVal Ctrl As Control)
    For Each ChildCtrl As Control In Ctrl.Controls
        ChildCtrl.Text = Translate(ChildCtrl.Text)

        If TypeOf ChildCtrl Is Label Then
            CType(ChildCtrl, Label).Tag = Translate(CType(ChildCtrl, Label).Tag)
        End If

        TranslateControl(ChildCtrl)
    Next
End Sub

works very well, but it doesn't include CommonDialog objects, such as FolderBrowser objects. How can I access these objects ? I tried this

    For Each ChildDialog As CommonDialog In Ctrl.Controls
        ChildDialog.Tag = Translate(ChildDialog.Tag)
    Next

But there is obviously an inheritance problem, since CommonDialog objects are not controls.

Is there a way for me to loop through really all the items displayed on my form?

Thanks a lot!

CFP

Clément
  • 12,299
  • 15
  • 75
  • 115

2 Answers2

1

No, they are components, not controls. Their code actually lives in the shell, they were written in unmanaged C/C++ by Microsoft. The only thing that's managed about them is a small wrapper that makes the necessary API calls to display them and return their result. OpenFileDialog for example.

The very first problem you'll run into is run your code when such a dialog is displayed. It is a dialog, control doesn't return to your program after the ShowDialog() call until the user dismisses it. It is possible with a fair amount of trickery. Check my code in this thread for the approach. As noted, that code will work for any shell dialog, as well as MessageBox.

That gets you the window handle of the dialog. Next, you have to iterate the child windows of the dialog. You can do that with the EnumChildWindows API call. That gives you the window handle of each child, you can then use SendMessage() to do something with the child. Whatever that might be, you didn't specify that in your question.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks for your answer! However, I must have badly formulated my question. What I meant was: I already have a way to enumerate the controls on my form. However, this method only enumerates controls, and not all items. I'm searching for a method to also enumerate other items which appear on my form, such as `FolderBrowserDialog`, too. For example, suppose I have a form with one label, and one browse button, which displays a FolderBrowserDialog object. I would like to be able to list all items that are on the form, including the FolderBrowser. Thanks! – Clément Jan 30 '10 at 15:23
  • No kidding. You can iterate some of the components on the form by iterating Me.components.Components. That doesn't yield the dialog objects though, they don't get added to the collection. Only Reflection is going to dig them out of the form object. – Hans Passant Jan 30 '10 at 15:57
-2
 Friend Sub resetFormControls(zForm As Form)

Try a subroutine to reset all controls back to non-used state: blank text box, unchecked checkbox and radiobutton, etc.

        For Each zCntl As Control In zForm.Controls
            If zCntl.HasChildren Then
                For Each zChildCntl As Control In zCntl.Controls
                    If zChildCntl.GetType Is GetType(CheckBox) Then
                        CType(zChildCntl, CheckBox).Checked = False
                    End If

                    If zChildCntl.GetType Is GetType(TextBox) Then CType(zChildCntl, TextBox).Text = ""
                    If zChildCntl.GetType Is GetType(TextBox) Then CType(zChildCntl, TextBox).BackColor = Color.White
                    If zChildCntl.GetType Is GetType(RichTextBox) Then CType(zChildCntl, RichTextBox).Text = ""
                    If zChildCntl.GetType Is GetType(RichTextBox) Then CType(zChildCntl, RichTextBox).BackColor = Color.White
                    If zChildCntl.GetType Is GetType(RadioButton) Then CType(zChildCntl, RadioButton).Checked = False

                Next
            End If
            If zCntl.GetType Is GetType(CheckBox) Then CType(zCntl, CheckBox).Checked = False
            If zCntl.GetType Is GetType(TextBox) Then CType(zCntl, TextBox).Text = ""
            If zCntl.GetType Is GetType(TextBox) Then CType(zCntl, TextBox).BackColor = Color.White
            If zCntl.GetType Is GetType(RichTextBox) Then CType(zCntl, RichTextBox).Text = ""
            If zCntl.GetType Is GetType(RichTextBox) Then CType(zCntl, RichTextBox).BackColor = Color.White
            If zCntl.GetType Is GetType(RadioButton) Then CType(zCntl, RadioButton).Checked = False
            If zCntl.GetType Is GetType(DateTimePicker) Then CType(zCntl, DateTimePicker).Text = Now.Date
            If zCntl.GetType Is GetType(ComboBox) Then CType(zCntl, ComboBox).SelectedIndex = 0

        Next
        Application.DoEvents()

    Catch ex As Exception

    End Try
End Sub
rgettman
  • 176,041
  • 30
  • 275
  • 357
Pat
  • 1