tags:

views:

68

answers:

4

I have a form set up with 25 RichTextBox controls. I need to clear them all on a button click (get rid of all text inside them).

I hav tried accessing the controls programmatically but can't. I just need every RichTextBox on the form to clear.

Any code samples would be great, thank you.

UPDATE: I wrote this:

    For Each oControl As Control In Me.Controls
        If TypeOf oControl Is RichTextBox Then
            oControl.Clear()
        End If
    Next

But for some reason it doesn't work.

A: 

What about the RichTextBox.Clear() method?

You can iterate over every control in your form (using MyBase.Controls) and check whether it's a RichTextBox and then call the Clear() method on it.

Joey
A: 

The controls of a form can be obtained via the Controls property. The problem is that some of these controls can be ContainerControls, which in turn can contain other controls.

Here is an approximate example in C# (sorry, I'm not fluent in VB; I hope you get the idea anyway):

void ClearRichTextBoxes(ContainerControlcontainer)
{
    foreach(var control in Controls) {
        if(control is ContainerControl) {
            ClearRichTextBoxes(control);
        } else if(control is RichTextBox) {
            ((RichTextBox)control).Clear();
        }
    }
}

You would invoke it like ClearRichTextBoxes(form).

UPDATE: This is wrong! You don't need to iterate throgh nested ContainerControls. From MSDN:

Use the Controls property to iterate through all controls of a form, including nested controls.

So the above code should work removing the if(control is ContainerControl) {...} else part.

Konamiman
A: 

What code you are using to access the RichTextBox control programatically? The answer Johannes given above should work. You can loop thru the controls collection and check typeOf().. and call the clear() method().

sajoshi
A: 

This is very weird, I just tried reproducing the problem with a simple form, containing 3 RichTextBoxes. One of the boxes lives inside a panel, and this one doesn't clear with the for each control in controls... method.

This is strange, because as others have said already, it should work.

Here's my solution: explicitly recurse over all controls in a Form (which itself is a Control as well). Use ClearControl(Me) in your code (Me is a Form is a Control).

Implementation:

'If a control has a collection of sub-controls, it's a container.
'In this case: recurse over its children until you hit a child without sub-controls.
'Then check if it's a (rich)TextBox and clear.
Private Sub ClearControl(ByVal ctrl As Control)

    If ctrl.Controls.Count > 0 Then
        For Each subCtrl As Control In ctrl.Controls
            ClearControl(subCtrl)
        Next
    End If

    If TypeOf ctrl Is RichTextBox Then
        DirectCast(ctrl, RichTextBox).Clear()
    End If

    'You can clear other types of controls in here as well
    If TypeOf ctrl Is TextBox Then
       DirectCast(ctrl, TextBox).Clear()
    End If
    'etcetera...

End Sub

I hope this works for you.

cfern